• Action中ArrayList显示到JSP页面


    UserAciton中通过hibernate查询到的数据保存到ArrayList中,JSP页面希望表格输出ArrayList对象,有两种方法:

    一、UserAction中获取到的ArrayList对象填充到UserForm中,jsp页面获取UserForm的初始值。

    UserAction的部分代码:

     private ActionForward executeManageAction(ActionMapping mapping, ActionForm form,
       HttpServletRequest request, HttpServletResponse response) {
      UserForm userForm = (UserForm)form;
      ArrayList userlist = new ArrayList();
      SessionFactory sf= new Configuration().configure().buildSessionFactory();
      Session session=sf.openSession();
      Transaction tx=session.beginTransaction();
      String sqlQuery="from User";
      Query lQuery=session.createQuery(sqlQuery);
      userlist=(ArrayList)lQuery.list();
      tx.commit();
      session.close();
      userForm.setUserlist(userlist);
      return mapping.findForward("main_user");
     }

    UsrForm的部分代码:

    private ArrayList userlist;

     public ArrayList getUserlist(){
      return userlist;
     }
     public void setUserlist(ArrayList userlist){
      this.userlist=userlist;
     }

    JSP页面代码:

        <table id="id1" style="border-right: darkgreen 1px solid;border-top:darkgreen 1px solid;border-left: darkgreen 1px solid;100%;
        border-bottom;darkgreen 1px solid;border-collapse:collapse" borderColor="darkgreen" cellSpacing="0" border="1">
       
        <logic:notEmpty name="userForm" property="userlist">
        <tr nowrap="nowrap">
        <td style="80px;height:16px" nowrap><b>用户名</b></td>
        <td style="80px;height:16px" nowrap><b>角色</b></td>
        <td style="84px;height:16px" ><b>姓名</b></td>
        <td style="88px;height:16px" ><b>电话</b></td>
        <td style="73px;height:16px" ><b>电子邮件</b></td>
        <td style="273px;height:16px" ><b>动作</b></td>
        </tr>
        <logic:iterate indexId="index" id="user" name="userForm" property="userlist">
        <tr>
          <td noWrap style="80px" ><bean:write name="user" property="username"/></td>
          <td noWrap style="80px" ><bean:write name="user" property="role"/></td>
          <td noWrap style="80px" ><bean:write name="user" property="name"/></td>
          <td noWrap style="80px" ><bean:write name="user" property="tel"/></td>
          <td noWrap style="80px" ><bean:write name="user" property="email"/></td>
          <td nowrap sryle="273px" >
          <a href="javascript:submitSid(document.fview,'<bean:write name="user" property="username"/>')">查看</a>
          <font >||</font>
          <a href="javascript:submitSid(document.fview,'<bean:write name="user" property="username"/>')">更新</a>
          <font >||</font>
          <a href="javascript:if (confirm('删除此用户么?')){ submitSid(document.fview,'<bean:write name="user" property="username"/>')}">删除</a>
          </td></tr>
          </logic:iterate>
          </logic:notEmpty>
          </table>

    二、UserAction中获取到数据ArrayList对象后,把ArrayList对象存在request中,JSP页面在获取到ArrayList对象。

    UserAction部分代码:

     private ActionForward executeManageAction(ActionMapping mapping, ActionForm form,
       HttpServletRequest request, HttpServletResponse response) {
      UserForm userForm = (UserForm)form;
      ArrayList userlist = new ArrayList();
      SessionFactory sf= new Configuration().configure().buildSessionFactory();
      Session session=sf.openSession();
      Transaction tx=session.beginTransaction();
      String sqlQuery="from User";
      Query lQuery=session.createQuery(sqlQuery);
      userlist=(ArrayList)lQuery.list();
      tx.commit();
      session.close();
      request.setAttribute("userlist", userlist);
      return mapping.findForward("main_user");
     }

    JSP部分代码:

     <table id="id1"  borderColor="darkgreen" cellSpacing="0" border="1">
       
       
        <tr >
        <td  ><b>用户名</b></td>
        <td  ><b>角色</b></td>
        <td  ><b>姓名</b></td>
        <td  ><b>电话</b></td>
        <td ><b>电子邮件</b></td>
        <td ><b>动作</b></td>
        </tr>
         <logic:present name="userlist">
        <logic:iterate indexId="index" id="user" name="userlist" >
        <tr>
          <td   ><bean:write name="user" property="username"/></td>
          <td   ><bean:write name="user" property="role"/></td>
          <td  ><bean:write name="user" property="name"/></td>
          <td   ><bean:write name="user" property="tel"/></td>
          <td ><bean:write name="user" property="email"/></td>
          <td  >
          <a href="javascript:submitSid(document.fview,'<bean:write name="user" property="username"/>')">查看</a>
          <font >||</font>
          <a href="javascript:submitSid(document.fview,'<bean:write name="user" property="username"/>')">更新</a>
          <font >||</font>
          <a href="javascript:if (confirm('删除此用户么?')){ submitSid(document.fview,'<bean:write name="user" property="username"/>')}">删除</a>
          </td></tr>
          </logic:iterate>
          </logic:present>
          </table>

  • 相关阅读:
    乐观锁悲观锁及其使用场景
    inner join, left join, right join的作用是什么
    主键和唯一索引的区别
    在排序数组中查找元素的第一个和最后一个位置
    寻找旋转排序数组中的最小值
    [模板] 最小费用最大流
    CF878E Numbers on the blackboard
    CF1286F Harry The Potter
    CF1368H1 Breadboard Capacity
    CF1442E Black, White and Grey Tree
  • 原文地址:https://www.cnblogs.com/htys/p/3296585.html
Copyright © 2020-2023  润新知