分页问题 本人的分不太多 请高手们见谅
调用顺序是这样的
首先一个请求到到login.action的findAllUsers()方法成功执行后跳到SUCCESS相应的页面(就是3的页面 详细内容在下面),
success页面通过findAllUsers()方法传过来的值进行页面传值,然后success里面形成一个如PageUtil类里面getToolsMenu()方法返回的字符串(就是首页,下一页的字符串)
现在问题出现了 当点击下一页的时候他是怎么用javascript往action里面传值的,就是这条语句document.forms(0).pages.value=1,原以为只要在login.action里面声明一个私有成员就可以实现,但是不好使,pages是login.action里面的私有成员吗?如果是用什么方法传进去的值,如果不是又怎么实现下一页的?(难道javascript可以操纵login.action里面的私有成员?)
1---------------------------------------------------------
这个是login.action的类 请求执行里面的findAllUsers()方法
package org.login.actions;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.login.page.PageUtil;
import org.login.service.IUserService;
import org.login.vo.Users;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;
public class Login extends ActionSupport implements ModelDriven,
Preparable {
private IUserService userService;
private String id;
private int pages = 1;
private int pagesize = 3;
private String pageBar;
private List list;
private Users user;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public IUserService getUserService() {
return userService;
}
public void setUserService(IUserService userService) {
this.userService = userService;
}
public int getPages() {
return pages;
}
public void setPages(int pageIndex) {
this.pages = pageIndex;
}
public String getPageBar() {
return pageBar;
}
public void setPageBar(String pageBar) {
this.pageBar = pageBar;
}
public List getList() {
return list;
}
public void setList(List list) {
this.list = list;
}
public Users getUser() {
return user;
}
public void setUser(Users user) {
this.user = user;
}
public void prepare() throws Exception {
if (id == null || id.length() == 0)
user = new Users();
else
user = getUserService().getUserById(Integer.parseInt(id));
}
public String execute() throws Exception {
if (getUserService().isLogin(user)) {
return SUCCESS;
}
return INPUT;
}
public String save() throws Exception {
System.out.println(user.getUserName().toString()
+ "@@@@@@@@@@@@@@@@@@@@@@");
if (getUserService().insertUser(user)) {
return SUCCESS;
}
return ERROR;
}
public String edit() {
return SUCCESS;
}
public String update() throws Exception {
if (getUserService().updateUser(user)) {
return SUCCESS;
}
return ERROR;
}
// 好像不能直接对id进行操作
public String delete() throws Exception {
if (getUserService().deleteUser(Integer.parseInt(id))) {
return SUCCESS;
}
return ERROR;
}
public String findAllUsers() throws Exception {
PageUtil page = new PageUtil();
int count = getUserService().getCount();
page.setCurPage(pages);
page.setPageSize(pagesize);
page.setTotalRow(count);
pageBar = page.getToolsMenu();
Map pageMap = new HashMap();
pageMap.put("start", page.getStart());
pageMap.put("end", page.getEnd());
list = getUserService().getAllUser(pageMap);
return SUCCESS;
}
public Users getModel() {
return user;
}
public int getPagesize() {
return pagesize;
}
public void setPagesize(int pagesize) {
this.pagesize = pagesize;
}
}
2-----------------------------------------------------
这个是分页算法类PageUtil
package org.login.page;
import java.util.List;
/**
- ibatis分页Bean
-
@author Administrator
*
*/
public class PageUtil {private int curPage = 0; //当前页
private int pageSize = 0; //每页多少行
private int endSize ; //用于not in(select top endSize id)不在多少行内
private int totalRow ; //共多少行
private int totalPage ; //共多少页public int getStart(){
if(curPage>1)
return (curPage-1)*pageSize;
else
return 0;
}
public int getEnd(){
return pageSize;
}
public int getCurPage() {
return curPage;
}
public void setCurPage(int curPage) {int temp = pageSize * (curPage-1);
this.setEndSize(temp);
this.curPage = curPage;
}
public int getEndSize() {
return endSize;
}
public void setEndSize(int endSize) {
this.endSize = endSize;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getTotalRow() {
return totalRow;
}
public void setTotalRow(int totalRow) {totalPage = totalRow/pageSize;
if(totalRow%pageSize > 0)
totalPage = totalPage + 1;this.totalRow = totalRow;
}
public int getTotalPage(){return this.totalPage;
}
public String getToolsMenu() {
StringBuffer str = new StringBuffer("");
int next, prev;
prev = curPage - 1;
next = curPage + 1;if (curPage > 1) { str.append( "<a href=\"#\" onclick=\"document.forms(0).pages.value=1;document.forms(0).submit();\">首页</a> "); } else { str.append("<a href=\"#\">首页</a> "); } if (curPage > 1) { str.append( "<a href=\"#\" onclick='document.forms(0).pages.value=" +prev + ";document.forms(0).submit();'>上页</a> "); } else { str.append("<a href=\"#\">上页</a> "); } if (curPage < totalPage) { str.append( "<a href=\"#\" onclick='document.forms(0).pages.value=" +next + ";document.forms(0).submit();'>下页</a> "); } else { str.append("<a href=\"#\" >下页</a> "); } if (totalPage > 1 && curPage != totalPage) { str.append( "<a href=\"#\" onclick='document.forms(0).pages.value=" +totalPage + ";document.forms(0).submit();'>末页</a> "); } else { str.append("<a href=\"#\" >末页</a> "); } str.append(" 共" + totalRow + "条记录"); str.append(" 每页<SELECT size=1 name=pagesize onchange='this.form.pages.value=1;this.form.pageSize.value=this.value;this.form.submit();'>"); if (pageSize == 3) { str.append("<OPTION value=3 selected>3</OPTION>"); } else { str.append("<OPTION value=3>3</OPTION>"); } if (pageSize == 10) { str.append("<OPTION value=10 selected>10</OPTION>"); } else { str.append("<OPTION value=10>10</OPTION>"); } if (pageSize == 20) { str.append("<OPTION value=20 selected>20</OPTION>"); } else { str.append("<OPTION value=20>20</OPTION>"); } if (pageSize == 50) { str.append("<OPTION value=50 selected>50</OPTION>"); } else { str.append("<OPTION value=50>50</OPTION>"); } if (pageSize == 100) { str.append("<OPTION value=100 selected>100</OPTION>"); } else { str.append("<OPTION value=100>100</OPTION>"); } str.append("</SELECT>"); str.append("条 分" + pageSize + "页显示 转到"); str.append("<SELECT size=1 name=Pagelist onchange='this.form.pages.value=this.value;this.form.submit();'>"); for (int i = 1; i < totalPage + 1; i++) { if (i == curPage) { str.append("<OPTION value=" + i + " selected>" + i + "</OPTION>"); } else { str.append("<OPTION value=" + i + ">" + i + "</OPTION>"); } } str.append("</SELECT>页"); str.append("<INPUT type=hidden value=" + curPage + " name=\"pages\" > "); str.append("<INPUT type=hidden value=" + pageSize + " name=\"pageSize\"> "); return str.toString();
}
}
3---------------------------------------------------------------------------------
这个是success成功之后跳转的页面 也就是显示分页的页面
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
Users list
<br><br> table {<br><br> border-collapse: collapse; <br> font-size: 12px;<br><br> }<br><br>
UserID | UserName | Password | Options | |