也许不会看见 2011-01-02
原理:将业务逻辑对象通过spring注入到Action中,从而避免了在Action类中的直接代码查询
1、spring和struts依赖库配置
*配置struts
--拷贝struts类库和jstl类库
--修改web.xml文件来配置ActionServlet
--提供struts-config.xml文件
--提供国际化资源文件
*配置spring
--拷贝spring类库
--提供spring配置文件
2、因为Action需要调用业务逻辑方法,所以需要在Action中提供setter方法,让spring将业务逻辑对象注入过来
3、在struts-config.xml文件中配置Action
*<action>标签中的type属性需要修改为org.springframework.web.struts.DelegatingActionProxy
DelegatingActionProxy是一个Action,主要作用是取得BeanFactory,然后根据<action>中的path属性值
到IoC容器中取得本次请求对应的Action
4、在spring配置文件中需要定义struts的Action,如:
<beanname="/login"class="com.bjsxt.usermgr.actions.LoginAction"scope="prototype">
<propertyname="userManager"ref="userManager"/>
</bean>
*必须使用name属性,name属性值必须和struts-config.xml文件中<action>标签的path属性值一致
*必须注入业务逻辑对象
*建议将scope设置为prototype,这样就避免了strutsAction的线程安全问题
WEB-INF下
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.4" 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"> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <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> <init-param> <param-name>debug</param-name> <param-value>2</param-value> </init-param> <init-param> <param-name>detail</param-name> <param-value>2</param-value> </init-param> <load-on-startup>2</load-on-startup> </servlet> <!-- Standard Action Servlet Mapping --> <servlet-mapping> <servlet-name>action</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <!-- <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:applicationContext-*.xml,/WEB-INF/applicationContext-*.xml</param-value> </context-param> --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:applicationContext-*.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> </web-app>
struts-config.xml
<?xml version="1.0" encoding="ISO-8859-1" ?> <!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> <form-beans> <form-bean name="loginForm" type="com.bjsxt.usermgr.forms.LoginActionForm"/> </form-beans> <action-mappings> <action path="/logininput" forward="/login.jsp" ></action> <action path="/login" type="org.springframework.web.struts.DelegatingActionProxy" name="loginForm" scope="request" > <forward name="success" path="/success.jsp"/> </action> </action-mappings> <message-resources parameter="MessageResources" /> </struts-config>
WebContent或WebRoot下
index.jsp
<%@ page language="java" contentType="text/html; charset=GB18030" pageEncoding="GB18030"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=GB18030"> <title>spring和struts的集成(第二种方案)</title> </head> <body> <h1>spring和struts的集成(第二种方案)</h1> <hr> <a href="logininput.do">登录</a> </body> </html>
login.jsp
<%@ page language="java" contentType="text/html; charset=GB18030" pageEncoding="GB18030"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=GB18030"> <title>用户登录</title> </head> <body> <form action="login.do" method="post"> 用户:<input type="text" name="username"><br> 密码:<input type="password" name="password"><br> <input type="submit" value="登录"> </form> </body> </html>
success.jsp
<%@ page language="java" contentType="text/html; charset=GB18030" pageEncoding="GB18030"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=GB18030"> <title>Insert title here</title> </head> <body> ${loginForm.username },用户登录成功! </body> </html>
src下
applicationContext-actions.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" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"> <bean name="/login" class="com.bjsxt.usermgr.actions.LoginAction" scope="prototype"> <property name="userManager" ref="userManager"/> </bean> </beans>
applicationContext-beans.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" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"> <bean id="userManager" class="com.bjsxt.usermgr.manager.UserManagerImpl"/> </beans>
MessageResources.properties 与例子1同
src.com.bjsxt.usermgr.actions下LoginAction.java
package com.bjsxt.usermgr.actions; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts.action.Action; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; import com.bjsxt.usermgr.forms.LoginActionForm; import com.bjsxt.usermgr.manager.UserManager; public class LoginAction extends Action { private UserManager userManager; @Override public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { LoginActionForm laf = (LoginActionForm)form; userManager.login(laf.getUsername(), laf.getPassword()); return mapping.findForward("success"); } public void setUserManager(UserManager userManager) { this.userManager = userManager; } }
src.com.bjsxt.usermgr.forms下 LoginActionForm.java
package com.bjsxt.usermgr.forms; import org.apache.struts.action.ActionForm; public class LoginActionForm extends ActionForm { private String username; private String password; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
src.com.bjsxt.usermgr.manager 下
UserManager.java
package com.bjsxt.usermgr.manager; public interface UserManager { public void login(String username, String password); }
UserManagerImpl.java
package com.bjsxt.usermgr.manager; public class UserManagerImpl implements UserManager { public void login(String username, String password) { System.out.println("UserManagerImpl.login() -- username=" + username); } }