1,使用Spring 的 DelegatingRequestProcessor 类
DelegatingRequestProcessor 继承自 org.apache.struts.action.RequestProcessor 并覆盖了里面的方法。
sturts-config.xml 中 <controller processorClass="org.springframework.web.struts.DelegatingRequestProcessor"/> 通过 <controller >来替代
org.apache.struts.action.RequestProcessor 的请求处理。
public class LoginAction extends Action { @Override public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { LoginForm formbean = (LoginForm)form; String name = formbean.getName().trim(); String password = formbean.getPassword().trim(); if("lmning".equals(name)&&"1234".equals(password)){ User user = new User(); user.setName(name); user.setPassword(password); request.setAttribute("user", user); return mapping.findForward("success"); }else return mapping.findForward("failure"); } }
public class User { private String name; private String password; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
public class LoginForm extends ActionForm { private static final long serialVersionUID = 7419866036877000754L; private String name; private String password; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
配置文件:
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.5" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/classes/applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <servlet> <servlet-name>struts</servlet-name> <servlet-class> org.apache.struts.action.ActionServlet </servlet-class> <init-param> <param-name>config</param-name> <param-value>/WEB-INF/struts-config.xml</param-value> </init-param> <init-param> <param-name>debug</param-name> <param-value>3</param-value> </init-param> <init-param> <param-name>detail</param-name> <param-value>3</param-value> </init-param> <load-on-startup>0</load-on-startup> </servlet> <servlet-mapping> <servlet-name>struts</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
struts-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" "http://struts.apache.org/dtds/struts-config_1_3.dtd">
<struts-config>
<form-beans>
<form-bean name="loginForm" type="com.lmning.form.LoginForm"></form-bean>
</form-beans>
<global-exceptions />
<global-forwards />
<action-mappings>
<action path="/login" name="loginForm" scope="request">
<forward name="success" path="/success.jsp"></forward>
<forward name="failure" path="/failure.jsp"></forward>
</action>
</action-mappings>
<!-- 把action交给spring处理 -->
<controller>
<set-property property="processorClass" value="org.springframework.web.struts.DelegatingRequestProcessor"/>
</controller>
<message-resources parameter="com.lmning.struts.ApplicationResources" />
</struts-config>
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean name="/login" class="com.lmning.LoginAction"> </bean> </beans>
I LOVE THIS ONE!
缺点:
RequestProcessor类已经被代理 如果要再实现自己的实现方式(如:编码处理)麻烦。
还必须依赖Struts的RequestProcessor类。
2,全权委托:
Action 的创建和对象的依赖注入全部由IOC容器来完成。 使用Spring的DelegatingAcionProxy来帮助实现代理的工作
org.springframework.web.struts.DelegatingActiongProxy继承于org.apache.struts.action.Action .
这种方式只需要改Struts-config.xml:
<action path="/login" name="loginForm" scope="request"
type="org.springframework.web.struts.DelegatingActionProxy">
<forward name="success" path="/success.jsp"></forward>
<forward name="failure" path="/failure.jsp"></forward>
</action>
与第一中方式相比:
<action>中 type指向的是spring 的代理类
去掉struts-config.xml中 <controller >
缺点:
type="org.springframework.web.struts.DelegatingActionProxy">
看上去都一样,而不是具体的类名
这种方式最灵活
3,最简单的整合方式:
使用Spring的ActionSupport .
Spring 的ActionSupport 继承至 org.apache.struts.action.Action
ActionSupport的子类可以或得 WebApplicationContext类型的全局变量。通过getWebApplicationContext()可以获得这个变量
<action-mappings>
<action path="/login" name="loginForm" scope="request"
type="com.lmning.LoginAction">
<forward name="success" path="/success.jsp"></forward>
<forward name="failure" path="/failure.jsp"></forward>
</action>
</action-mappings>
在Action中获取Spring的ApplicationContext
例如:
WebApplicationContext ctx = this.getWebApplicationContext();
LoginDao dao = (LoginDao) ctx.getBean("loginDao");
用的少,因为
缺点:一,Struts与Spring紧密耦合,不能改换到其他IoC容器;
二,难以使用Spring AOP特性;
三,对于需要使用DispatchAction的Struts应用无能为力。