技术开发 频道

Struts2、Spring和Hibernate应用实例(二)



【IT168 专稿】

Struts2、Spring和Hibernate应用实例(一)


六、        创建Action类:BookAction
 
有Struts 1.x经验的朋友都知道Action是Struts的核心内容,当然Struts 2.0也不例外。不过,Struts 1.x与Struts 2.0的Action模型很大的区别。
 
Struts 1.x
Stuts 2.0
接口
必须继承org.apache.struts.action.Action或者其子类
无须继承任何类型或实现任何接口
表单数据
表单数据封装在FormBean中
表单数据包含在Action中,通过Getter和Setter获取
 
1、建立BookAction

package com.sterning.books.web.actions; import java.util.Collection; import com.sterning.books.model.Books; import com.sterning.books.services.iface.IBooksService; import com.sterning.commons.AbstractAction; import com.sterning.commons.Pager; import com.sterning.commons.PagerService; public class BooksAction extends AbstractAction { private IBooksService booksService; private PagerService pagerService; private Books book; private Pager pager; protected Collection availableItems; protected String currentPage; protected String pagerMethod; protected String totalRows; protected String bookId; protected String queryName; protected String queryValue; protected String searchName; protected String searchValue; protected String queryMap; public String list() throws Exception { if(queryMap ==null||queryMap.equals("")){ }else{ String[] str=queryMap.split("~"); this.setQueryName(str[0]); this.setQueryValue(str[1]); } System.out.println("asd"+this.getQueryValue()); int totalRow=booksService.getRows(this.getQueryName(),this.getQueryValue()); pager=pagerService.getPager(this.getCurrentPage(), this.getPagerMethod(), totalRow); this.setCurrentPage(String.valueOf(pager.getCurrentPage())); this.setTotalRows(String.valueOf(totalRow)); availableItems=booksService.getBooks(this.getQueryName(),this.getQueryValue(),pager.getPageSize(), pager.getStartRow()); this.setQueryName(this.getQueryName()); this.setQueryValue(this.getQueryValue()); this.setSearchName(this.getQueryName()); this.setSearchValue(this.getQueryValue()); return SUCCESS; } public String load() throws Exception { if(bookId!=null) book = booksService.getBook(bookId); else bookId=booksService.getMaxID(); return SUCCESS; } public String save() throws Exception { if(this.getBook().getBookPrice().equals("")){ this.getBook().setBookPrice("0.0"); } String id=this.getBook().getBookId(); Books book=booksService.getBook(id); if(book == null) booksService.addBook(this.getBook()); else booksService.updateBook(this.getBook()); this.setQueryName(this.getQueryName()); this.setQueryValue(this.getQueryValue()); if(this.getQueryName()==null||this.getQueryValue()==null||this.getQueryName().equals("")||this.getQueryValue().equals("")){ }else{ queryMap=this.getQueryName()+"~"+this.getQueryValue(); } return SUCCESS; } public String delete() throws Exception { booksService.deleteBook(this.getBookId()); if(this.getQueryName()==null||this.getQueryValue()==null||this.getQueryName().equals("")||this.getQueryValue().equals("")){ }else{ queryMap=this.getQueryName()+"~"+this.getQueryValue(); } return SUCCESS; } public Books getBook() { return book; } public void setBook(Books book) { this.book = book; } public IBooksService getBooksService() { return booksService; } public void setBooksService(IBooksService booksService) { this.booksService = booksService; } public Collection getAvailableItems() { return availableItems; } public String getCurrentPage() { return currentPage; } public void setCurrentPage(String currentPage) { this.currentPage = currentPage; } public String getPagerMethod() { return pagerMethod; } public void setPagerMethod(String pagerMethod) { this.pagerMethod = pagerMethod; } public Pager getPager() { return pager; } public void setPager(Pager pager) { this.pager = pager; } public String getTotalRows() { return totalRows; } public void setTotalRows(String totalRows) { this.totalRows = totalRows; } public String getBookId() { return bookId; } public void setBookId(String bookId) { this.bookId = bookId; } public String getQueryName() { return queryName; } public void setQueryName(String queryName) { this.queryName = queryName; } public String getQueryValue() { return queryValue; } public void setQueryValue(String queryValue) { this.queryValue = queryValue; } public String getSearchName() { return searchName; } public void setSearchName(String searchName) { this.searchName = searchName; } public String getSearchValue() { return searchValue; } public void setSearchValue(String searchValue) { this.searchValue = searchValue; } public String getQueryMap() { return queryMap; } public void setQueryMap(String queryMap) { this.queryMap = queryMap; } public PagerService getPagerService() { return pagerService; } public void setPagerService(PagerService pagerService) { this.pagerService = pagerService; } }

com.sterning.books.web.actions.BookAction.java

1)、默认情况下,当请求bookAction.action发生时(这个会在后面的Spring配置文件中见到的)Struts运行时(Runtime)根据struts.xml里的Action映射集(Mapping),实例化com.sterning.books.web.actions.BookAction类,并调用其execute方法。当然,我们可以通过以下两种方法改变这种默认调用。这个功能(Feature)有点类似Struts 1.x中的LookupDispathAction
classes/sturts.xml中新建Action,并指明其调用的方法;
访问Action时,在Action名后加上“!xxx”xxx为方法名)。
2)、细心的朋友应该可能会发现com.sterning.books.web.actions.BookAction.javaAction方法(execute)返回都是SUCCESS。这个属性变量我并没有定义,所以大家应该会猜到它在ActionSupport或其父类中定义。没错,SUCCESS在接口com.opensymphony.xwork2.Action中定义,另外同时定义的还有ERROR, INPUT, LOGIN, NONE
此外,我在配置Action时都没有为result定义名字(name),所以它们默认都为success。值得一提的是Struts 2.0中的result不仅仅是Struts 1.xforward的别名,它可以实现除forward外的很激动人心的功能,如将Action输出到FreeMaker模板、Velocity模板、JasperReports和使用XSL转换等。这些都过result里的type(类型)属性(Attribute)定义的。另外,您还可以自定义result类型。
3)、使用Struts 2.0,表单数据的输入将变得非常方便,和普通的POJO一样在Action编写GetterSetter,然后在JSPUI标志的name与其对应,在提交表单到Action时,我们就可以取得其值。
4)、Struts 2.0更厉害的是支持更高级的POJO访问,如this.getBook().getBookPrice()private Books book所引用的是一个关于书的对象类,它可以做为一个属性而出现在BookActoin.java类中。这样对我们开发多层系统尤其有用。它可以使系统结构更清晰。
5)、有朋友可能会这样问:“如果我要取得Servlet API中的一些对象,如requestresponsesession等,应该怎么做?这里的execute不像Struts 1.x的那样在参数中引入。”开发Web应用程序当然免不了跟这些对象打交道。在Strutx 2.0中可以有两种方式获得这些对象:非IoC(控制反转Inversion of Control)方式和IoC方式。

0
相关文章