【问题标题】:JSP MySQL resultsJSP MySQL 结果
【发布时间】:2013-05-29 13:36:26
【问题描述】:

我已经编写了这个函数来从 MySQL 数据库中获取项目列表到 JSP 中:

public List getBookList() throws SQLException
   {
      List  BookList = new ArrayList();


      ResultSet results = statement.executeQuery("SELECT * FROM book" );



      while ( results.next() ) {
         BookBean  view  = new BookBean();

        view.setID(results.getString( 1 ));
        view.setName(results.getString( 2 ));
        view.setDescription(results.getString( 3));
        view.setCatID(results.getString( 4));
        view.setUID(results.getString(5 ));
        view.setDateAdded(results.getString( 6 ));
        view.setPicThumb(results.getString( 7 ));
        view.setPicLarge(results.getString( 8 ));




         BookList.add(view);
      }

      return BookList;
   }

如何调用此方法在 .jsp 页面上呈现我的结果集,有没有更好的方法来重写此方法??

【问题讨论】:

标签: java mysql database jsp


【解决方案1】:

是的,方法很好。您需要的只是将此 ArrayList 对象通过 request.setAttribute 带到您的 jsp 中,或者从 jsp scriplet <% somewhere on the top of the page... %> 调用此方法,然后使用 jstl 呈现您的 ArrayList。

这里是 ArrayList<HashMap<String,Object>> 在 jsp 中通过 jstl 渲染的示例

click here for example

谷歌一下,你会发现很多例子。

【讨论】:

    【解决方案2】:

    使用 MVC 设计模式,您可以将值设置为 model,然后将模型对象设置为在您的 controller 中请求。然后使用

    类似的东西

    request.setAttrbute("key to access the model",your model Object);
    ServletContext context= getServletContext();
    RequestDispatcher rd= context.getRequestDispatcher("path_to_your_jsp_page");
    rd.forward(request, response);
    

    然后使用ELJSTL 读取JSP 中的值 方法有很多种,以上就是其中一种方法!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-11-04
      • 2014-02-14
      • 1970-01-01
      • 1970-01-01
      • 2012-08-12
      • 2018-06-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多