queryDeleteUser.jsp 默认每次读10条数据,当然可以设置
<s:if test="first>0">
<td width="49"><div align="center"><a href="admin/queryUsers!queryUsers?first=<s:property value="first-10"/>&maxLength=10">上一页</a></div></td>
</s:if>
<s:if test="(first+maxLength)<totalPage">
<td width="49"><div align="center"><a href="admin/queryUsers!queryUsers?first=<s:property value="first+10"/>&maxLength=10">下一页</a></div></td></s:if>
struts.xml
<action name="queryUsers" class="com.apache.shopping.action.AdminUserAction">
<result>/queryDeleteUser.jsp</result>
</action>
AdminUserAction.java
package com.apache.shopping.action;
import java.util.ArrayList;
import java.util.Map;
import org.apache.struts2.interceptor.SessionAware;
import com.apache.shopping.dto.UserDto;
import com.apache.shopping.model.Admin;
import com.apache.shopping.model.User;
import com.apache.shopping.service.UserService;
import com.opensymphony.xwork2.ActionSupport;
public class AdminUserAction extends ActionSupport implements SessionAware{
public static final long serialVersionUID=-2352343211L;
private Map session;
private UserService userService;
private UserDto userDto;
private int first;
private int maxLength;
private int totalPage;
private int id;
private ArrayList<User> users;
public ArrayList<User> getUsers() {
return users;
}
public void setUsers(ArrayList<User> users) {
this.users = users;
}
public int getFirst() {
return first;
}
public void setFirst(int first) {
this.first = first;
}
public int getMaxLength() {
return maxLength;
}
public void setMaxLength(int maxLength) {
this.maxLength = maxLength;
}
public void setSession(Map<String, Object> session) {
this.session=session;
}
public UserService getUserService() {
return userService;
}
public void setUserService(UserService userService) {
this.userService = userService;
}
public UserDto getUserDto() {
return userDto;
}
public void setUserDto(UserDto userDto) {
this.userDto = userDto;
}
public String queryUsers(){
totalPage=userService.getAllUserSize();
users=userService.loadAllUsers(first, maxLength);
return SUCCESS;
}
public int getTotalPage() {
return totalPage;
}
public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
}
public String deleteUser(){
userService.deleteUserById(id);
return queryUsers();
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
UserDaoImpl.java
@SuppressWarnings("unchecked")
public ArrayList<User> loadAllUsers(final int first,final int maxLength) {//关键代码
return (ArrayList<User>)hibernateTemplate.executeFind(new HibernateCallback(){
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
Query query=session.createQuery("from User");
query.setFirstResult(first);
query.setMaxResults(maxLength);
return query.list();
}
});
}
很简单就做到了分页