且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Struts+Spring整合方式

更新时间:2022-10-02 23:01:32

struts与spring整合方式有三种:

方式一:通过Spring的ActionSupport类实现。

方式二:通过Sping的DelegatingRequestProcessor类覆盖struts的requestprocessor实现。

方式三:通过Sping的DelegatingActionProxy类实现,将struts action管理委托给spring框架。

相同点:以上三种方式都需要在struts的配置文件struts-config.xml中注册spring的ContextLoaderPlugIn插件。即:

<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">

<set-property property="contextConfigLocation" value="/WEB-INF/applicationContext.xml"/>

</plug-in>

不同点:

方式一:使用Spring提供的ActionSupport类,只需要使用Action继承Spring的ActionSupport类,即可实现Spring与struts的整合,使用起来简单方便,但是struts的Action与Spring耦合在一起,并且Struts的Action不在spring的控制之中,这样如果更换别的IOC容器,或想使用Spring的AOP都是比较困难的,而且如果是多个动作放在一个Action中,则这种方式就无能为了了!

方式二:使用Spring的DelegatingRequestProcessor类,需要在Struts的配置文件中,使用DelegatingRequestProcessor代替Struts的RequestProcessor类,并在Spring的配置文件中动作映射对应的Bean,可以很方便的转换而不用修改代码,这种方式的缺点是依赖于Struts的RequestProcessor类。

方式三:使用Spring的DelegatingActionProxy类,需要在Struts的配置文件中,定义动作映射的type属性为DelegatingActionProxy而不是类的实际名称,并在Spring的配置文件中定义和Struts配置文件动作映射对应的Bean,这种方式和Struts耦合性最小。

注:三者中,使用Spring的DelegatingActionProxy类来整合Spring和Struts的方式最为强大和灵活。

 

方式三的实例

login.jsp

 


  1. 代码  
  2.  
  3. <%@ page language="java" pageEncoding="ISO-8859-1"%>  
  4. <%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>   
  5. <%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>  
  6.    
  7. <html>   
  8.     <head>  
  9.         <title>JSP for LoginForm form</title>  
  10.     </head>  
  11.     <body>  
  12.         <html:form action="/login">  
  13.               
  14.             username : <html:text property="username"/><html:errors property="username"/><br/>  
  15.             password : <html:password property="password"/><html:errors property="password"/><br/>  
  16.             <html:submit/><html:cancel/>  
  17.         </html:form>  
  18.     </body>  
  19. </html> 

/StrutsLoginDemo/WebRoot/WEB-INF/struts-config.xml

 


  1. 代码  
  2.  
  3. <?xml version="1.0" encoding="UTF-8"?> 
  4. <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd"> 
  5.  
  6. <struts-config> 
  7.   <data-sources /> 
  8.   <form-beans > 
  9.     <form-bean name="loginForm" type="com.qdu.sun.struts.form.LoginForm" /> 
  10.  
  11.   </form-beans> 
  12.  
  13.   <global-exceptions /> 
  14.   <global-forwards /> 
  15.   <action-mappings > 
  16.     <action 
  17.       attribute="loginForm" 
  18.       input="/login.jsp" 
  19.       name="loginForm" 
  20.       path="/login" 
  21.       scope="request" 
  22.       type="org.springframework.web.struts.DelegatingActionProxy"> 
  23.       <forward name="failed" path="/fail.jsp" /> 
  24.       <forward name="success" path="/success.jsp" /> 
  25.     </action> 
  26.  
  27.   </action-mappings> 
  28.  
  29.   <message-resources parameter="com.qdu.sun.struts.ApplicationResources" /> 
  30.   <plug-in className="org.springframework.web.struts.ContextLoaderPlugIn"> 
  31.     <set-property property="contextConfigLocation" value="/WEB-INF/applicationContext.xml"/> 
  32.  </plug-in> 
  33.     
  34. </struts-config> 

/StrutsLoginDemo/WebRoot/WEB-INF/applicationContext.xml

 


  1. 代码  
  2.  
  3. <?xml version="1.0" encoding="UTF-8"?> 
  4. <beans 
  5.     xmlns="http://www.springframework.org/schema/beans" 
  6.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  7.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"> 
  8. <bean id="login" class="com.qdu.sun.struts.BO.LoginBO"/> 
  9.   <bean name="/login"   
  10.     class="com.qdu.sun.struts.action.LoginAction">     
  11.      <property name="loginBean"> 
  12.         <ref bean="login"/> 
  13.      </property> 
  14.   </bean> 
  15.  
  16.  
  17. </beans> 

/StrutsLoginDemo/src/com/qdu/sun/struts/action/LoginAction.java

 

  1. 代码  
  2.  
  3. /*  
  4.  * Generated by MyEclipse Struts  
  5.  * Template path: templates/java/JavaClass.vtl  
  6.  */  
  7. package com.qdu.sun.struts.action;  
  8.  
  9. import javax.servlet.http.HttpServletRequest;  
  10. import javax.servlet.http.HttpServletResponse;  
  11. import org.apache.struts.action.Action;  
  12. import org.apache.struts.action.ActionForm;  
  13. import org.apache.struts.action.ActionForward;  
  14. import org.apache.struts.action.ActionMapping;  
  15.  
  16. import com.qdu.sun.struts.BO.LoginBO;  
  17. import com.qdu.sun.struts.form.LoginForm;  
  18.  
  19. /**   
  20.  * MyEclipse Struts  
  21.  * Creation date: 08-05-2008  
  22.  *   
  23.  * XDoclet definition:  
  24.  * @struts.action path="/login" name="loginForm" input="/login.jsp" scope="request" validate="true" 
  25.  * @struts.action-forward name="failed" path="/fail.jsp" 
  26.  * @struts.action-forward name="success" path="/success.jsp" 
  27.  */  
  28. public class LoginAction extends Action {  
  29.     /*  
  30.      * Generated Methods  
  31.      */  
  32.  
  33.     /**   
  34.      * Method execute  
  35.      * @param mapping  
  36.      * @param form  
  37.      * @param request  
  38.      * @param response  
  39.      * @return ActionForward  
  40.      */  
  41.     private LoginBO loginBean;   
  42.     public LoginBO getLoginBean() {  
  43.         return loginBean;  
  44.       }  
  45.       public void setLoginBean(LoginBO loginBean) {     
  46.         this.loginBean=loginBean;  
  47.       }   
  48.  
  49.     public ActionForward execute(ActionMapping mapping, ActionForm form,  
  50.             HttpServletRequest request, HttpServletResponse response) {  
  51.         LoginForm loginForm = (LoginForm) form;// TODO Auto-generated method stub  
  52.         LoginBO login=getLoginBean();    
  53.         if(login.validate(loginForm.getUsername(), loginForm.getPassword()))  
  54.         {  
  55.             request.setAttribute("username", loginForm.getUsername());  
  56.         return mapping.findForward("success");  
  57.         }  
  58.         else  
  59.             return mapping.findForward("failed");  
  60.     }  
  61.  

 


本文转自linzheng 51CTO博客,原文链接:http://blog.51cto.com/linzheng/1080803