在 JSP 中呈现列表
我在尝试在 JSP 中输出数组时遇到问题:
@RequestMapping(value="/searchResult.do", method = RequestMethod.POST)
ModelAndView search(HttpServletRequest request, HttpServletResponse response) throws SQLException
{
String keyword = request.getParameter("keyword");
String within = request.getParameter("witin");
String status = request.getParameter("status");
String query="select id,title,description,story_type,uploaded_date,popularity from story where status='"+status+"' and title like '"+'%'+keyword+'%'+"'";
stmt= (Statement) DBManager.getMyConnection();
rs = stmt.executeQuery(query);
Map map = new HashMap();
List wordList = new ArrayList();
if(keyword!=null){
while (rs.next()) {
String id = rs.getString("id");
String title = rs.getString("title");
String description = rs.getString("description");
String parent = rs.getString("uploaded_date");
String type = rs.getString("story_type");
String pop = rs.getString("popularity");
wordList.add(id);
wordList.add(title);
wordList.add(type);
wordList.add(description);
wordList.add(parent);
wordList.add(pop);
map.put("wordList", wordList);
}
return new ModelAndView("searchResult",map);
}
return new ModelAndView("search");
}
在 JSP 中我使用 ${wordList}
打印
[20,印度赢得世界杯,体育,这 是我的杯子,2011-03-26, 2, 27, 印度 赢得世界杯, 体育, xyz, 2011-04-08, 2]
...这是一个数组中的两条记录。
如何在 JSP 中的单独行中打印每个项目?
I'm having a problem trying to output an array in a JSP:
@RequestMapping(value="/searchResult.do", method = RequestMethod.POST)
ModelAndView search(HttpServletRequest request, HttpServletResponse response) throws SQLException
{
String keyword = request.getParameter("keyword");
String within = request.getParameter("witin");
String status = request.getParameter("status");
String query="select id,title,description,story_type,uploaded_date,popularity from story where status='"+status+"' and title like '"+'%'+keyword+'%'+"'";
stmt= (Statement) DBManager.getMyConnection();
rs = stmt.executeQuery(query);
Map map = new HashMap();
List wordList = new ArrayList();
if(keyword!=null){
while (rs.next()) {
String id = rs.getString("id");
String title = rs.getString("title");
String description = rs.getString("description");
String parent = rs.getString("uploaded_date");
String type = rs.getString("story_type");
String pop = rs.getString("popularity");
wordList.add(id);
wordList.add(title);
wordList.add(type);
wordList.add(description);
wordList.add(parent);
wordList.add(pop);
map.put("wordList", wordList);
}
return new ModelAndView("searchResult",map);
}
return new ModelAndView("search");
}
in the JSP I use ${wordList}
which prints
[20, india won worldcup, sports, this
is my cup, 2011-03-26, 2, 27, india
won worldcup, sports, xyz, 2011-04-08,
2]
...these are two records in one array.
How can I print each item in a separate row in a JSP?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
名为 wordList 的变量是一个集合。要显示集合的元素,可以使用 forEach。
此处有一个很好的示例以及一个较短的示例< a href="http://www.kodejava.org/examples/526.html" rel="nofollow">此处。
Your variable called wordList is a collection. To display elements of a collection you can use forEach.
There is a good example of this available here and a shorter example here.