测试webwork与spring的集成,写一个简单的分页程序,xwork.xml文件里面配置action,class属性里面写类名,只能显示第一页,往下翻页的时候其中一个参数会为空,奇怪!
如果action的时候,class属性里面写bean的id,翻页就正常了。这是为什么呢?
把几个重要文件的代码贴出来:
- PHP code
<%@ page contentType="text/html; charset=gb2312" %><%@ taglib prefix="ww" uri="/webwork" %><jsp:include flush="true" page="/head.jsp"></jsp:include><div align="center"><ww:set name="pg" value="#request.pager"/><table width="500" border="1" cellspacing="0" bordercolor="#666666"> <tr> <td width="210"><div align="center">图书名称</div></td> <td width="140"><div align="center">价格</div></td> </tr> <ww:iterator value="#request['books']" id="book"> <tr> <td><div align="center"><ww:property value="#book.bookname"/></div></td> <td><div align="center"><ww:property value="#book.price" /></div></td> </tr> </ww:iterator></table><ww:if test="#pg.hasFirst"> <a href="browsebook.action?currentPage=1">首页</a></ww:if><ww:else>首页</ww:else><ww:if test="#pg.hasPrevious"> <a href="browsebook.action?currentPage=<ww:property value="#pg.currentPage-1"/>">前页</a></ww:if><ww:else>前页</ww:else><ww:if test="#pg.hasNext"> <a href="browsebook.action?currentPage=<ww:property value="#pg.currentPage+1"/>">后页</a></ww:if><ww:else>后页</ww:else><ww:if test="#pg.hasLast"> <a href="browsebook.action?currentPage=<ww:property value="#pg.totalPages"/>">尾页</a></ww:if><ww:else>尾页</ww:else><br>总共<ww:property value="#pg.totalPages"/>页,当前第<ww:property value="#pg.currentPage"/>页</div>
xwork.xml配置文件:
- XML code
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE xwork PUBLIC "-//OpenSymphony Group//XWork 1.1.1//EN" "http://www.opensymphony.com/xwork/xwork-1.1.1.dtd"><xwork> <include file="webwork-default.xml"></include> <package name="book" extends="webwork-default" namespace="/book"> <action name="browsecatalog" class="cn.gth.test.view.BookController"> <result name="success">/browsecatalog.jsp</result> </action> <action name="browsebook" class="cn.gth.test.view.BookController" method="browseBook"> <result name="success">/browsebook.jsp</result> <result name="error">/error.jsp</result> </action> </package></xwork>
上面是class属性直接写类名,翻页的时候出现问题,浏览器显示的是error.jsp这个文件
action类文件:
- Java code
package cn.gth.test.view;import java.util.List;import java.util.Map;import cn.gth.test.common.Pager;import cn.gth.test.dao.IBookDao;import cn.gth.test.dao.ICatalogDao;import com.opensymphony.xwork.ActionContext;import com.opensymphony.xwork.ActionSupport;@SuppressWarnings("serial")public class BookController extends ActionSupport { private ICatalogDao catalogDaoImp; private IBookDao bookDaoImp; private Long catalogid; private int currentPage = 1; public ICatalogDao getCatalogDaoImp() { return catalogDaoImp; } public void setCatalogDaoImp(ICatalogDao catalogDaoImp) { this.catalogDaoImp = catalogDaoImp; } public IBookDao getBookDaoImp() { return bookDaoImp; } public void setBookDaoImp(IBookDao bookDaoImp) { this.bookDaoImp = bookDaoImp; } public Long getCatalogid() { return catalogid; } public void setCatalogid(Long catalogid) { this.catalogid = catalogid; } public int getCurrentPage() { return currentPage; } public void setCurrentPage(int currentPage) { this.currentPage = currentPage; } @SuppressWarnings("unchecked") @Override public String execute() throws Exception { List catalogs = catalogDaoImp.getAllCatalogs(); Map requ = (Map)ActionContext.getContext().get("request"); requ.put("cats", catalogs); return SUCCESS; } @SuppressWarnings("unchecked") public String browseBook() throws Exception { if (catalogid != null) { int totalSize = bookDaoImp.getTotalByCatalogid(catalogid); Pager pager = new Pager(5,currentPage,totalSize); List books = bookDaoImp.getBookByCatalogid(catalogid, currentPage, pager.getPageSize()); Map requ = (Map)ActionContext.getContext().get("request"); requ.put("books", books); requ.put("pager", pager); this.setCurrentPage(1); return SUCCESS; } return ERROR; }}