Struts要与Spring结合使用,主要的方法就是让Struts知道Spring的存在,以让管理相关的组件,避免在程序中直接编写组件的依赖关系建立。
1.HelloAction.java
Struts的Action直接继承它自己的抽象Action类,但让Spring来管理Struts的Action对象,让Action对象也成为Spring管理的Bean,这么一来,就可以直接使用依赖注入的方式注入相依赖对象。
package cn.com.boyang;
import java.util.*;
import javax.servlet.http.*;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
public class HelloAction extends Action {
private UserChecker userChecker;
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
String username = request.getParameter("user");
username = this.getUserChecker().check(username);
Map model = new HashMap();
model.put("username", username);
request.setAttribute("userInfo", model);
return mapping.findForward("helloUser");
}
public UserChecker getUserChecker() {
return userChecker;
}
public void setUserChecker(UserChecker userChecker) {
this.userChecker = userChecker;
}
}
2.UserChecker.java
模拟业务层的权限检查对象,你要在Struts的Action中使用到UserChecker 的实例,这可以交由Spring来为你依赖注入,可以将HelloAction与UserChecker 的实例交由Spring管理。
package cn.com.boyang;
public class UserChecker {
public String check(String username) {
if(username != null || !"".equals(username)) {
return username;
}
else {
return "guest";
}
}
}
3.beans-config.xml
Action的实例现在纳入Spring的管理,那么Struts在分配请求时,要有一个中间代理机制,在请求要分配至指定的Action之前,先分配至代理对象,由代理对象通知Spring所管理的Action实惠来处理请求并将处理结果返回代理对象,再由代理对象返回给Struts,这;可以在struts-config.xml中使用org.springframework.web.struts.DelegatingActionProxy的实例来作代理。
<?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="userChecker"
class="cn.com.boyang.UserChecker"/>
<bean name="/hello"
class="cn.com.boyang.HelloAction">
<property name="userChecker">
<ref bean="userChecker"/>
</property>
</bean>
</beans>
4.struts-config.xml
红色背景用来配置其它的Bean配置文件。
注意:下面的path的属性必须设置成"/hello",DelegatingActionProxy是利用这个来找到Action实例。其实,beans-config.xml中HelloAction的name属性设置的是"/hello",这两都必须一致。下面绿色背景的部分,添加来是用Spring的"org.springframework.web.struts.DelegatingRequestProcessor"来取代Struts的RequestProcessor.(org.springframework.web.struts.DelegatingRequestProcessor ==> RequestProcessor)(ActionServlet -> web.xml, RequestProcessor -> struts-config.xml)
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd">
<struts-config>
<action-mappings>
<action
path="/hello"
type="org.springframework.web.struts.DelegatingActionProxy">
<forward
name="helloUser"
path="/jsp/hello.jsp"/>
</action>
</action-mappings>
<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation"
value="/WEB-INF/beans-config.xml, /WEB-INF/others-config.xml"/>
</plug-in>
<controller "org.springframework.web.struts.DelegatingRequestProcessor" />
</struts-config>
5.web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<!-- Standard Action Servlet Configuration -->
<servlet>
<servlet-name>action</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>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- Standard Action Servlet Mapping -->
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
6.hello.jsp
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<html>
<head>
<meta http-equiv="Content-Type"
content="text/html; charset=UTF-8">
<title>Struts Demo</title>
</head>
<body>
<H1>Hello, ${userInfo["username"]} !</H1>
</body>
</html>