spring+struts的集成(第二种集成方案,推荐)
spring+struts的集成(第二种集成方案)
原理:将业务逻辑对象通过spring注入到Action中,从而避免了在Action类中的直接代码查询
1、spring和struts依赖库配置
* 配置struts
--拷贝struts类库和jstl类库
--修改web.xml文件来配置ActionServlet
--提供struts-config.xml文件
--提供国际化资源文件
* 配置spring
--拷贝spring类库
--提供spring配置文件
2、因为Action需要调用业务逻辑方法,所以需要在Action中提供setter方法,让spring将业务逻辑对象注入过来
public class LoginAction extends Action {private UserManager userManager;@Overridepublic 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;}}
<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><plug-in className="org.springframework.web.struts.ContextLoaderPlugIn"> <set-property property="contextConfigLocation" value="/WEB-INF/applicationContext.xml,/WEB-INF/applicationContext-*.xml" /></plug-in>
<?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" scope="prototype"><property name="userManager" ref="userManager"/></bean> <bean id="userManager" class="com.bjsxt.usermgr.manager.UserManagerImpl"/></beans>