在 Spring MVC 中显示字符串数组
我正在尝试在 JSP 页面中显示字符串数组。
我的控制器中有一个测试字符串数组,将其设置为我的注册模型
String[] test={"ab","cb","sc","ad"};
registration.setTestArray(test);
现在我尝试在 jsp 中显示它,如果我这样做,它工作正常
<tr>
<c:forEach var="arr" items="${registration.testArray}">
<td>${arr} </td>
</c:forEach>
</tr>
但我的问题是我只想显示该数组中的一些值,例如该数组的第二个和第四个索引。
我尝试过,
<tr>
<c:forEach var="arr" items="${registration.testArray}">
<td>${arr[2]} </td>
</c:forEach>
</tr>
但它抛出了一个错误。这只是我的实际项目中的一个测试,我有很长的数组,我必须从中显示一些选定的值。
我正在考虑首先在控制器中处理所需的值,然后在 jsp 中显示它。但我不确定这是最好的方法。如果有人建议我更好的方法,那将会有很大的帮助。
I am trying to display string array in JSP page.
I have got a test String array in my controller set this to my registration model
String[] test={"ab","cb","sc","ad"};
registration.setTestArray(test);
Now I am trying to display it in jsp Its working fine if I do like this
<tr>
<c:forEach var="arr" items="${registration.testArray}">
<td>${arr} </td>
</c:forEach>
</tr>
But my problem is I want to display only some of the values from this array like 2nd and 4th index of this array.
I tried like
<tr>
<c:forEach var="arr" items="${registration.testArray}">
<td>${arr[2]} </td>
</c:forEach>
</tr>
but its throwing an error. This is just a test in my actual project I have long array of array from which I have to display some selected values.
I am thinking of doing this by first process my required values in controller and then display it in jsp. But I am not sure is this the best method. It would be great help if someone suggest me the better way.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这取决于您如何获得这些“选定值”。您可以:
${registration.testArray[2]}
c:forEach
标记< c:如果测试=“${selectedValues.contains(arrItem)}
It depends on how you get these "selected values". You can:
${registration.testArray[2]}
step
of thec:forEach
tag<c:if test="${selectedValues.contains(arrItem)}