Struts2与Hibernate简易分页Demo(待完善)

UserPojo:

package com.xxx.practice.pojo;

import java.util.Date;

public class User {
	private String id;
	private String userName;
	private String sex;
	private String age;

	private String email;
	private String mobilePhone;
    private String address;
    
    private Date birthday;
    
	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getAge() {
		return age;
	}

	public void setAge(String age) {
		this.age = age;
	}

	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getMobilePhone() {
		return mobilePhone;
	}

	public void setMobilePhone(String mobilePhone) {
		this.mobilePhone = mobilePhone;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public Date getBirthday() {
		return birthday;
	}

	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}
	
    
}
UserDao:

public interface UserDao {
        int staticsUserCount();
         /**
	 * @param begin    当前页面第一条记录开始处
	 * @param pageSize 每页显示的最大条数
	 * @return 
	 */
	List<User> getPage(int begin , int pageSize); 
}

UserDaoImpl:

@Override
	public int staticsUserCount() {
		return userDao.staticsUserCount();
	}

	@Override
	public PagingVo getPage(int pageNum){
		int count = userDao.staticsUserCount();
		int totalPages = count % ConstantDic.PAGESIZE == 0 ? count / ConstantDic.PAGESIZE :count / ConstantDic.PAGESIZE + 1; 
		int begin = (pageNum - 1) * ConstantDic.PAGESIZE ;
		List<User> userList = userDao.getPage(begin, ConstantDic.PAGESIZE);
		
		PagingVo pagingVo = new PagingVo();
		pagingVo.setRecordCount(count);
		pagingVo.setTotalPages(totalPages);
		pagingVo.setCurrentPage(pageNum);
		pagingVo.setResultList(userList);
		return pagingVo;
	}

UserService:

public interface UserService {
 int staticsUserCount();
  PagingVo getPage(int pageNum);
}

UserServiceImpl:

@Override
	public int staticsUserCount() {
		return userDao.staticsUserCount();
	}

	@Override
	public PagingVo getPage(int pageNum){
		int count = userDao.staticsUserCount();
		int totalPages = count % ConstantDic.PAGESIZE == 0 ? count / ConstantDic.PAGESIZE :count / ConstantDic.PAGESIZE + 1; 
		int begin = (pageNum - 1) * ConstantDic.PAGESIZE ;
		List<User> userList = userDao.getPage(begin, ConstantDic.PAGESIZE);
		
		PagingVo pagingVo = new PagingVo();
		pagingVo.setRecordCount(count);
		pagingVo.setTotalPages(totalPages);
		pagingVo.setCurrentPage(pageNum);
		pagingVo.setResultList(userList);
		return pagingVo;
	}

UserAction:

package com.xxx.practice.user.web.action;

import com.opensymphony.xwork2.ActionSupport;
import com.xxx.practice.service.UserService;
import com.xxx.practice.service.impl.UserServiceImpl;
import com.xxx.practice.vo.PagingVo;

public class UserAction extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private PagingVo pageingVo;
	private int page;
	//TODO IoC
    private UserService userSerivce = new UserServiceImpl();
	@Override
	public String execute() throws Exception {
		return "success";
	}
    
	public String showAllUsers() throws Exception{
		page = (page == 0) ? 1 :page;
		//TODO other validate :isNotNumber etc...
		pageingVo = userSerivce.getPage(page);
		return "displayUser";
	}

	public PagingVo getPageingVo() {
		return pageingVo;
	}

	public void setPageingVo(PagingVo pageingVo) {
		this.pageingVo = pageingVo;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

}

jsp:

<%@ page language="java" import="java.util.*"  contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<!-- doctype声明 的dtd目前最好使用过渡版本,即xhtml1-transitional.dtd,这里仅作效果演示-->
<html>
<head>
<base href="<%=basePath%>">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>所有用户</title>
<style type="text/css">
  a:link{text-decoration:none}
  a:visited{text-decoration:none}
  a:hover{text-decoration:none}
</style>
</head>
<body>

  <div>
  <!--  -->
  <table>
    <tr>
    <td>姓名</td>
    <td>性别</td>
    <td>年龄</td>
    <td>电子邮件</td>
    </tr>
    <s:iterator var="user" status="s" value="pageingVo.resultList">
    <tr>
         <td><s:property value="#user.userName"/></td>
         <td>
         <s:if test="#user.sex == 'MALE'">男</s:if>
         <s:else>女</s:else>
         </td>
         <td><s:property value="#user.age"/></td>
         <td><s:property value="#user.email"/></td>
    </tr>
    </s:iterator>
    <tr>
  </table>
  </div>
  <s:debug></s:debug>
  <div id="paging">
             共<s:property value="pageingVo.totalPages"/>页 
    <a href="userManager/userManager/showUsers?page=1">首页 </a>
     <s:if test="pageingVo.hasPrevious == true">
          <a href="userManager/userManager/showUsers?page=<s:property value='pageingVo.currentPage - 1'/>">上一页</a>  
    </s:if>
    <s:if test="pageingVo.hasNext">
          <a href="userManager/userManager/showUsers?page=<s:property value='pageingVo.currentPage + 1'/>">下一页</a>
    </s:if>
    <s:if test="pageingVo.hasNext">
     <a href="userManager/userManager/showUsers?page=<s:property value='pageingVo.totalPages'/>">尾页 </a>
    </s:if>
             第<s:property value="pageingVo.currentPage"/>页  
  </div>
</body>
</html>

效果图1:


效果图2:


效果图3:



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值