1,添加spring支持
2,添加hibernate支持
3,添加struts支持
4,在web.xml里添加监听器。作用是在系统启动时执行该Listener,以读取Spring的xml配置文件。
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext*.xml</param-value>
</context-param>
5,在web.xml里添加上下文参数,该参数指定了哪些SpringXML配置文件要被加载
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
*以上两步是struts与spring的松耦合方式。可以使用以下片段去调用spring的bean对象
WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationConte(servletContext);//获得引用
UserBean userBean=(UserBean)ctx.getBean("userBean");
*以下是struts与spring更深入的紧耦合配置
6,在struts-config.xml里配置ContextLoaderPlugin插件,来加载Spring配置(作用和在web.xml里配置一样)
<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation"
value="/WEB-INF/action-servlet.xml,/WEB-INF/applicationContext.xml"/>
</plug-in>
7,在struts-config.xml里配置DelegatingRequestProcessor,需要重载<controller>元素的processorClass属性
<controller>
<set-property property="processorClass"
value="org.springframework.web.struts.DelegatingRequestProcessor"/>
</controller>
*如果已有一个自定义的RequestProcessor,这样就不能再使用DelegatingRequestProcessor了。此时可以指定action的type是DelegatingActionProxy
<action path="/user" type="org.springframework.web.struts.DelegatingActionProxy"
name="userForm" scope="request" validate="false" parameter="method">
<forward name="list" path="/userList.jsp"/>
<forward name="edit" path="/userForm.jsp"/>
</action>
第三种struts+spring的方式:继承Spring的ActionSupport类,包括ActionSupport,DispatchAcitonSupport等
public class UserAction extends DispatchActionSupport {
public ActionForward execute(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) throws Exception {
if (log.isDebugEnabled()) {
log.debug("entering 'delete' method...");
}
WebApplicationContext ctx = getWebApplicationContext();
UserManager mgr = (UserManager) ctx.getBean("userManager");
// talk to manager for business logic
return mapping.findForward("success");
}
}
8,新建action的时候要在spring的配置文件里配置bean。
<bean name="/user" scope="prototype" autowire="byName"
class="org.example.web.UserAction"/>
9,依赖注入可以使用设值注入:
public class BuildAction extends DispatchAction {
private BuildBiz buildBiz=null;
public void setBuildBiz(BuildBiz buildBiz){
this.buildBiz=buildBiz;
}