Action中ArrayList顯示到JSP頁面的具體執行個體_JSP編程

來源:互聯網
上載者:User

一、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;width: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="width:80px;height:16px" nowrap><b>使用者名稱</b></td>
    <td style="width:80px;height:16px" nowrap><b>角色</b></td>
    <td style="width:84px;height:16px" ><b>姓名</b></td>
    <td style="width:88px;height:16px" ><b>電話</b></td>
    <td style="width:73px;height:16px" ><b>電子郵件</b></td>
    <td style="width:273px;height:16px" ><b>動作</b></td>
    </tr>
    <logic:iterate indexId="index" id="user" name="userForm" property="userlist">
    <tr>
      <td noWrap style="width:80px" ><bean:write name="user" property="username"/></td>
      <td noWrap style="width:80px" ><bean:write name="user" property="role"/></td>
      <td noWrap style="width:80px" ><bean:write name="user" property="name"/></td>
      <td noWrap style="width:80px" ><bean:write name="user" property="tel"/></td>
      <td noWrap style="width:80px" ><bean:write name="user" property="email"/></td>
      <td nowrap sryle="width: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>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.