2017-03-03 85 views
2

我想動態生成模型屬性名稱並在JSP中使用它。解決JSTL屬性

對於當量: -

for (Integer integer : integers){  
    model.addAttribute("model_" + integer, integer); 
} 

model.setAttribute( 「整數」,整數);

在JSP

<c:foreach items=${integers} var=integer> 
${model_integer} // Want to Print the value but throwing error. 
</c:foreach> 

在此先感謝

+1

爲什麼要爲每個整數指定單獨的模型屬性? 您可以直接將整數集合/數組添加爲模型屬性。並使用JSTL進行檢索。 –

回答

0

要打印出來的價值,你就需要使用out標記

<!-- You need to surround the values of your attributes with quotes --> 
<c:foreach items="${integers}" var="integer"> 
    <c:out value="${integer}" /> <!-- the var name in the for each" --> 
</c:foreach> 

https://www.tutorialspoint.com/jsp/jstl_core_out_tag.htm

你也應該將整數添加到某種列表中,然後將該列表添加爲模型屬性。

+0

這不會起作用 –

+0

@HarshalPatil爲什麼這不起作用? – jmw5598

1

這應該有效。

<c:foreach items=${integers} var=integer> 
    <c:set var="totalBuild" value="${0}"/> 
    <c:set var="totalBuild" value="${totalBuild + integer "/> 
    <c:set var="modelAtt" value="model_${totalBuild}" /> 
    ${modelAtt} 
</c:foreach>