2013-10-10 39 views
0

學習jsps。懷疑嵌套的表達。我在servlet中創建一個列表和映射,將它添加到請求屬性並將其分派給jsp。在jsp中,我只是想讀取值。在jsp中使用嵌套表達式(el)

List<String> someList = new ArrayList<String>(); 
    someList.add("one");someList.add("two");someList.add("three"); 
    Map<String,Integer> m = new HashMap<String,Integer>(); 
    m.put("one", 1); 
    m.put("two", 2); 
    m.put("three", 3); 
    req.setAttribute("someList",someList); 
    req.setAttribute("hmap", m); 

JSP代碼:

<body> 
    <br> list : ${someList} 
    <br> 
    <br> map:${hmap} 
    <br /> 
    <br> using JSTL: ${hmap["${someList['0']}"]} is the value for key 
    ${someList["0"]} 
    <br /> 
    <br /> USING hardocded ${hmap["one"]} is the value for key one 
    <br /> 
</body> 

HTML輸出

list : [one, two, three] 

map:{two=2, one=1, three=3} 

using JSTL: is the value for key one 

USING hardocded 1 is the value for key one 

我爲什麼要使用嵌套JSTL漸漸空虛空間:$ {HMAP [」

設置在servlet的屬性$ {someList ['0']}「]},這是正確的方式嗎?

回答

0

試試這個:

${hmap[someList["0"]]} 
+0

奏效..所以內部EL沒必要用$ {}和引號包圍他們? –

+0

是的,$ {}應該只有一次。 – Alex

+0

oh ok ..因爲即使我引用像$ {list [「index」]}中的索引,應該使用引號,所以我想在這裏也需要引號。感謝您的回答 –