您的位置:首页 > 其它

S2SH 分页

2013-05-14 09:09 295 查看
核心代码:Page.java,PageUtil.java,Result.java

下载地址为:http://download.csdn.net/detail/clint2002/5371655

具体使用方法:

1、把这三个类放到src下,包名为com.clint.fenye(或其他名字)

2、在需要显示分页的那个板块的DaoImpl中添加如下方法并在Dao中添加接口(2个,红色为核心代码log.debug等为自动生成的,getSession() 可能报错,原因可能是我们使用Hibernate所写的方法不同,我继承的是HibernateDaoSupport接口):

  //传一个Page进去, 如Lxr为数据库中表名,目的是从数据库中查询出所有的

public List findAll(Page page) {

log.debug("finding all Lxr  instances");

try {

String queryString = "from Lxr";

Query queryObject = getSession().createQuery(queryString);

queryObject.setFirstResult(page.getBeginIndex());

queryObject.setMaxResults(page.getEveryPage());

return queryObject.list();

} catch (RuntimeException re) {

log.error("find all failed", re);

throw re;

}

}

//返回查询的总条数

public int queryLxrConut() {

log.debug("finding all Lxr instances");

try {

String queryString = "from Lxr";

Query queryObject = getSession().createQuery(queryString);

return queryObject.list().size();

} catch (RuntimeException re) {

log.error("find all failed", re);

throw re;

}

}

3、在相应serviceImpl中配置,并在service添加相应接口,serviceImpl示例如下:

public class LxrServiceImpl implements  LxrService{

private LxrDAO lxrDAO;

public LxrDAO getLxrDAO() {

return lxrDAO;

}

public void setLxrDAO(LxrDAO lxrDAO) {

this.lxrDAO = lxrDAO;

}

public Result findLxrByPage(Page page) {

page = PageUtil.createPage(page, lxrDAO.queryLxrConut());

List<Lxr> lxrs = lxrDAO.findAll(page);

Result result = new Result();

result.setList(lxrs);

result.setPage(page);

return result;

}

4、新建action(名字随便取,不过得--如UserFindAllAction等):

注:引入相关的类和继承合适的类,我这是直接继承的ActionSupport,若Base中已继承相关接口,则继承Base类。

public class LxrFindAllAction extends ActionSupport {

private LxrService lxrService;  //相关的service,

private Integer currentPage;   //设置当前页

public Integer getCurrentPage() {

return currentPage;

}

public void setCurrentPage(Integer currentPage) {

this.currentPage = currentPage;

}

public LxrService getLxrService() {

return lxrService;

}

public void setLxrService(LxrService lxrService) {

this.lxrService = lxrService;

}

public String execute() throws Exception {

Page page = new Page();

page.setEveryPage(10);

if(currentPage == null) {

currentPage = new Integer(1);

}

page.setCurrentPage(currentPage);

Result result = lxrService.findLxrByPage(page);

HttpServletRequest request = ServletActionContext.getRequest();

request.setAttribute("lxrs", result.getList());  //转到request域中

request.setAttribute("page", result.getPage());

return this.SUCCESS;

}

}

5、在显示页面(jsp)中获取分页信息:我这用的是一个表格(根据超链接把参数转到struts中)

            <tr>

             <td colspan="11" align="center">

             <s:if test="#request.page.hasPrePage">

             <a href="lxrFindAll.action?currentPage=1">首页</a>

             <a href="lxrFindAll.action?currentPage=${request.page.currentPage - 1 }">上一页</a>

             </s:if>

             <s:else>

             首页

             上一页

             </s:else>

             <s:if test="#request.page.hasNextPage">

             <a href="lxrFindAll.action?currentPage=${request.page.currentPage + 1 }">下一页</a>

             <a href="lxrFindAll.action?currentPage=${request.page.totalPage }">尾页</a>

             </s:if>

             <s:else>

             下一页

             尾页

             </s:else>

             当前第${page.currentPage}页,共${page.totalPage}页,共有${page.totalCount}条记录。

             </td>

            </tr>  

6、配置struts.xml(具体怎么转根据具体情况;如下),

  注意:得在spring配置文件中配置相应的action。

<action name="lxrFindAll" class="lxrFindAllAction">

     <result name="success">/form/ShowLxr.jsp</result>

  </action>

若有不懂处,请留言,我看到会尽快解决你的问题,此外,我也是菜鸟一个,若有什么更好的分页方法,我们可以相互讨论,相互学习--
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  s2sh 分页