【很迷惑】webwork+spring的集成,xwork.xml文件里面配置action,class属性里面写bean的id和直接写类名,差别就这么大?谁能讲讲明白?
测试webwork与spring的集成,写一个简单的分页程序,xwork.xml文件里面配置action,class属性里面写类名,只能显示第一页,往下翻页的时候其中一个参数会为空,奇怪!
如果action的时候,class属性里面写bean的id,翻页就正常了。这是为什么呢?
把几个重要文件的代码贴出来:
<%@ 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>
<?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>
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; }}
package cn.gth.test.dao.imp;import java.util.List;import org.hibernate.Query;import org.hibernate.Session;import cn.gth.test.common.DaoSession;import cn.gth.test.dao.IBookDao;public class BookDaoImp extends DaoSession implements IBookDao { public List getBookByCatalogid(Long catalogid, int currentPage, int pageSize) { Session session = this.getSession(); Query query = session.createQuery("from Book b where b.catalog.catalogid = ?"); query.setParameter(0, catalogid); int startRow = (currentPage - 1) * pageSize; query.setFirstResult(startRow); query.setMaxResults(pageSize); List books = query.list(); session.close(); return books; } public int getTotalByCatalogid(Long catalogid) { Session session = this.getSession(); Query query = session.createQuery("from Book b where b.catalog.catalogid = ?"); query.setParameter(0, catalogid); List books = query.list(); int totalSize = books.size(); session.close(); return totalSize; }}
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"> </property> <property name="url" value="jdbc:oracle:thin:@127.0.0.1:1521:hnnd"> </property> <property name="username" value="test"></property> <property name="password" value="test"></property> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource"> <ref bean="dataSource" /> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect"> org.hibernate.dialect.Oracle9Dialect </prop> <prop key="hibernate.show_sql"> true </prop> </props> </property> <property name="mappingResources"> <list> <value>cn/gth/test/domain/hbm/Catalog.hbm.xml</value> <value>cn/gth/test/domain/hbm/Book.hbm.xml</value> </list> </property> </bean> <bean id="daoSession" class="cn.gth.test.common.DaoSession"> <property name="sessionFactory"> <ref bean="sessionFactory"/> </property> </bean> <bean id="catalogDaoImp" class="cn.gth.test.dao.imp.CatalogDaoImp" parent="daoSession"> </bean> <bean id="bookDaoImp" class="cn.gth.test.dao.imp.BookDaoImp" parent="daoSession"> </bean> <bean id="bookController" class="cn.gth.test.view.BookController"> <property name="catalogDaoImp"> <ref bean="catalogDaoImp"/> </property> <property name="bookDaoImp"> <ref bean="bookDaoImp"/> </property> </bean> </beans>
<?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="bookController"> <result name="success">/browsecatalog.jsp</result> </action> <action name="browsebook" class="bookController" method="browseBook"> <result name="success">/browsebook.jsp</result> <result name="error">/error.jsp</result> </action> </package></xwork>