使用 Spring 的 ActionSupport 類整合 Structs1.X, 使用 Spring 的 DelegatingRequestProcessor 覆蓋 Struts 的 RequestProcessor,將 Struts Action 管理委託給 Spring 框架
裝載應用環境:
無論您使用哪種技術,都需要使用 Spring 的 ContextLoaderPlugin 爲 Struts 的 ActionServlet 裝載 Spring 應用程序環境
在struts-config.xml 文件尾處添加該插件:
< plug-in className= "org.springframework.web.struts.ContextLoaderPlugIn">
< set-property property= "contextConfigLocation" value="/WEB-INF/classes/applicationContext.xml"/>
< /plug-in>
第一種:使用Spring的ActionSupport
該方法: 簡單快捷,但會導致struts和spring耦合在一起,如果要移值struts應用程序要重寫代碼.
例如:
public class ActionName extends ActionSupport { //繼承自actionsupport
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {
DynaActionForm searchForm = (DynaActionForm) form;//使用動態的actionForm
String isbn = (String) searchForm.get("isbn");
//the old fashion way
//BookService bookService = new BookServiceImpl();
ApplicationContext ctx = getWebApplicationContext(); //org.springframework.web.struts.ActionSupport 類提
//供了一個 getWebApplicationContext() 方法
BookService bookService =
(BookService) ctx.getBean("bookService"); //查找名爲bookService的Spring bean
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");
}
}
第二種:覆蓋 RequestProcessor
該方法使用 org.springframework.web.struts.DelegatingRequestProcessor 類來覆蓋 Struts 的 RequestProcessor 處理程序,通過
Spring 的 DelegatingRequestProcessor 進行整合,看下面的struts-config.xml文件的主要配置部分:
< form-beans>
< form-bean name="searchForm"
type="org.apache.struts.validator.DynaValidatorForm">
< form-property name="isbn" type="java.lang.String"/>
< /form-bean>
< /form-beans>
< action path="/searchSubmit"
type="com.iwtxokhtd.books.actions.SearchSubmit"
input="/searchEntry.do"
validate="true"
name="searchForm">
< forward name="success" path="/WEB-INF/jsp/detail.jsp"/>
< forward name="failure" path="/WEB-INF/jsp/search.jsp"/>
< /action>
< controller processorClass="org.springframework.web.struts.
DelegatingRequestProcessor"/>
< plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
< set-property property="contextConfigLocation" value="/WEB-INF/classes/applicationContext.xml"/>
< /plug-in>
然後在spring配置文件applicationContext.xml文件中進行bean配置:
< beans>
< bean id="bookService" class="com.iwtxokhtd.books.business.BookServiceImpl"/>
< bean name="/searchSubmit" //一定要與struts-config.xml文件中的action中的path名一樣
class="com.iwtxokhtd.books.actions.SearchSubmit"> //指名所在的action
< property name="bookService">//dao
< ref bean="bookService"/>//dao Impl 類
< /property>
< /bean>
< /beans>
此外,一定要在action類中加入 private BookService bookService;並生成相應的getter和setter方法
此方法比第一種要好,但如果您使用一個不同的 RequestProcessor,則需要手動整合 Spring 的 DelegatingRequestProcessor,添加的代碼
會造成維護的麻煩並且將來會降低您的應用程序的靈活性。
第三種:將動作管理委託給 Spring
這裏列出struts-config.xml中的主要部分:
< action path="/searchSubmit"
type="org.springframework.web.struts.DelegatingActionProxy"
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>
< plug-in
className="org.springframework.web.struts.ContextLoaderPlugIn">
< set-property property="contextConfigLocation" value="/WEB-INF/classes/application.xml"/>
< /plug-in>
然後在applicatinContext.xml文件中配置:
< beans>
< bean id="bookService" class="ca.nexcel.books.business.BookServiceImpl"/>
< bean name="/searchSubmit"
class="com.iwtxokhtd.books.actions.SearchSubmit">
< property name="bookService">//dao
< ref bean="bookService"/>//dao impl
< /property>
< /bean>
< /beans>
此方法是三種方法中最好的