2015-06-20 48 views
1

我正在與CQ5第一次合作,請幫助我!如何在JavaScript中訪問存儲在會話中的hashmap?

我在jsp中設置了會話中的鍵/值hashmap。現在我的問題是,我將如何在會話中獲取該hashmap,並將其設置爲JavaScript變量作爲鍵值對。

我的javascript是在不同的文件夾中,我可以像訪問下面的代碼一樣訪問jstl標籤。

var hashMapFields = { 
    <c:forEach var="entry" items="${hashmapFields}"> 
    '${entry.key}': '${entry.value}', 
    </c:forEach> 
}; 

我該如何使用數據屬性來做到這一點。

這將是實施的好方法。

回答

0

您的方法應該可行。您也可以將hashmap轉換爲JSON並將其存儲在Session中。

Map<String, String> map = new HashMap<>(); 
map .put("A", "1"); 
map .put("B", "2"); 
map .put("C", "3"); 

Gson gson = new Gson(); 
String json = gson.toJson(map); 

session.setAttribute("map", map); 

JSP:

var map = '${map}'; 

https://code.google.com/p/google-gson/

+0

謝謝你的respnse,但我想是這樣的console.log( 「map.testId」);但它扔我一個錯誤。我怎麼會得到我的密鑰和價值在JavaScript – maddy

+0

嘗試像這樣:for(var i in map){console.log(i);}或者只是一個值console.log(map ['A']); – Sas

相關問題