【问题标题】:Resolving JSTL attribute解析 JSTL 属性
【发布时间】:2017-03-03 01:21:01
【问题描述】:

我想动态生成模型属性名并在JSP中使用。

对于情商:

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>

【问题讨论】:

  • 为什么要为每个整数设置单独的模型属性?您可以直接添加整数集合/数组作为模型属性。并使用 JSTL 检索。

标签: java spring jsp jstl


【解决方案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>

【讨论】:

    【解决方案2】:

    要打印出你需要使用 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

    您还应该将整数添加到某种列表中,然后将该列表添加为模型属性。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多