==========================DAO==============
package com.fenye.dao;import java.util.List;import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;import com.fenye.bean.*;
import com.hua.fenye.sessionfactory.HibernateSessionFactory;public class getDatao { Query q = null; Integer DataCount = 0; List list = null; public Integer getCount() throws Exception { Session session = null; Transaction ts = null;
try {
String arg0 = "select count(*) from com.fenye.bean.Jobs";
session = HibernateSessionFactory.getSession();
System.out.println("SESSION IS GETSESSION!");
ts = session.beginTransaction();
System.out.println("tRANSACTION IS GETSESSION!");
q = session.createQuery(arg0);
DataCount = (Integer) q.uniqueResult();
ts.commit(); } catch (Exception e) {
ts.rollback();
throw e; }
return DataCount;
} public List showData(Integer pageSize, Integer thepage) throws Exception {
/*返回每页要显示的记录数*/

Session session = null;
Transaction ts = null;
try { String arg0 = " from com.fenye.bean.Jobs";
session = HibernateSessionFactory.getSession();
System.out.println("+++++++++++++++++++++++");
ts = session.beginTransaction();
q = session.createQuery(arg0);

q.setFirstResult(pageSize * (thepage - 1));/*设置从那里开始显示*/
q.setMaxResults(pageSize);/* 设置每页显示的大小*/ list = q.list();
ts.commit(); } catch (Exception e) {
ts.rollback(); throw e;
}
return list; }}

解决方案 »

  1.   

    =====================ACTION=========================
    public ActionForward execute(ActionMapping mapping, ActionForm form,
    HttpServletRequest request, HttpServletResponse response) {
    // TODO Auto-generated method stub
    getDatao data = new getDatao();
    int start = Integer.parseInt(request.getParameter("start")); int pageSize = 4;
    int count = 0;
    int first = 1;
    int last = 0;
    List list = new ArrayList();
    request.setAttribute("first", new Integer(first));
    /*----------------------------------*/ try {
    count = data.getCount();
    if (count % pageSize == 0) { last = count / pageSize;/* 最后一页的页号也就是总共的页数 */ } else {
    last = count / pageSize + 1;/* 假如除不尽,就多分一页 */ }
    request.setAttribute("last", new Integer(last)); /*------------------------------------*/
    list = data.showData(pageSize, start);/* 获取每业将要显示的数据 */
    request.setAttribute("list", list); /*-------------------------------------*/
    int previous = start - 1;/* 上一页等与当前页减一 */
    if (previous > 0) { request.setAttribute("previous", new Integer(previous)); } else { request.setAttribute("previous", new Integer(1));/* 上一页的页号如果不大与零,那么就让她是第一页 */ }
    /*--------------------------------------*/
    int next = start + 1; /*下一页就是当前页的页号在加一*/
    if (list.size() >= 4) { request.setAttribute("next", new Integer(next)); } else {
    next = start;// 为什么这么写?
    request.setAttribute("next", new Integer(next)); } /*---------------------------------------------------*/ } catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } return mapping.findForward("ok"); }
      

  2.   

    =================================JSP==================
    <body>
    <table align="center" border="1" bordercolor="lightgreen" width="60%">
    <tr>
    <td>
    ID
    </td>
    <td>
    DESC
    </td>
    <td>
    MAX
    </td>
    <td>
    MIN
    </td> </tr>
    <logic:iterate id="myjobs" name="list">
    <tr>
    <td>
    ${myjobs.jobId}
    </td>
    <td>
    ${myjobs.jobDesc}
    </td>
    <td>
    ${myjobs.minLvl }
    </td>
    <td>
    ${myjobs.maxLvl }
    </td>
    </tr>
    </logic:iterate> <tr>
    <td>
    <html:link action="/managerData.do" paramId="start"
    paramName="first">第一页</html:link>
    </td>
    <td>
    <html:link action="/managerData.do" paramId="start"
    paramName="next">下一页</html:link>
    </td>
    <td>
    <html:link action="/managerData.do" paramId="start"
    paramName="previous">上一页</html:link>
    </td>
    <td>
    <html:link action="/managerData.do" paramId="start"
    paramName="last">最后一页</html:link>
    </td>
    </tr>
    </table>
    </body>
    </html:html>