import org. springframework. web. struts. ActionSupport import ca excel. books. beans. Book import ca. nexcel books business Book Service public class SearchSubmit extends ActionSupport (1) public Action Forward execute( Action Mapping mapping Action Form form HttpservletreqUest request HttpservletresPonse response throws IOException, ServletException I DynaActionForm searchForm =(DynaAction Form) form String isbn =(String) searchForm get(isbn") //the old fashion way //BookService book Service new BookServiceImp1o ApplicationContext ctx getWebApplication Contexto:(2) Bookservice book service (Book Service) ctx getBean ("bo vIce Book book bookService. read (isbn trimo) f (null = book)I ActionErrors errors new ActionErrors o: errors. add(Action Errors. GLOBAL ERROR, new ActionError saveErrors (request, errors return mapping. findForward(failure") request setAttribute( book", book)
import org.springframework.web.struts.ActionSupport; import ca.nexcel.books.beans.Book; import ca.nexcel.books.business.BookService; public class SearchSubmit extends ActionSupport { (1) public ActionForward execute( ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { DynaActionForm searchForm = (DynaActionForm) form; String isbn = (String) searchForm.get("isbn"); //the old fashion way //BookService bookService = new BookServiceImpl(); ApplicationContext ctx = getWebApplicationContext(); (2) BookService bookService = (BookService) ctx.getBean("bookService"); (3) Book book = bookService.read(isbn.trim()); if (null == book) { ActionErrors errors = new ActionErrors(); errors.add(ActionErrors.GLOBAL_ERROR,new ActionError ("message.notfound")); saveErrors(request, errors); return mapping.findForward("failure") ; } request.setAttribute("book", book);
return mapping. findForward(success") 在(1)处,通过从 Spring的 Action Support类而不是 Struts的 Action类进行扩展, 创建了一个新的 Action。在(2)处,使用 getWebApplication Context0方法获得一个 Application Context。为了获得业务服务,使用在(2)处获得的环境在(3)处査找一个 Spring beane 这种技术很简单并且易于理解。不幸的是,它将 Struts动作与 Spring框架耦合在 起。如果您想替换掉 Spring,那么您必须重写代码。并且,由于 Struts动作不在 Spring 的控制之下,所以它不能获得 Spring aoP的优势。当使用多重独立的 Spring环境时, 这种技术可能有用,但是在大多数情况下,这种方法不如另外两种方法合适。 窍门2.覆盖 RequestProcessor 将 Spring从 Struts动作中分离是一个更巧妙的设计选择。分离的一种方法是使用 org. springframework.web. struts. DelegatingRequestProcessor类来覆盖 Struts的 RequestProcessor处理程序,如清单2所示: 清单2.通过 Spring的 DelegatingRequestProcessor进行整合 <?xml version="1.0 encoding="IS0-8859-1" ?> <! DOCTYPE struts-config PUBLIC -//Apache Software Foundation//DTD Struts Configuration 11//EN http://jakarta.apacheorg/struts/dtds/struts-config_1l.dtd> <struts-config> <form-beans> <form-bean name=" searchForm type="org. apache. struts. validator. DynavalidatorForm"> <form-property name="isbn" type="java. lang. String"/>
return mapping.findForward("success"); } } 在 (1) 处,通过从 Spring 的 ActionSupport 类而不是 Struts 的 Action 类进行扩展, 创建了一个新的 Action。在 (2) 处,使用 getWebApplicationContext() 方法获得一个 ApplicationContext。为了获得业务服务,使用在 (2) 处获得的环境在 (3) 处查找一个 Spring bean。 这种技术很简单并且易于理解。不幸的是,它将 Struts 动作与 Spring 框架耦合在 一起。如果您想替换掉 Spring,那么您必须重写代码。并且,由于 Struts 动作不在 Spring 的控制之下,所以它不能获得 Spring AOP 的优势。当使用多重独立的 Spring 环境时, 这种技术可能有用,但是在大多数情况下,这种方法不如另外两种方法合适。 窍门 2. 覆盖 RequestProcessor 将 Spring 从 Struts 动作中分离是一个更巧妙的设计选择。分离的一种方法是使用 org.springframework.web.struts.DelegatingRequestProcessor 类来覆盖 Struts 的 RequestProcessor 处理程序,如清单 2 所示: 清单 2. 通过 Spring 的 DelegatingRequestProcessor 进行整合 <?xml version="1.0" encoding="ISO-8859-1" ?> <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd"> <struts-config> <form-beans> <form-bean name="searchForm" type="org.apache.struts.validator.DynaValidatorForm"> <form-property name="isbn" type="java.lang.String"/>
</form-bean> </form-beans> Kglobal-forwards type="org. apache. struts action. Action Forward"> <forward name= welcome path=/welcome. do/> Forward name= searchEntry path="/searchEntry do/> <forward name= searchSubmit path="/searchSubmit do/> </global-forwards> < action- mappings〉 Action path="/welcome" forward="/WEB-INF/pages/welcome. htm"/> Kaction path=/searchEntry" forward="/ WEB-INF/pages/search. jsp"/> Action path="/ searchsubml type="ca. nexcel books actions. SearchSubmit t=/se validate= true name="searchFor"> <forward name="success"path"/WEB-INF/pages/detail. jsp/> <forward name="failure path=/WEB-INF/pages/search. jsp"/> C/action> </action-mappings> message-resources parameter="ApplicationResources"/> Controller processorClass="org. springframework. web. struts DelegatingRequestProcessor"/>(1) Kplug-in className="org. apache struts. validator. ValidatorPlugIn") <set-property property=" pathnames value="/WEB-INF/validator-rules. xml, /WEB-INF/validation xm1"/> </plug-in> <plug-in className="org. springframework. web. struts. ContextLoaderPlugIn"> <set-property property="csntextConfigLocation" value="/WEB-INF/beans. xm1"/> </plug-in>
</form-bean> </form-beans> <global-forwards type="org.apache.struts.action.ActionForward"> <forward name="welcome" path="/welcome.do"/> <forward name="searchEntry" path="/searchEntry.do"/> <forward name="searchSubmit" path="/searchSubmit.do"/> </global-forwards> <action-mappings> <action path="/welcome" forward="/WEB-INF/pages/welcome.htm"/> <action path="/searchEntry" forward="/WEB-INF/pages/search.jsp"/> <action path="/searchSubmit" type="ca.nexcel.books.actions.SearchSubmit" input="/searchEntry.do" validate="true" name="searchForm"> <forward name="success" path="/WEB-INF/pages/detail.jsp"/> <forward name="failure" path="/WEB-INF/pages/search.jsp"/> </action> </action-mappings> <message-resources parameter="ApplicationResources"/> <controller processorClass="org.springframework.web.struts. DelegatingRequestProcessor"/> (1) <plug-in className="org.apache.struts.validator.ValidatorPlugIn"> <set-property property="pathnames" value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml"/> </plug-in> <plug-in className="org.springframework.web.struts.ContextLoaderPlugIn"> <set-property property="csntextConfigLocation" value="/WEB-INF/beans.xml"/> </plug-in>
</struts-config) 利用了< controller>标记来用 DelegatingRequestProcessor覆盖默认的 Struts RequestProcessor。下一步是在我的 Spring配置文件中注册该动作,如清单3所示: 清单3.在 Spring配置文件中注册一个动作 <?xml version="1.0 encoding="UTF-8?> < doCtYPE beans PUBLIC "-//SPRING//DTD BEAN//EN http://www.springframeworkorg/dtd/spring-beans.dtd> <bean id=bookService" class="ca excel. books. business Book ServiceImpl/> bean name="/searchSubmit class="ca excel. books. actions. SearchSubmit" >(1) Property name=bookService"> Kref bean="book Service"/> </property) </bean> </beans> 注意:在(1)处,使用名称属性注册了一个bean,以匹配 struts-config动作映射名称 SearchSubmit动作揭示了一个 JavaBean属性,允许 Spring在运行时填充属性,如清单 4所示 清单4.具有 JavaBean属性的 Struts动作 package ca excel. books. actions import java. io IOException import javax. servlet ServletException importjavax.servlethttpHttpservletrequEst
</struts-config> 利用了 <controller> 标记来用 DelegatingRequestProcessor 覆盖默认的 Struts RequestProcessor。下一步是在我的 Spring 配置文件中注册该动作,如清单 3 所示: 清单 3. 在 Spring 配置文件中注册一个动作 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <bean id="bookService" class="ca.nexcel.books.business.BookServiceImpl"/> <bean name="/searchSubmit" class="ca.nexcel.books.actions.SearchSubmit"> (1) <property name="bookService"> <ref bean="bookService"/> </property> </bean> </beans> 注意:在 (1) 处,使用名称属性注册了一个 bean,以匹配 struts-config 动作映射名称。 SearchSubmit 动作揭示了一个 JavaBean 属性,允许 Spring 在运行时填充属性,如清单 4 所示: 清单 4. 具有 JavaBean 属性的 Struts 动作 package ca.nexcel.books.actions; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest;
importjavax.servlethttpHttpservletrespOnse import org. apache struts action Action import org. apache struts action. Actionerror import org. apache struts action. ActionErrors import org. apache. struts action. ActionForm; import org. apache struts action. Action Forward port org. apache. struts action action mapping import org. apache. struts. action dynaAction Form import ca excel. books. beans. Book import ca. nexcel books business. BookService public class SearchSubmit extends Action I private bookService bookService public bookService ok Service I return book ervice public void setBook Service(Book Service book Service)(1) this, book Service bookservice public Action Forward execute( ActionMapping mapping Action Form form HttpservletreqUest request HttpservletrespOnse response throws IOException, ServletException I DynaActionForm searchForm =(DynaActionForm) form String isbn =(String) searchForm get(isbn") Book book getBookServiceO. read (isbn trimO):(2)
import javax.servlet.http.HttpServletResponse; import org.apache.struts.action.Action; import org.apache.struts.action.ActionError; import org.apache.struts.action.ActionErrors; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; import org.apache.struts.action.DynaActionForm; import ca.nexcel.books.beans.Book; import ca.nexcel.books.business.BookService; public class SearchSubmit extends Action { private BookService bookService; public BookService getBookService() { return bookService; } public void setBookService(BookService bookService) { (1) this.bookService = bookService; } public ActionForward execute( ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { DynaActionForm searchForm = (DynaActionForm) form; String isbn = (String) searchForm.get("isbn"); Book book = getBookService().read(isbn.trim()); (2)