Spring MVC-参考数据
下面是一个场景:我有这样的东西Spring MVC-参考数据,spring,spring-mvc,Spring,Spring Mvc,下面是一个场景:我有这样的东西 <form:select path="somePath" .....> <form:option value="" label="Please Select"/> <form:options items="${students}" itemValue="id" itemLabel="name"/> </form:select> 这个下拉列表很好用 但我怎样才能显示某个学生的名字呢?我想这样做: &
<form:select path="somePath" .....>
<form:option value="" label="Please Select"/>
<form:options items="${students}" itemValue="id" itemLabel="name"/>
</form:select>
这个下拉列表很好用
但我怎样才能显示某个学生的名字呢?我想这样做:
<c:out value="${students[id].name}"/>
谁能帮我弄一下语法吗
谢谢我假设
${students}
是学生对象的数组或列表。因此,它不按id编制索引,也不能通过这种方式直接访问
选择包括:
1) 将您的学生集合作为从id到学生对象的映射;然后,您的items
属性变为${students.values}
,然后您可以将单个学生作为${students[id]}
查找
2) 或者,将其保留为列表,然后遍历列表并找到id匹配的列表:
<c:forEach var="student" items="${students}">
<c:if test="${student.id==id}">
<c:out value="${student.name}" />
</c:if>
</c:forEach>
3) 或者,最后,如果您从一开始就知道您关心哪个学生,请将该学生单独包含在参考数据中。是否要显示学生姓名以响应您选择的内容?是的,类似于此。实际上,我将所有学生显示为一个网格,每行上都有编辑链接。因此,当有人想要编辑某个学生(更改姓名)时,他们会单击编辑。页面上的一个文本框显示了该学生的姓名,这样他们就可以更改姓名并单击更新按钮。谢谢Jacob.,它正在工作。。。${students}是一个映射,它的值是学生列表,所以我使用了第二个选项。