使用了struts和spring一段时间.但是对其中他们的整合也用了好几次.就这次机会总结下经验并整理下思绪.
整合方式1: 最原始而易懂的方式: Action继承spring提供的类org.springframework.web.struts.MappingDispatchActionSupport Action中的代码:
Java代码
- public class UserAction extends MappingDispatchActionSupport {
- public ActionForward login(ActionMapping mapping, ActionForm form,
- HttpServletRequest request, HttpServletResponse response)
- throws Exception {
- String uname = request.getParameter("uname");
- String upass = request.getParameter("upass");
- //使用其自带的一个方法实例化ApplicationContext对象
- ApplicationContext context = this.getWebApplicationContext();
- userService=(UserService)context.getBean("userService");
- User user = userService.findByName(uname, upass);
- if (user==null) {
- request.setAttribute("error", "对不起,您输入的用户名或者密码错误!");
- return mapping.findForward("failure");
- } else {
- request.getSession().setAttribute("user", user);
- return mapping.findForward("success");
- }
- }
- }
Java代码
- <action path="/login" parameter="login"
- type="com.addresslist.action.UserAction" scope="request">
- <forward name="success" path="/page/index.htm"></forward>
- </action>
Java代码
- <!--
- //可以选择使用ContextLoaderServle
- <servlet>
- <servlet-name>springInitServlet</servlet-name>
- <servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>
- // 如果使用要注意这里设置开启的优先级要比Struts的ActionServlet高
- <load-on-startup>2</load-on-startup>
- </servlet>
- -->
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>/WEB-INF/applicationContext.xml</param-value>
- </context-param>
- <!-- 使用ContextLoaderListener -->
- <listener>
- <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
- </listener>
Java代码
- <action path="/login" parameter="login"
- type="com.addresslist.action.UserAction" scope="request">
- <forward name="success" path="/page/index.htm"></forward>
- </action>
- <!--
- 可以选择使用
- <plug-in
- className="org.springframework.web.struts.ContextLoaderPlugIn">
- <set-property property="contextConfigLocation"
- value="/WEB-INF/applicationContext.xml" />
- </plug-in>
- -->
Java代码
- public class UserAction extends MappingDispatchAction {
- public UserService getFileService(){
- //这里使用了WebApplicationContextUtils工具类实例化ApplicationContext
- ApplicationContext ac=WebApplicationContextUtils.getWebApplicationContext(this.getServlet().getServletContext());
- return (UserService) ac.getBean("userService");
- }
- public ActionForward login(ActionMapping mapping, ActionForm form,
- HttpServletRequest request, HttpServletResponse response)
- throws Exception {
- String uname = request.getParameter("uname");
- String upass = request.getParameter("upass");
- User user = getFileService().findByName(uname, upass);
- if (user==null) {
- request.setAttribute("error", "对不起,您输入的用户名或者密码错误!");
- return mapping.findForward("failure");
- } else {
- request.getSession().setAttribute("user", user);
- return mapping.findForward("success");
- }
- }
- }
Java代码
- <struts-config>
- <data-sources />
- <form-beans />
- <global-exceptions />
- <global-forwards />
- <action-mappings>
- <!--这里使用了org.springframework.web.struts.DelegatingAction-->
- <action path="/login" parameter="login" type="org.springframework.web.struts.DelegatingActionProxy">
- <forward name="success" path="/page/index.htm"/>
- <forward name="failure" path="/page/fail.htm"/>
- </action>
- </action-mappings>
- <message-resources
- parameter="com.addresslist.properties.ApplicationResources" />
- <!--这里使用ContextLoaderPlugIn建立Spring的环境-->
- <plug-in
- className="org.springframework.web.struts.ContextLoaderPlugIn">
- <set-property property="contextConfigLocation"
- value="/WEB-INF/applicationContext.xml" />
- </plug-in>
Java代码
- public class UserAction extends MappingDispatchAction {
- //使用普遍依赖注入方式
- UserService userService;
- public void setUserService(UserService userService) {
- this.userService = userService;
- }
- public ActionForward login(ActionMapping mapping, ActionForm form,
- HttpServletRequest request, HttpServletResponse response)
- throws Exception {
- String uname = request.getParameter("uname");
- String upass = request.getParameter("upass");
- //直接使用userService
- User user = userService.findByName(uname, upass);
- if (user==null) {
- request.setAttribute("error", "对不起,您输入的用户名或者密码错误!");
- return mapping.findForward("failure");
- } else {
- request.getSession().setAttribute("user", user);
- return mapping.findForward("success");
- }
- }
- }
Java代码
- <bean name="/login" class="com.addresslist.action">
- <property name="userService" ref="userService"></property>
- </bean>
Java代码
- <action-mappings>
- <action path="/login" parameter="login" type="com.addresslist.action.UserAction">
- <forward name="success" path="/page/index.htm"/>
- <forward name="failure" path="/page/fail.htm"/>
- </action>
- <!--下面controller配置是核心-->
- <controller processorClass="org.springframework.web.struts.
- DelegatingRequestProcessor"/>
- <plug-in
- className="org.springframework.web.struts.ContextLoaderPlugIn">
- <set-property property="contextConfigLocation"
- value="/WEB-INF/applicationContext.xml" />
- </plug-in>