在Spring Form标记中为输入字段设置动态路径名

时间:2018-12-24 07:30:18

标签: spring

我试图通过基于jsp页面中计数值的spring表单标签动态设置路径名。我试图像这样设置路径

<form:select path = "relatives[<%=count%>].student.year.id"class="SiblingYear"> 

但显示错误java.lang.NumberFormatException:对于输入字符串:“ <%= count%>”

下面是完整代码:

<%int count=0;
 %>
   <tr align="center" id="dvtext1">
                <td>
                   <form:select path = "relatives[<%=count%>].student.year.id"  class="SiblingYear">
   <form:option value = "-1" label = "Select Year"/>
   <form:options items = "${student.years}" itemValue="id" itemLabel="name" />
</form:select>

如果count的值为零,我希望路径名为relatives[0].student.year.id

任何人都可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

这可以通过使用JSTL Core库来实现。

<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core_rt" %>

<%int count=0;%>
<c:set value="<%=count%>" var="countindex" />
<form:select path = "relatives[${countindex}].student.year.id"  class="SiblingYear">

作为一个整体示例:

<%int count=0;%>
<c:set value="<%=count%>" var="countindex" />
<tr align="center" id="dvtext1">
  <td>
    <form:select path = "relatives[${countindex}].student.year.id"  class="SiblingYear">
      <form:option value = "-1" label = "Select Year"/>
      <form:options items = "${student.years}" itemValue="id" itemLabel="name" />
   </form:select>

供参考: https://developer.ucsd.edu/develop/user-interface-3/building-a-form/form-binding-with-collections.html