首页 > 编程 > JSP > 正文

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

2024-09-05 00:21:53
字体:
来源:转载
供稿:网友
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 borderColor="darkgreen" cellSpacing="0">
    <logic:notEmpty property="userlist">
    <tr nowrap="nowrap">
    <td nowrap><b>用户名</b></td>
    <td nowrap><b>角色</b></td>
    <td ><b>姓名</b></td>
    <td ><b>电话</b></td>
    <td ><b>电子邮件</b></td>
    <td ><b>动作</b></td>
    </tr>
    <logic:iterate indexId="index" property="userlist">
    <tr>
      <td noWrap ><bean:write property="username"/></td>
      <td noWrap ><bean:write property="role"/></td>
      <td noWrap ><bean:write property="name"/></td>
      <td noWrap ><bean:write property="tel"/></td>
      <td noWrap ><bean:write property="email"/></td>
      <td nowrap sryle="width:273px" >
      <a href="javascript:submitSid(document.fview,'<bean:write property="username"/>')">查看</a>
      <font >||</font>
      <a href="javascript:submitSid(document.fview,'<bean:write property="username"/>')">更新</a>
      <font >||</font>
      <a href="javascript:if (confirm('删除此用户么?')){ submitSid(document.fview,'<bean:write 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  borderColor="darkgreen" cellSpacing="0">
    <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>
    <logic:iterate indexId="index" >
    <tr>
      <td   ><bean:write property="username"/></td>
      <td   ><bean:write property="role"/></td>
      <td  ><bean:write property="name"/></td>
      <td   ><bean:write property="tel"/></td>
      <td ><bean:write property="email"/></td>
      <td  >
      <a href="javascript:submitSid(document.fview,'<bean:write property="username"/>')">查看</a>
      <font >||</font>
      <a href="javascript:submitSid(document.fview,'<bean:write property="username"/>')">更新</a>
      <font >||</font>
      <a href="javascript:if (confirm('删除此用户么?')){ submitSid(document.fview,'<bean:write property="username"/>')}">删除</a>
      </td></tr>
      </logic:iterate>
      </logic:present>
      </table>

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表