在 JSP 中呈现列表

发布于 2024-11-04 00:53:24 字数 1631 浏览 1 评论 0原文

我在尝试在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

伤感在游骋 2024-11-11 00:53:24

名为 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文