A concrete instance of ArrayList displayed to JSP pages in Action

  • 2020-06-19 11:32:12
  • OfStack

1. The ArrayList object obtained in UserAction fills in UserForm, and the jsp page gets the initial value of UserForm.
Part of the UserAction code:


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");
 }

Part of the UsrForm code:

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

JSP page code:

    <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> The user name </b></td>
    <td style="width:80px;height:16px" nowrap><b> role </b></td>
    <td style="width:84px;height:16px" ><b> The name </b></td>
    <td style="width:88px;height:16px" ><b> The phone </b></td>
    <td style="width:73px;height:16px" ><b> E-mail </b></td>
    <td style="width:273px;height:16px" ><b> action </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"/>')"> To view </a>
      <font >||</font>
      <a href="javascript:submitSid(document.fview,'<bean:write name="user" property="username"/>')"> update </a>
      <font >||</font>
      <a href="javascript:if (confirm(' Delete this user? ')){ submitSid(document.fview,'<bean:write name="user" property="username"/>')}"> delete </a>
      </td></tr>
      </logic:iterate>
      </logic:notEmpty>
      </table>

2. After the ArrayList object is obtained from UserAction, the ArrayList object is stored in request, and the JSP page gets the ArrayList object.
UserAction part code:

 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 part code:

<table id="id1"  borderColor="darkgreen" cellSpacing="0" border="1">
    <tr >
    <td  ><b> The user name </b></td>
    <td  ><b> role </b></td>
    <td  ><b> The name </b></td>
    <td  ><b> The phone </b></td>
    <td ><b> E-mail </b></td>
    <td ><b> action </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"/>')"> To view </a>
      <font >||</font>
      <a href="javascript:submitSid(document.fview,'<bean:write name="user" property="username"/>')"> update </a>
      <font >||</font>
      <a href="javascript:if (confirm(' Delete this user? ')){ submitSid(document.fview,'<bean:write name="user" property="username"/>')}"> delete </a>
      </td></tr>
      </logic:iterate>
      </logic:present>
      </table>


Related articles: