怎么实现struts2和spring的整合
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了怎么实现struts2和spring的整合相关的知识,希望对你有一定的参考价值。
第一步创建spring配置文件applicationContext.xml。该配置负责导入处理业务bean和持久化bean的配置,添加对controller层的action bean管理的支持。此文件一般放在WEB-INF文件夹下,如下:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 导入spring配置 --> <import resource="spring-service.xml" /> <import resource="spring-dao.xml" /> <!-- 打开组件扫描controller,将action对象管理起来 --> <context:component-scan base-package="com.entor.rams.actions"> <!-- 只对@Controller --> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" /> </context:component-scan> </beans>
第二步配置struts2的核心配置struts.xml;负责处理controller层的action bean以及前端控制功能,声明action对象的管理交给spring容器。
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN" "http://struts.apache.org/dtds/struts-2.5.dtd"> <struts> <!-- 常量 --> <!-- 开发模式打开 --> <constant name="struts.devMode" value="true" /> <!-- 声明action对象的管理交给spring容器 --> <constant name="struts.objectFactory" value="spring"></constant> <!--通过ACTION的限制得到的结果页面路径 --> <constant name="struts.convention.result.path" value="/WEB-INF/jsps/"></constant> <!-- action配置: 包 命名空间 action, result等 --> <!-- <package name="zijiding" extends="struts-default" namespace="/"> name表示请求的url:/rams/user_* class表示请求的处理action对象 method表示处理请求的方法名,{1}与name中第一次出现的通配符关联 <action name="user_*" class="userAction" method="{1}"> result表示响应的结果,此处响应webapp根目录下面的index.jsp页面 <result name="success">/WEB-INF/jsps/result.jsp</result> </action> --> <!-- </package> --> </struts>
第三步配置Struts-Spring集成设置web.xml, 该配置主要
1.配置web监听器ContextLoaderListener 监听spring上下文对象;
2.初始化Spring容器,加载spring核心配置文件;
3.配置struts过滤器,接管所有的请求。如下所示:
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>RAMSMY</display-name> <!-- 首页 --> <welcome-file-list> <welcome-file>/index</welcome-file> </welcome-file-list> <!-- web监听 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 初始化spring容器,加载spring核心配置文件 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/configs/applicationContext.xml</param-value> </context-param> <!-- 配置struts过滤器,接管所有的请求 --> <!-- 默认加载classpath路径下面的struts.xml --> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
主要配置文件已完成后新建,创建一个User.java类,提供get set 方法。
然后写好dao层和service层。新建action类UserAction.java,如下:
package com.entor.rams.actions; import java.io.Serializable; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; import org.apache.struts2.ServletActionContext; import org.apache.struts2.convention.annotation.Action; import org.apache.struts2.convention.annotation.Result; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import com.entor.rams.domain.User; import com.entor.rams.service.UserService; import com.entor.rams.utils.common.RamsConstants; import com.opensymphony.xwork2.ActionSupport; import com.opensymphony.xwork2.ModelDriven; @Controller public class UserAction extends ActionSupport implements ModelDriven<User>,Serializable{ private static final long serialVersionUID =483187020462134967L; @Autowired private UserService userService; private User user; public User getUser() { return user; } @Override public User getModel() { if(user == null) this.user =new User(); return user; } /** * 处理请求首页 * @return */ @Action(value = "index", className = "userAction" , results = // @Result(name = "success", location = "index.jsp")) public String index(){ return SUCCESS; } /** * 处理登陆请求 * @return */ @Action(value="user_login",className="userAction",results={ @Result(name = "success",location = "result.jsp"),// @Result(name = "fail",location = "index.jsp")}) public String login(){ //取出请求和会话对象 HttpServletRequest request = ServletActionContext.getRequest(); HttpSession session = request.getSession(false); User u = userService.fingByLoginNameAndPassword(this.user.getLoginName(), this.user.getPassword()); //用户登陆成功 if(u !=null){ this.user = u; session.setAttribute(RamsConstants.USER_SESSION, user); return SUCCESS; } //用户登陆失败 else{ String errorMsg = "用户名或密码错误"; request.setAttribute("errorMsg", errorMsg); return RamsConstants.FAIL; } } }
最后测试结果如下:
以上是关于怎么实现struts2和spring的整合的主要内容,如果未能解决你的问题,请参考以下文章
请问spring的quartz怎么和struts2整合起来啊。最好详细些。新手,谢谢了
Struts2 Spring JPA 整合时报错:No bean named 'entityManagerFactory' is defined ,请问各位是怎么解决