public int getTotalpagesize(int rowcount) {
int totalpagesize = 0;
if (rowcount % pagesize == 0) {
totalpagesize = rowcount / pagesize;
} else {
totalpagesize = rowcount / pagesize + 1;
}
return totalpagesize;
}
}
自定义分页标签(3)
时间:2009-12-13 BlogJava §朱家二少§
到此为止,自定义标签书已完成。可应用于项目各处,只要页面上遵循标签描述规则,后台该给标签属性传值的时候记得传就行了。
以下用一个简单的例子来说明一下,红色字体显示的部分别忘记写就行了。
UserAction.java:
public class UserAction extends ActionSupport {
private UserService userService;
private List<User> users;
public String findUser(){
String str = null;
HttpServletRequest request = ServletActionContext.getRequest();
Map sessionMap = ActionContext.getContext().getSession();
String currpagenum= "1";
try {
String pagenum = request.getParameter("pagenum ");
if(pagenum != null && pagenum .length()!=0){
currpagenum= pagenum ;
}
} catch (Exception e) {
}
//查询用户记录
users= userService.findUser(pageNum);
if(users.size!=0){
request.setAttribute("users", users);
int rowcount = userService.getCount();
request.setAttribute("rowcount ",rowcount );
request.setAttribute("currpagenum",currpagenum);
str = "success";//成功视图
}else{
message = "无记录!"
str = "failure";//失败视图
}
request.setAttribute("pagesize", PagerTag.USER_PAGESIZE);
request.setAttribute("action", "findUser.action);
//返回视图
return str;
}
public UserService getUserService() {
return userService;
}
public void setUserService(UserService userService) {
this.userService = userService;
}
public List<User> getUsers(){
return users;
}
public void setUsers(List<User> users){
this.users = users;
}
}
UserService.java:
public class UserService {
private UserDao userDao;
public List<User> findUser(String pageNum){
List<User> userList = userDao.findUser(pageNum);
return userList;
}
public int getCount(){
int count = userDao.getCount();
return count;
}
public UserDao getUserDao() {
return userDao;
}
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
}
UserDao.java:
public class UserDao extends HibernateDaoSupport {
/**
* 查询用户
* @return User对象集合
*/
public List<User> findUser(String pagenum) {
List<User> users = null;
Session session = null;
try {
int myPagenum= Integer.parseInt(pagenum);
String hql = "from User";
session = this.getSession();
Query query = session.createQuery(hql);
query.setFirstResult(Pager.USER_PAGESIZE * (myPagenum - 1));
query.setMaxResults(Pager.USER_PAGESIZE);
users = query.list();
session.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (session != null) {
session.close();
}
}
return users;
}
/**
* 获取用户总记录数
* @return 用户总记录数
*/
public int getCount(){
String hql ="select count(id) from User
|