火星文 技术研习社

Noname Cat, Keep Thinking
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

struts加hibernate中分页显示问题的解决

Posted on 2006-04-23 18:49  剑廿三  阅读(170)  评论(0)    收藏  举报

struts加hibernate中分页显示问题的解决

hibernate 的查询语句"from TbFitment where 1=1 and ......"
此查询语句查询出来的结果List中的记录。在显示页面,struts的<logic:iterate>
标签可以把结果List的中的object[]自动识别为TbFitment类 javaBean。
可以使用<td id="bgtitle"> <bean:write name="arraylist" property="inputDeptname"/> </td>
的方法循环显示出来;
但是如果查询语句仅仅查询 TbFitment表中一部分字段,那么struts的<logic: iterate>将无法
将结果list中的Object[]转化为 TbFitment类 javaBean
显示页面使用<td id="bgtitle"> <bean:write name="arraylist" property="inputDeptname"/> </td>
将报 找不到 getter 方法的错误。
为解决此问题,我们在hibernate下构造一个新的类(PO)。从TbFitment类中取一部分字段
(按照查询出来的字段决定)。HQL改变为:"form TbFitmentSearch" where 1=1 and ......"
在显示页面,struts 会自动构造 TbFitmentSearch javaBean 循环显示。

struts ation 类 代码:
package com.sunco.fitment.request.action;

import java.util.List;

import javax.servlet.http.*;
import java.util.List;

import com.sunco.base.action.BaseAction;
import com.sunco.fitment.hibernate.TbFitment;
import com.sunco.fitment.hibernate.dao.*;
import com.sunco.fitment.request.form.*;
import org.apache.struts.action.*;

import com.sunco.base.common.*;

import com.sunco.base.common.HttpServletBean;
import com.sunco.base.common.UserFace;
import net.sf.hibernate.Query;
import net.sf.hibernate.*;
import net.sf.hibernate.cfg.*;
import java.util.Vector;
import java.util.Iterator;
import java.util.*;


public class RequestSearchListAction
    extends BaseAction {

//    /**
//     * 此处不允许覆写该类 public ActionForward execute()
//     * @param actionMapping ActionMapping
//     * @param actionForm ActionForm
//     * @param resp HttpServletRequest
//     * @param req HttpServletResponse
//     * @return ActionForward
//     */
//    public ActionForward execute(ActionMapping actionMapping,
//                                 ActionForm actionForm, HttpServletRequest resp,
//                                 HttpServletResponse req) {
//    }

  /**
   *
   * @param actionMapping ActionMapping
   * @param actionForm ActionForm
   * @param req HttpServletRequest
   * @param resp HttpServletResponse
   * @throws Exception
   * @return ActionForward
   */
  public ActionForward exec(ActionMapping actionMapping, ActionForm actionForm
                            , HttpServletRequest req,
                            HttpServletResponse resp, HttpServletBean hsBean)

      throws Exception {

    RequestSearchForm requestSearchForm = (RequestSearchForm) actionForm;
    String fitid = requestSearchForm.getFitid();
    String FK_REGIONNAMEALL = "&"+requestSearchForm.getDistrict1()
                             +"&"+requestSearchForm.getDistrict2()
                             +"&"+requestSearchForm.getDistrict3();
    String address = requestSearchForm.getAddress();
    String begindate = requestSearchForm.getBegindate();
    String enddate = requestSearchForm.getEnddate();
    String reqstatus = requestSearchForm.getReqstatus();
    String gname = requestSearchForm.getGname();
    String gtelephone = requestSearchForm.getGtelephone();
    String clientsource = requestSearchForm.getClientsource();
    String vdept = requestSearchForm.getVdept();

    if (clientsource.equals("请选择"))
      clientsource = "";
    if (reqstatus.equals("全部"))
      reqstatus = "";

    file://查询语句 查询条件大原则:※不包括撤单和成交的 ※下次回访日期未过期的 ---------未设置过滤
  String HQLStr = "from TbFitmentSearch as tf";
//    String HQLStr = "select tf.ifAssign ,tf.reqCode ,tf.status,tf.manageDeptName ,"
//                          +"tf.inputDeptname ,tf.inputUser ,tf.gname ,tf.id ,tf.gsex "
//                          +",tf.gtelephone ,tf.address ,tf.inputDate "
//                          +",tf.fkRegionnameall ,tf.firstDate "
//                          +",tf.ifSpend ,tf.spdeptName "
//                          +" from TbFitment as tf";


    file://查询条件
    String qrycondition="";
    if (fitid.length()>0)
      qrycondition += " and id="+fitid;
    else{
      if (address.length() > 0)
        qrycondition += " and address like '" + address + "%'";
      if (begindate.length() > 0)
        qrycondition += " and inputDate>=to_date'" + begindate +
            " 00:00:00','YYYY-MM-DD HH24:MI:SS')";
      if (enddate.length() > 0)
        qrycondition += " and inputDate<=to_date'" + enddate +
            " 23:59:59','YYYY-MM-DD HH24:MI:SS')";
      if (reqstatus.length() > 0)
        qrycondition += " and status='" + reqstatus + "'";
      if (gname.length() > 0)
        qrycondition += " and gname like '" + gname + "%'";
      if (gtelephone.length() > 0)
        qrycondition += " and gtelephone like '" + gtelephone + "%'";
    }


    UserFace uf = new UserFace(req);
    uf.setOnePage(15);
    uf.url="/requests/RequestSearchListAction.do";
    List ltFitmentSearch = null;
    int record = 0;
    record = ((Integer)hsBean.dbSession.iterate("select count(*) from TbFitmentSearch tf where 1=1"+qrycondition).next()).intValue();
    uf.PageNum= Math.abs(record/uf.getOnePage());
     Query q = hsBean.dbSession.createQuery(HQLStr);
//    q.setFirstResult(uf.getOnePage()*(uf.CurPage-1));//起始记录的位置
    q.setFirstResult(uf.getStartRecord());
    q.setMaxResults(uf.getOnePage());//一页显示的数量
    ltFitmentSearch = q.list();

    req.setAttribute("userface",uf);
    req.setAttribute("ltFitmentSearch",ltFitmentSearch);

    return actionMapping.findForward("requestSearchOK");
  }

}

分页显示 jsp 页面代码:
<%@ include file="../common/include_taglibs.jsp" %>
<%@ include file="../common/include_charset.jsp" %>
<link href="../css/eall2002.css" rel="stylesheet" type="text/css">
<html:html>
<head>
<title>
billStoreList
</title>
</head>
<body bgcolor="#ffffff">
  <h1>
装修需求查询·结果
</h1>
<bean:write name="userface" filter="false" property="content"/>
<table id="bgtable" border='0' cellspacing='1' cellpadding='0'>

<logic:iterate id="arraylist" name="ltFitmentSearch" indexId="ind">
    <tr>
        <td id="bgtitle"> <bean:write name="arraylist" property="inputDeptname"/> </td>
     <td id="bgtitle"> <bean:write name="arraylist" property="inputUser"/> </td>
         <td id="bgtitle"> <bean:write name="arraylist" property="reqCode"/> </td>
         <td id="bgtitle"> <bean:write name="arraylist" property="gname"/> </td>
         <td id="bgtitle"> <bean:write name="arraylist" property="gsex"/> </td>
         <td id="bgtitle"> <bean:write name="arraylist" property="gtelephone"/> </td>
         <td id="bgtitle"> <bean:write name="arraylist" property="fkRegionnameall"/> </td>
         <td id="bgtitle"> <bean:write name="arraylist" property="address"/> </td>
         <td id="bgtitle"> <bean:write name="arraylist" property="status"/> </td>
         <td id="bgtitle"> <bean:write name="arraylist" property="manageDeptName"/> </td>
         <td id="bgtitle"> <bean:write name="arraylist" property="inputDate"/> </td>
         <td id="bgtitle"> <bean:write name="arraylist" property="firstDate"/> </td>
        <td id="bgtitle"> <html:link page="/requests/requestSearchDetailAction.do" paramId="fitmentid" paramName="arraylist" paramProperty="id">详细</html:link></td>
    </tr>
</logic:iterate>
</table>
</body>
</html:html>