ggmm,招人、找工作, 请到 www.ggmmjob.com 。。。。。
下面的是STRUTS1 与 SPRING的结合。
使用 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>
此方法是三种方法中最好的
ggmm,招人、找工作, 请到 www.ggmmjob.com 。。。。。
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/xiebinghu/archive/2009/04/09/4058977.aspx