Java 如何使用 SPRING MVC 在 JSP 中动态显示 TABLE
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24279743/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to display TABLE dynamically in JSP using SPRING MVC
提问by user1782009
I want to create a table dynamically in such a way that no of headers and data i can pass from JAVA class and on no. basis, jsp should create a table accordingly.
我想以这样的方式动态创建一个表,即没有标题和数据可以从 JAVA 类传递,也没有。基础上,jsp 应该相应地创建一个表。
I have controller class in which i am returning model object which is my bean class. In that i have a list attribute in which i have hardcoded some values.These values should be by header names in the JSP table. When i am rendering this model object to jsp then i am not able to retrieve the headers info. Plz suggest. My contrller class loooks like this:
我有控制器类,我在其中返回模型对象,它是我的 bean 类。因为我有一个列表属性,我在其中硬编码了一些值。这些值应该是 JSP 表中的标题名称。当我将此模型对象呈现给 jsp 时,我无法检索标题信息。请建议。我的控制器类看起来像这样:
@RequestMapping
public ModelAndView getHeaders(PortletRequest portlestRequest, PortletResponse portletResponse){
ModelAndView mv = new ModelAndView();
TableDAO dao = new TableDAO();
List<String> headersList = dao.getHeaders();
TableView tableView = new TableView();
tableView.setTableHeaders(headersList);
mv.addObject("tableView",tableView);
mv.setView("tableView");
return mv;
}
My jsp:
我的jsp:
<table>
<c:forEach var = "listValue" items = "${tableView.tableHeaders}">
<tr>
<%for(int i = 0;i<5;i++){ %>
<td>
<%=${listValue.get(i)} %>
</td>
<%} %>
</tr>
</c:forEach>
</table>
Someone plz help me on this.
有人请帮助我。
回答by Louise Miller
Why are you looking over the list of headers twice? You dont need that scriptlet code.
为什么要查看标题列表两次?您不需要那个scriptlet 代码。
Since tableView.tableHeaders returns a list of strings, you just need :
由于 tableView.tableHeaders 返回一个字符串列表,您只需要:
<table>
<tr>
<c:forEach var = "listValue" items = "${tableView.tableHeaders}">
<td>
<c:out value="${listValue}"/>
</td>
</c:forEach>
</tr>
</table>