现附上代码,日志查看到HQL语句已转换成SQL语句。而且正常调用了。但是在JSP页中没有数据出现。请高手解决。。在线等。。谢谢!pagination.javapackage com.paging;import java.util.ArrayList;
import java.util.List;import javax.servlet.http.HttpServletRequest;import org.apache.struts2.ServletActionContext;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;import com.opensymphony.xwork2.ActionSupport;public class pagination extends ActionSupport { private Session session = null;
private List list;
private int totalPage;// 页面总数
private int totalCount;// 用户总数
private int pageSize;// 每页显示数据量
private int currentPage;// 当前页码数 public pagination() {
// 找到hibernate配置文件
Configuration config = new Configuration().configure();
// 从配置文件中取出SessionFactory
SessionFactory factory = config.buildSessionFactory();
// 从SessionFactory中取session
this.session = factory.openSession();
} /*
 * 功 能:获取总记录数 参 数:持久化对象名 返回值:int
 */
public int getUserTotalCount(String tablename) {
Query q = session.createQuery("select count(*) from " + tablename);
List ll = q.list();
Integer a = (Integer) ll.get(0);
return a.intValue();
} /*
 * 功 能:获取总页数 参 数:pageSize(每页显示数据量) 返回值:int
 */
public int getTotalPage(int pageSize) {
totalCount = getUserTotalCount("Subject");
totalPage = ((totalCount + pageSize) - 1) / pageSize;
return totalPage;
} /*
 * 功 能:分页显示数据 参 数:hql , pageNum-第几页 , pageSize-每页条数 返回值:List
 */
public List pagination(String hql, int pageNum, int pageSize) {
int numBegin = 0;
if (pageNum < 1) {
numBegin = 0;
} else {
numBegin = (pageNum - 1) * pageSize;
}
List retList = new ArrayList();
try {
Query query = session.createQuery(hql);
query.setFirstResult(numBegin);
query.setMaxResults(pageSize);
retList = query.list();
return retList;
} catch (HibernateException e) {
e.printStackTrace();
} finally {
try {
session.close();
} catch (HibernateException e) {
e.printStackTrace();
}
}
return retList;
} public String pp(String hql){
pageSize = 2;
HttpServletRequest request = ServletActionContext.getRequest();
String pageString = request.getParameter("page");
if (pageString == null || pageString.length() == 0) {
pageString = "1";
}
currentPage = 0;
try {
currentPage = Integer.parseInt(pageString);
} catch (Exception e) {
e.printStackTrace();
}
if (currentPage == 0) {
currentPage = 1;
}
totalPage = getTotalPage(pageSize);
totalCount = getTotalCount();
currentPage = currentPage;
list = this.pagination(hql, currentPage, pageSize);
//System.out.println(currentPage);
//System.out.println(pageSize);
return SUCCESS;
}
//省略set 和 get方法 
......}jsp 页面
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
 <base href="<%=basePath%>">
<HEAD>
<BODY bgColor=#f2f9fb leftMargin=0 topMargin=0>
<DIV align=center>
<CENTER>
<TABLE 
style="BORDER-TOP-WIDTH: 0px; BORDER-LEFT-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; WIDTH: 100%; BORDER-RIGHT-WIDTH: 0px" 
cellSpacing=0 cellPadding=5 width="100%" border=0>
 
  <TBODY>
  <TR>
    <TD align=middle colSpan=6>以下是按关键字<FONT color=red> 所有题目 
      </FONT>的查询结果&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <a href="<s:url action="ListSubject"/>">查找</a> <INPUT 
      name=keyword> 题目 <INPUT type=submit value=查找 name=B1>
      </TD>
  </TR>
  <TR>
    <TD align=left colSpan=6>温馨提示:<font color="#FF0000">类型格式:</font>1-毕业设计 2-毕业论文 ; <font color="#FF0000">题目来源:</font>1-教师科研课题 2-自选课题 3-实习单位课题 ; <font color="#FF0000">课题类型:</font>1-理论研究 2-实验研究 3-工程设计 4-工程技术研究 5-软件开发 </TD>
  </TR>
  <TR>
    <TD width=113 background=images/dtitle_9_1.gif height=24>&nbsp;</TD>
    <TD align=middle width=1103 background=images/b2_1.gif 
    height=24>&nbsp;</TD></TR>
  <TR>
    <TD align=middle colSpan=6 height=22>
      <TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
        <TBODY>
        <TR>
          <TD colSpan=18 height=2></TD></TR>
<s:iterator id="ls" value="list" status="st">
<!-- 根据当前集合元素的索引是否为奇数来决定是否使用CSS样式 -->
        <TR <s:if test="#st.odd">style="background-color:#e9f8f0"</s:if><s:else>style="background-color:#ffffff"</s:else>>
          <tr><TD align=middle height=24>&nbsp;</TD>
          <TD align=middle height=24><s:property value="%{id}"/></TD>
          <TD align=middle height=24>&nbsp;</TD>
          <TD><s:property value="%{subTitle}"/><s:property value="%{id}"/></TD>
          <TD>&nbsp;</TD>
          <TD><s:property value="%{subType1}"/></TD>
          <TD>&nbsp;</TD>
          <TD><s:property value="%{subFrom}"/></TD>
          <TD>&nbsp;</TD>
          <TD><s:property value="%{subType2}"/></TD>
          <TD>&nbsp;</TD>
          <TD><s:property value="%{techId}"/></TD>
          <TD></TD>
          <TD align=middle><s:property value="%{subState}"/></TD>
          <TD></TD>
          <TD align=middle><A 
            href="#">编辑</A> | <A 
            href="#">删除</A></TD>
          <TD></TD>
          <TD width="29"></TD></TR>
  </s:iterator>
</TBODY></TABLE></TD></TR>
  <TR>
    <TD align=middle colSpan=6>
      第${currentPage}页/共${totalPage}页
    <a href="/bsxt/show.action">首页</a>
    <s:if test="currentPage>1">
    [<a href="/bsxt/show.action?page=${currentPage-1}">上一页</a>]    </s:if>
    <s:if test="currentPage<=1">
    [上一页]    </s:if>
    <s:if test="currentPage<totalPage">
    [<a href="/bsxt/show.action?page=${currentPage+1}">下一页</a>]    </s:if>
    <s:if test="currentPage>=totalPage">
    [下一页]    </s:if>
    <a href="/bsxt/show.action?page=${totalPage}">尾页</a>   </TD></TR></TBODY></TABLE>
</CENTER></DIV></BODY></HTML>struts.xml <action name="show" class="com.operate.SubjectOperate" method="ListSubject">
<result>/subject_list.jsp</result>
</action>
SubjectOperate.javapublic class SubjectOperate extends ActionSupport{
private static final String SUCCESS = null;
private Session session=null;

public SubjectOperate()
{
//找到hibernate配置文件
Configuration config = new Configuration().configure();
//从配置文件中取出SessionFactory
SessionFactory factory = config.buildSessionFactory();
//从SessionFactory中取session
this.session = factory.openSession();
}
/*
 * 函数说明:查询题目
 * 参数说明:HQL语句
 * 返回值:String
 */
public String ListSubject() throws Exception{
pagination pa = new pagination();
return pa.pp("from com.hibernate.Subject");
//System.out.print(pa.pengch_query("from com.hibernate.Subject"));
//return pa.execute();
}

}

解决方案 »

  1.   

    利用Hibernate 进行分页我也是最近刚学完,完全没有楼主那么麻烦,下面是对SQL数据库的一个job表的分页,很简单
    import java.util.List;
    import org.apache.commons.logging.Log;
    import org.apache.commons.logging.LogFactory;
    import org.hibernate.LockMode;
    import org.hibernate.Query;
    import org.hibernate.criterion.Example;/**
     * Data access object (DAO) for domain model class Jobs.
     * @see com.cheng.bean.Jobs
     * @author MyEclipse - Hibernate Tools
     */
    public class JobsDAO extends BaseHibernateDAO { //每页显示的信息数
    public  final int PAGE_SIZE=4;

    /**
     * 实现全部查询
     */
    public List findAll(){
    return getSession().createQuery("from Jobs").list(); 
    }

    /**获得总页数的方法
     * @param transientInstance
     */

    public int getTotalPage(){
    return (findAll().size()%PAGE_SIZE)==0 ? findAll().size()/PAGE_SIZE : (findAll().size()/PAGE_SIZE+1);
    }

    /**
     * 实现具体分页的操作
     * @param transientInstance
     */
    public List fenYe(int currPage){
    //判断上一页就是第一页
    if(currPage<1){
    currPage=1;
    }
    //判断最后一页
    if(currPage>getTotalPage()){
    currPage=getTotalPage();
    }
    //实现分页的操作
    return  getSession().createQuery("from Jobs").setFirstResult((currPage-1)*PAGE_SIZE)
    .setMaxResults(5).list();

    }