Action中ArrayList显示到JSP页面的具体实例

发布时间:2022-04-17 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了Action中ArrayList显示到JSP页面的具体实例脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

一、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>

脚本宝典总结

以上是脚本宝典为你收集整理的Action中ArrayList显示到JSP页面的具体实例全部内容,希望文章能够帮你解决Action中ArrayList显示到JSP页面的具体实例所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。