我想各位做java web开发的人都做过分页是吧!
关于struts1.2分页问题小弟有点不明白请各位看下我的代码指点下。这里是分页的页面当我点击上一页或下一页或goto的时候回到action里去处理
<html:link page="/page.do?action=firstPage">首页</html:link>
<logic:equal name="page" property="hasNextPage" value="true">
<html:link page="/page.do?action=nextPage">下一页</html:link> </logic:equal>
<logic:notEqual name="page" property="hasNextPage" value="true">
下一页
</logic:notEqual>
<logic:equal name="page" property="hasPreviousPage" value="true">
<html:link page="/page.do?action=previousPage">上一页</html:link> </logic:equal>
<logic:notEqual name="page" property="hasPreviousPage" value="false">
上一页
</logic:notEqual>
<html:link page="/page.do?action=lastPage">末页</html:link>
<form action="page.do?action=goto" method="post">输入要跳转的页数:<input type="text" name="pages"/>
<input type="submit" value="goto"/>这里是我的处理代码
public class PageAction extends Action {
//分页类
private Pager page;
private List list;
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
PageForm pageForm=(PageForm)form;
String action=null;
action=request.getParameter("action");
if(action==null||action.equals("null")){
list=new ListBean().findAll();
page=new Pager(list);
list=page.getPageList();
request.setAttribute("list", list);
request.setAttribute("page", page);
}else{
if(action=="firstPage"||action.equals("firstPage")){
page.getFirstPage();
list=page.getPageList();
request.setAttribute("list", list);
request.setAttribute("page", page);
}
if(action=="nextPage"||action.equals("nextPage")){
page.getNextPage();
list=page.getPageList();
request.setAttribute("list", list);
request.setAttribute("page", page);
}
if(action=="previousPage"||action.equals("previousPage")){
page.getPreviousPage();
list=page.getPageList();
request.setAttribute("list", list);
request.setAttribute("page", page);
}
if(action=="lastPage"||action.equals("lastPage")){
page.getLastPage();
list=page.getPageList();
request.setAttribute("list", list);
request.setAttribute("page", page);
}
if("goto".equals(action)){
//一会再说这种情况先把基本的搞定先
int pages=pageForm.getPages();
page.setGotoPage(pages);
list=page.getPageList();
request.setAttribute("list", list);
request.setAttribute("page", page);
}
}
return mapping.findForward("page");
}
}
这是我很早以前刚开始开发做的分页代码了,现在在看这个代码觉得问题很大,我说的问题不是说Action类继承错误不应该把所以得请求方在一个方法里去处理,我的意思是说struts1中的action是单例模式的就是说一个action是所有用户共享的,那么我想问下如果当有多个用户在做分页操作的时候都调用了这个Action来分页那不是会覆盖原来用户所设置的Pager(分页类)类中信息。不知道你们想过这样的问题没有还是我理解错了。请高手指点
关于struts1.2分页问题小弟有点不明白请各位看下我的代码指点下。这里是分页的页面当我点击上一页或下一页或goto的时候回到action里去处理
<html:link page="/page.do?action=firstPage">首页</html:link>
<logic:equal name="page" property="hasNextPage" value="true">
<html:link page="/page.do?action=nextPage">下一页</html:link> </logic:equal>
<logic:notEqual name="page" property="hasNextPage" value="true">
下一页
</logic:notEqual>
<logic:equal name="page" property="hasPreviousPage" value="true">
<html:link page="/page.do?action=previousPage">上一页</html:link> </logic:equal>
<logic:notEqual name="page" property="hasPreviousPage" value="false">
上一页
</logic:notEqual>
<html:link page="/page.do?action=lastPage">末页</html:link>
<form action="page.do?action=goto" method="post">输入要跳转的页数:<input type="text" name="pages"/>
<input type="submit" value="goto"/>这里是我的处理代码
public class PageAction extends Action {
//分页类
private Pager page;
private List list;
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
PageForm pageForm=(PageForm)form;
String action=null;
action=request.getParameter("action");
if(action==null||action.equals("null")){
list=new ListBean().findAll();
page=new Pager(list);
list=page.getPageList();
request.setAttribute("list", list);
request.setAttribute("page", page);
}else{
if(action=="firstPage"||action.equals("firstPage")){
page.getFirstPage();
list=page.getPageList();
request.setAttribute("list", list);
request.setAttribute("page", page);
}
if(action=="nextPage"||action.equals("nextPage")){
page.getNextPage();
list=page.getPageList();
request.setAttribute("list", list);
request.setAttribute("page", page);
}
if(action=="previousPage"||action.equals("previousPage")){
page.getPreviousPage();
list=page.getPageList();
request.setAttribute("list", list);
request.setAttribute("page", page);
}
if(action=="lastPage"||action.equals("lastPage")){
page.getLastPage();
list=page.getPageList();
request.setAttribute("list", list);
request.setAttribute("page", page);
}
if("goto".equals(action)){
//一会再说这种情况先把基本的搞定先
int pages=pageForm.getPages();
page.setGotoPage(pages);
list=page.getPageList();
request.setAttribute("list", list);
request.setAttribute("page", page);
}
}
return mapping.findForward("page");
}
}
这是我很早以前刚开始开发做的分页代码了,现在在看这个代码觉得问题很大,我说的问题不是说Action类继承错误不应该把所以得请求方在一个方法里去处理,我的意思是说struts1中的action是单例模式的就是说一个action是所有用户共享的,那么我想问下如果当有多个用户在做分页操作的时候都调用了这个Action来分页那不是会覆盖原来用户所设置的Pager(分页类)类中信息。不知道你们想过这样的问题没有还是我理解错了。请高手指点
* 章节管理Action。
*
* @author xcf
*/
import java.io.File;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;import com.socix.comic.common.Alerter;
import com.socix.comic.common.BaseAction;
import com.socix.comic.common.COMICCFG;
import com.socix.comic.common.ComicException;
import com.socix.comic.common.FileUpload;
import com.socix.comic.dao.ChapterDAO;
import com.socix.comic.dao.SeriesDAO;
import com.socix.comic.form.AdminUserForm;
import com.socix.comic.form.ChapterForm;
import com.socix.comic.obj.ChapterObj;
import com.socix.comic.obj.SeriesObj;
import com.socix.configure.GlobalInfo;public class ChapterAction extends BaseAction { public ActionForward doPerform(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
String forward = "";
ChapterForm chapterForm = (ChapterForm) form;
String functionName = chapterForm.getFunctionName();
String functionData = chapterForm.getFunctionData();
chapterForm.setFunctionName("");
chapterForm.setFunctionData(""); // 插入
if ("Ins_Chapter".equalsIgnoreCase(functionName)) {
forward = "B4_Ins_Chapter"; try {
ChapterObj chapterObj = chapterForm.getChapterObj();
SeriesObj seriesObj = chapterForm.getSeriesObj();
String filePath = seriesObj.getSeriesId() + "/"
+ chapterObj.getResolution() + "/"
+ chapterObj.getChapterNo()
+ COMICCFG.STORE.CHAPTER_EXT; chapterObj.setSeriesId(seriesObj.getSeriesId());
chapterObj.setFilePath(filePath);
try {
// 获取存储根路径
String storePath = GlobalInfo.getProperty(
COMICCFG.STORE.comic_content, COMICCFG.STORE.app,
COMICCFG.STORE.root_dir);
if (storePath == null || storePath.equals("")) {
throw new ComicException("取存储根路径失败!");
}
// 取上传后的文件
FormFile formFile = chapterForm.getFormFile();
if (formFile == null || formFile.getFileName().equals("")) {
throw new ComicException("取上传后的章节文件失败!");
} String storeDir = storePath + seriesObj.getSeriesId() + "/"
+ chapterObj.getResolution() + "/";
// 存文件
FileUpload.storeFile(formFile, storeDir, chapterObj
.getChapterNo()); // 取操作员ID
String operatorId = ((AdminUserForm) request.getSession()
.getAttribute("ADMIN_USER_INFORMATION"))
.getAdminUser().getUserId(); // 插入数据库
ChapterDAO.getInstance().insertChapter(chapterObj,
operatorId);
chapterObj.setChapterNo(String.valueOf(Integer
.parseInt(chapterObj.getChapterNo()) + 1));
} catch (ComicException e) {
if (e.getMessage().contains("ORA-00001")) {
throw new ComicException(" “章节序号-分辨率”重复!");
} else {
throw e;
}
} catch (Exception e) {
throw e;
}
chapterForm.setFunctionName("ISUCCESS");
} catch (ComicException e) {
chapterForm.setFunctionName("IFAIL");
chapterForm.setFunctionData(e.getMessage());
} catch (Exception e) {
chapterForm.setFunctionName("IFAIL");
chapterForm.setFunctionData(e.getMessage());
}
return mapping.findForward(forward);
} // 插入前
if ("B4_Ins_Chapter".equalsIgnoreCase(functionName)) {
forward = "B4_Ins_Chapter";
return mapping.findForward(forward);
} // 删除
if ("Del_Chapter".equalsIgnoreCase(functionName)) {
forward = "Query_Chapter";
try {
String chapterId = functionData.split(",")[0];
String resolution = functionData.split(",")[1];
String chapterNo = functionData.split(",")[2];
SeriesObj seriesObj = chapterForm.getSeriesObj();
String operatorId = ((AdminUserForm) request.getSession()
.getAttribute("ADMIN_USER_INFORMATION")).getAdminUser()
.getUserId();
// 删除记录
int ret = ChapterDAO.getInstance().deleteChapter(
seriesObj.getSeriesId(), chapterId, resolution,
operatorId); if (ret == 0) {
Alerter.delChapter(seriesObj.getSeriesId(), resolution,
chapterNo);
}
// 删除文件
try {
String fileName = GlobalInfo.getProperty(
COMICCFG.STORE.comic_content, COMICCFG.STORE.app,
COMICCFG.STORE.root_dir)
+ seriesObj.getSeriesId()
+ "/"
+ resolution
+ "/"
+ chapterNo + COMICCFG.STORE.CHAPTER_EXT;
File file = new File(fileName);
if (file.exists()) {
file.delete();
}
} catch (Exception e) {
// 如果文件删除失败,忽略
}
chapterForm.setFunctionName("SUCCESS");
} catch (ComicException e) {
chapterForm.setFunctionName("FAIL");
chapterForm.setFunctionData(e.toString());
}
chapterForm.getIteratorChapter();
chapterForm.setPagingInfoTotalSize();
return mapping.findForward(forward);
} // 查询
if ("Query_Chapter".equalsIgnoreCase(functionName)) { // 设置翻页需要的记录总数
SeriesObj seriesObj = SeriesDAO.getInstance().getSeriesObj(
functionData);
chapterForm.setSeriesObj(seriesObj);
chapterForm.getIteratorChapter();
chapterForm.setPagingInfoTotalSize();
forward = "Query_Chapter";
return mapping.findForward(forward);
} // 翻页
if ("Paging".equalsIgnoreCase(functionName)) {
chapterForm.getPagingInfo().setPageAction(functionData);
chapterForm.getPagingInfo().changePage();
forward = "Query_Chapter";
return mapping.findForward(forward);
} return mapping.findForward(forward);
}
}
或者在外部写一个分页的类,调用就行了啊!还混到struts里头,麻烦!
或者在外部写一个分页的类,调用就行了啊!还混到struts里头,麻烦!
以下我使用jsp+javaBean时借用的分页类:当然也可以用在单独使用Struts的分页上package com.bjsxt.drp.util;import java.util.List;public class PageModel {
//所有的记录数
private int totalRecords;
//查询的结果集
private List<?> list; //第几页
private int pageNo;
//每页多少条数据
private int pageSize;
public List<?> getList() {
return list;
} public void setList(List<?> list) {
this.list = list;
} public int getTotalRecords() {
return totalRecords;
} public void setTotalRecords(int totalRecords) {
this.totalRecords = totalRecords;
} public int getPageNo() {
return getTotalPages()==0?1:pageNo;
} public void setPageNo(int pageNo) {
this.pageNo = pageNo;
} public int getPageSize() {
return pageSize;
} public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getTotalPages() {
if (totalRecords == 0)
return 1;
return (totalRecords + pageSize -1)/pageSize;
}
//得到首页
public int getTopPageNo() {
return 1;
}
//得到上一页
public int getPreviousPageNo() {
if (pageNo == 1) {
return 1;
}
return pageNo - 1;
}
//得到下一页
public int getNextPageNo() {
if (pageNo == getBottomPageNo()) {
return getBottomPageNo();
}
return pageNo + 1;
}
//得到末页
public int getBottomPageNo() {
return getTotalPages();
}
}