<% int totalPage = (Integer)request.getAttribute("totalPage");
        %>
                </tr>
                  <s:url id="url_pre" value="show.action">
                     <s:param name="pageNow" value="pageNow-1"></s:param>
                  </s:url>
                 <s:url id="url_next" value="show.action">
                    <s:param name="pageNow" value="pageNow+1"></s:param>
                 </s:url>  
                 <s:url id="first" value="show.action">
                    <s:param name="pageNow" value="1"></s:param>
                 </s:url> 
                  <s:url id="last" value="show.action">
                    <s:param name="pageNow" value="<%=totalPage%>"></s:param>
                 </s:url>
以上是struts2 的分页  其他功能都实现了
                      怎么才能看到最后一页???(一共页数 已经统计出来 )怎么在JSP中 把action中的totalPage 传来

解决方案 »

  1.   

    totalPage是总的页数吧
    int totalPages = 1;

    String sql = "select count(*) from tablename where XX = " + XX;
            //用上面语句计算总共有多少条记入totalRecords 

    totalPages = (totalRecords + PAGE_SIZE - 1)/PAGE_SIZE;//计算总页数
    request.setAttribute("totalPage",totalPages);
      

  2.   

    在action中搞个私有变量 
    private int totalPages;跟上get set方法在action中附上值,就可以在页面上直接用了,不需要传,struts2自动帮你传了页面上:<s:property value="totalPages" />
      

  3.   

    private int totalPages; 
    public void setTotalPages(int totalPages) {
    this.totalPages= totalPages;
    } public int getTotalPages() {
    return totalPages;
    }
     <s:property value="totalPages" />
      

  4.   

    SSH(建议通用分页标签)--->PageDemo:简单的示例:1、三个页面代码
    ==================================================================
    <1>===============index.jsp============
    <%@ page language="java" pageEncoding="UTF-8"%><%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
    <%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
    <%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
    <%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %>
    <html:html lang="true">
      <head>
        <html:base /> 
        <title>index.jsp</title>
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
      </head>
      
      <body>
        <html:form action="/score?opt=toList" method="post">
          <table border="0">
           
            <tr>
              <td colspan="2" align="center"><html:submit value="提交" /></td>
            </tr>
          </table>
        </html:form>
      </body>
    </html:html><2>===============List.jsp===============
    <%@ page language="java" pageEncoding="UTF-8"%><%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
    <%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
    <%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
    <%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %>
    <%@ taglib tagdir="/WEB-INF/tags/" prefix="my" %><html:html lang="true">
      <head>
        <html:base /> 
        <title>List.jsp</title>
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <style type="text/css">
    table{
    border-width;1px;
    border-style:solid;
    border-color:green;
    }
    td{
    border-width;1px;
    border-style:solid;
    border-color:green;
    }

    </style>
      </head>
      
      <body>
        <html:form action="/userInfo?opt=toList" method="post">
          <table border="0" width="300">
             <tr>
             <td>编号</td><td>姓名</td><td>余额</td>
             </tr>
             <logic:iterate id="user" collection="${bean.date}">
    <tr>
    <td>${user.userId }</td>
    <td>${user.userName }</td>
    <td>${user.balance }</td>
    </tr>
             </logic:iterate>
          </table>
          <my:myPage cpage="${bean.currentPage}" allNum="${bean.allNum}" allPage="${bean.allPage}" pageNum="${bean.pageNum}"/>
        </html:form>
      </body>
    </html:html><3>========ScoreList.jsp=======
    <%@ page language="java" pageEncoding="UTF-8"%><%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
    <%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
    <%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
    <%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %>
    <%@ taglib tagdir="/WEB-INF/tags/" prefix="my" %><html:html lang="true">
      <head>
        <html:base /> 
        <title>List.jsp</title>
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <style type="text/css">
    table{
    border-width;1px;
    border-style:solid;
    border-color:green;
    }
    td{
    border-width;1px;
    border-style:solid;
    border-color:green;
    }

    </style>
      </head>
      
      <body>
        <html:form action="/score?opt=toList" method="post">
          <table border="0" width="300">
             <tr>
             <td>编号</td><td>成绩</td>
             </tr>
             <logic:iterate id="ss" collection="${bean.date}">
    <tr>
    <td>${ss.cid }</td>
    <td>${ss.score }</td>
    </tr>
             </logic:iterate>
          </table>
          <my:myPage cpage="${bean.currentPage}" allNum="${bean.allNum}" allPage="${bean.allPage}" pageNum="${bean.pageNum}"/>
        </html:form>
      </body>
    </html:html>
    ==================================================================
    2/ entity 类:
    1、===========Score========
    package com.myweb.entity;/**
     * Score generated by MyEclipse Persistence Tools
     */public class Score implements java.io.Serializable { // Fields private Integer cid; private UserInfo userInfo; private Double score; // Constructors /** default constructor */
    public Score() {
    } /** full constructor */
    public Score(UserInfo userInfo, Double score) {
    this.userInfo = userInfo;
    this.score = score;
    } // Property accessors public Integer getCid() {
    return this.cid;
    } public void setCid(Integer cid) {
    this.cid = cid;
    } public UserInfo getUserInfo() {
    return this.userInfo;
    } public void setUserInfo(UserInfo userInfo) {
    this.userInfo = userInfo;
    } public Double getScore() {
    return this.score;
    } public void setScore(Double score) {
    this.score = score;
    }}
    2、=============UserInfo=================
    package com.myweb.entity;import java.util.HashSet;
    import java.util.Set;/**
     * UserInfo generated by MyEclipse Persistence Tools
     */public class UserInfo implements java.io.Serializable { // Fields private String userId; private String userName; private Integer balance; private Set scores = new HashSet(0); // Constructors /** default constructor */
    public UserInfo() {
    } /** minimal constructor */
    public UserInfo(String userName, Integer balance) {
    this.userName = userName;
    this.balance = balance;
    } /** full constructor */
    public UserInfo(String userName, Integer balance, Set scores) {
    this.userName = userName;
    this.balance = balance;
    this.scores = scores;
    } // Property accessors public String getUserId() {
    return this.userId;
    } public void setUserId(String userId) {
    this.userId = userId;
    } public String getUserName() {
    return this.userName;
    } public void setUserName(String userName) {
    this.userName = userName;
    } public Integer getBalance() {
    return this.balance;
    } public void setBalance(Integer balance) {
    this.balance = balance;
    } public Set getScores() {
    return this.scores;
    } public void setScores(Set scores) {
    this.scores = scores;
    }}=========================================================================
    1、DAo Interface 接口方法
    void ListByPage(String hql,PageBean bean); 
    2、实现接口类 DaoImpl:
    package com.myweb.dao.impl;import java.util.List;import org.hibernate.Query;import com.myweb.dao.CommonDao;
    import com.myweb.util.PageBean;
    import org.hibernate.*;
    import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
    public class CommonDaoImpl extends HibernateDaoSupport implements CommonDao { public void ListByPage(String hql, PageBean bean) {
    if (null==hql){
    return ;
    }
    Session session=this.getSession();
    Query query = session.createQuery(hql);
    int start=bean.getPageNum()*(bean.getCurrentPage()-1);//开始取数据行
    query.setFirstResult(start);
    query.setMaxResults(bean.getPageNum());//每次去多少行
    List ret = query.list();
    bean.setDate(ret);//设置每次要去的数据集合

    String queryString = "";
    if (hql.toUpperCase().indexOf("SELECT") != -1) {
    int i = query.getQueryString().toUpperCase().indexOf("FROM");
    queryString = "Select count(*) " + hql.substring(i,hql.length());
    } else {
    queryString = "Select count(*) " + hql;
    }
    // 去掉ORDER BY 的部分
    int j = queryString.toUpperCase().lastIndexOf("ORDER");
    if (j!=-1){
    queryString = queryString.substring(0, j);
    }
    Query cquery = session.createQuery(queryString);
    int allnums=Integer.parseInt(cquery.uniqueResult().toString());
    bean.setAllNum(allnums);//设置总行数
    int allpages=allnums%bean.getPageNum()==0?allnums/bean.getPageNum():allnums/bean.getPageNum()+1;
    bean.setAllPage(allpages);//设置总页数
    session.close();
    }}
    ======================================SSH===============================================
      

  5.   

    3、业务类接口------------------------ScoreBiz:void getListByPage(Score score, PageBean bean);UserInfoBiz:void getListByPage(UserInfo user, PageBean bean);4、实现业务接口类:----------=================ScoreBizImpl==========
    package com.myweb.biz.impl;import com.myweb.biz.ScoreBiz;
    import com.myweb.dao.CommonDao;
    import com.myweb.entity.Score;
    import com.myweb.util.PageBean;public class ScoreBizImpl implements ScoreBiz{ private CommonDao dao;
    public CommonDao getDao() {
    return dao;
    }
    public void setDao(CommonDao dao) {
    this.dao = dao;
    }
    public void getListByPage(Score score, PageBean bean) {
    String hql="from Score s where 1=1";
    if(score!=null){
    if(score.getScore()!=null)
    hql+=" s.score ="+score.getScore();

    }
    dao.ListByPage(hql, bean);
    }

    }
    ==============UserInfoBizImpl==================
    package com.myweb.biz.impl;import com.myweb.biz.UserInfoBiz;
    import com.myweb.dao.CommonDao;
    import com.myweb.entity.UserInfo;
    import com.myweb.util.PageBean;public class UserInfoBizImpl implements UserInfoBiz { private CommonDao dao;
    public void getListByPage(UserInfo user, PageBean bean) {
    String hql="from UserInfo u where 1=1";
    if(user!=null){
    if(user.getUserId()!=null)
    hql+=" u.userId ="+user.getUserId();
    if(user.getUserName()!=null)
    hql+="  u.userName like '%"+user.getUserName()+"%' ";
    }
    dao.ListByPage(hql, bean);
    }
    public CommonDao getDao() {
    return dao;
    }
    public void setDao(CommonDao dao) {
    this.dao = dao;
    }}
    =========================================action================================
    1、ScroeAction:---------------
    /*
     * Generated by MyEclipse Struts
     * Template path: templates/java/JavaClass.vtl
     */
    package com.myweb.struts.action;import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import org.apache.struts.action.Action;
    import org.apache.struts.action.ActionForm;
    import org.apache.struts.action.ActionForward;
    import org.apache.struts.action.ActionMapping;
    import org.apache.struts.actions.DispatchAction;import com.myweb.biz.ScoreBiz;
    import com.myweb.struts.form.BaseForm;
    import com.myweb.struts.form.ScoreForm;/** 
     * MyEclipse Struts
     * Creation date: 06-27-2009
     * 
     * XDoclet definition:
     * @struts.action path="/score" name="scoreForm" input="/form/score.jsp" scope="request" validate="true"
     */
    public class ScoreAction extends DispatchAction {

    private ScoreBiz biz;
    public ScoreBiz getBiz() {
    return biz;
    }
    public void setBiz(ScoreBiz biz) {
    this.biz = biz;
    }
    public ActionForward toList(ActionMapping mapping, ActionForm form,
    HttpServletRequest request, HttpServletResponse response) {
    ScoreForm sf = (ScoreForm) form;
    biz.getListByPage(sf.getScore(), sf.getPageBean());
    request.setAttribute("bean", sf.getPageBean());
    System.out.println("===================================");
    return mapping.findForward("list");
    }
    }2、UserInfoAction:------------
    /*
     * Generated by MyEclipse Struts
     * Template path: templates/java/JavaClass.vtl
     */
    package com.myweb.struts.action;import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import org.apache.struts.action.ActionForm;
    import org.apache.struts.action.ActionForward;
    import org.apache.struts.action.ActionMapping;
    import org.apache.struts.actions.DispatchAction;import com.myweb.biz.UserInfoBiz;
    import com.myweb.struts.form.UserInfoForm;
    import com.myweb.util.PageBean;
    public class UserInfoAction extends DispatchAction { private UserInfoBiz biz;
    public UserInfoBiz getBiz() {
    return biz;
    }
    public void setBiz(UserInfoBiz biz) {
    this.biz = biz;
    }
    public ActionForward toList(ActionMapping mapping, ActionForm form,
    HttpServletRequest request, HttpServletResponse response) {
    UserInfoForm uf = (UserInfoForm) form;
    biz.getListByPage(uf.getUserinfo(), uf.getPageBean());
    request.setAttribute("bean", uf.getPageBean());
    return mapping.findForward("list");
    }
    }=========================================Form================================
    1、baseForm:-------------
    /*
     * Generated by MyEclipse Struts
     * Template path: templates/java/JavaClass.vtl
     */
    package com.myweb.struts.form;import org.apache.struts.action.ActionForm;
    import com.myweb.util.PageBean;/** 
     * MyEclipse Struts
     * Creation date: 06-27-2009
     * 
     * XDoclet definition:
     * @struts.form name="baseForm"
     */
    public class BaseForm extends ActionForm {
    /*
     * Generated fields
     */ /** pageBean property */
    private PageBean pageBean=null; public BaseForm(){
    pageBean=new PageBean();
    }
    /*
     * Generated Methods
     */ /** 
     * Returns the pageBean.
     * @return PageBean
     */
    public PageBean getPageBean() {
    return pageBean;
    } /** 
     * Set the pageBean.
     * @param pageBean The pageBean to set
     */
    public void setPageBean(PageBean pageBean) {
    this.pageBean = pageBean;
    }
    }2、ScoreForm:--------------
    /*
     * Generated by MyEclipse Struts
     * Template path: templates/java/JavaClass.vtl
     */
    package com.myweb.struts.form;import javax.servlet.http.HttpServletRequest;
    import org.apache.struts.action.ActionErrors;
    import org.apache.struts.action.ActionForm;
    import org.apache.struts.action.ActionMapping;
    import com.myweb.entity.Score;/** 
     * MyEclipse Struts
     * Creation date: 06-27-2009
     * 
     * XDoclet definition:
     * @struts.form name="scoreForm"
     */
    public class ScoreForm extends BaseForm {
    /*
     * Generated fields
     */ /** score property */
    private Score score=new Score();
    public Score getScore() {
    return score;
    } /** 
     * Set the score.
     * @param score The score to set
     */
    public void setScore(Score score) {
    this.score = score;
    }
    }3、UserInfoForm:----------------
    /*
     * Generated by MyEclipse Struts
     * Template path: templates/java/JavaClass.vtl
     */
    package com.myweb.struts.form;import org.apache.struts.action.ActionForm;
    import com.myweb.entity.UserInfo;/** 
     * MyEclipse Struts
     * Creation date: 06-27-2009
     * 
     * XDoclet definition:
     * @struts.form name="userInfoForm"
     */
    public class UserInfoForm extends BaseForm {
    /*
     * Generated fields
     */ /** userinfo property */
    private UserInfo userinfo=new UserInfo();
    /*
     * Generated Methods
     */ /** 
     * Returns the userinfo.
     * @return UserInfo
     */
    public UserInfo getUserinfo() {
    return userinfo;
    } /** 
     * Set the userinfo.
     * @param userinfo The userinfo to set
     */
    public void setUserinfo(UserInfo userinfo) {
    this.userinfo = userinfo;
    }
    }===============================================================================
    ==========================================PageBean==============================
    package com.myweb.util;import java.util.ArrayList;
    import java.util.List;public class PageBean {
    private int currentPage=1;//当前页
    private int allPage=0;//总页数
    private int allNum=0;//总行数
    private int pageNum=5;//每页显示行数
    private List date=new ArrayList();//每页显示数据 

    public int getAllNum() {
    return allNum;
    }
    public void setAllNum(int allNum) {
    this.allNum = allNum;
    }
    public int getAllPage() {
    return allPage;
    }
    public void setAllPage(int allPage) {
    this.allPage = allPage;
    }
    public int getCurrentPage() {
    return currentPage;
    }
    public void setCurrentPage(int currentPage) {
    this.currentPage = currentPage;
    }
    public List getDate() {
    return date;
    }
    public void setDate(List date) {
    this.date = date;
    }
    public int getPageNum() {
    return pageNum;
    }
    public void setPageNum(int pageNum) {
    this.pageNum = pageNum;

    }