1.说明
strut2提供了一种非常简单的方式来实现与spring的整合
struts2通过一种“可插拨式”的插件,实现了与Spring框架的整合。
在实现应用中,只需要把struts2-spring-plugin-x.x.x.x.jar(其中的xxxx为版本号)文件拷到应用的lib下即可。
Struts2提供了两种基本的整合策略:
1. 将Action实例交给Spring容器来负责生成,管理,通过这种方式,可以充分利用Spring容器的IOC特性,提供最好的解耦;
2. 利用Spring插件的自动装配方式,当Spring插件创建Action实例之后,立即将Spring窗口中对应的业务逻辑组件注入Action实例。
2.整合方式一:
将Action实例交给Spring容器来负责生成,管理,通过这种方式,可以充分利用Spring容器的IOC特性,提供最好的解耦;
1.新建一个web工程
这个就不多说了。
2.打入jar包。
导入如图所示的jar包:
3.生成Action类及service类
结构如下:
LoginAction.java内容:
package com.user.action; import com.opensymphony.xwork2.ActionSupport; import com.user.service.IUserService; public class LoginAction extends ActionSupport{ private static final long serialVersionUID = 1L; private IUserService userService; //前台传递过来的参数 private String username; private String password; public IUserService getUserService() { return userService; } public void setUserService(IUserService userService) { this.userService = userService; } public String getUsername() { return username; } public String getPassword() { return password; } public void setUsername(String username) { this.username = username; } public void setPassword(String password) { this.password = password; } public String execute() throws Exception{ System.out.println("in execute!"); if(userService.validCheck(username, password)){ return SUCCESS; } return ERROR; } }
IUserService.java 类的内容:
package com.user.service; public interface IUserService { public boolean validCheck(String username,String password); }
UserService.java
package com.user.service.impl; import com.user.service.IUserService; public class UserService implements IUserService { @Override public boolean validCheck(String username, String password) { if (username.equals("hello") && password.equals("world")){ return true; } return false; } }
4.创建index.jsp等前端页面
结构如下:
index.jsp页面内容:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <%@taglib prefix="s" uri="/struts-tags" %> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> </head> <body> <s:form action="user/login" method="post"> <s:textfield name="username" label="用户名" key="user"></s:textfield> <s:password name="password" label="密码" key="password"></s:password> <s:submit key="login" value="提交"></s:submit> </s:form> </body> </html>
success.jsp页面:
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!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=utf-8"> <title>Insert title here</title> </head> <body> ${username},欢迎您!登录成功! </body> </html>
error.jsp
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!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=utf-8"> <title>Insert title here</title> </head> <body> 错误页面! </body> </html>
5. 配置struts和spring的配置文件
结构如下:
说明:
在项目下新建文件夹 resource,有modules文件夹和struts.xml , spring.xml
struts2中,struts.xml需要在src目录底下。classpath中需要配置。下面会有说明。
struts.xml内容:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <!-- struts的action配置文件 --> <include file="modules/user/struts-user.xml"/> </struts>
struts-user.xml文件内容:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"> <struts> <package name="user" namespace="/user" extends="struts-default"> <!-- 注意class属性不是指向一个action,而是指向spring 的一个bean --> <action name="login" class="loginAction" > <!-- 定义逻辑视图和物理资源之间的映射 --> <result name="success">/success.jsp</result> <result name="error">/error.jsp</result> </action> </package> </struts>
spring.xml文件的内容:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <bean id="userService" class="com.user.service.impl.UserService" ></bean> <bean id="loginAction" class="com.user.action.LoginAction"> <property name="userService" ref="userService"/> </bean> </beans>
6.创建web.xml文件
在web- WEB-INF下创建web.xml文件:
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <!-- 配置spring监听器 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 初始化 --> <context-param> <param-name>contextConfigLocation</param-name> <!-- 如果有多个文件,在文件之间用英文逗号隔开 --> <!-- <param-value>/WEB-INF/applicationContext.xml,/WEB-INF/applicationContext-db.xml </param-value> --> <param-value>classpath:spring.xml;classpath:modules/**/spring*.xml</param-value> </context-param> <!-- 定义struts2的核心filter --> <filter> <filter-name>struts2</filter-name> <filter-class> org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <!-- 让struts定义的核心filter拦截所有请求 --> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- 项目欢迎界面 --> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
7.Build Path
将第一步中的lib中的jar包加入到classpath中。
将resource文件下设置为src(struts.xml需要在src目录下)
设置输出目录
8.发布项目,启动Tomcat。
3.整合方式二:
利用Spring插件的自动装配方式,当Spring插件创建Action实例之后,立即将Spring窗口中对应的业务逻辑组件注入Action实例。
使用自动装配方式整合
(1)指定自动装配的策略,让Spring 自动管理Bean与Bean之间的依赖有关系,无需使用ref显式指定依赖Bean ,Spring容器会自动检查XML配置文件内容,为主调Bean 注入依赖Bean。Spring 提供了3种装配策略,通过修改struts.objectFactory.spring.autoWire常量指定,可接受的3个值:
1.name:根据属性名自动装配,这个是默认值。
2.type根据属性类型自动装配
3.auto自动检测需要使用哪种自动装配方式
与方式一的区别是:
1.struts-user.xml配置文件:class属性改成完整类名com.user.action.LoginAction
<struts> <package name="user" namespace="/user" extends="struts-default"> <action name="login" class="com.user.action.LoginAction" > <!-- 定义逻辑视图和物理资源之间的映射 --> <result name="success">/success.jsp</result> <result name="error">/error.jsp</result> </action> </package> </struts>
2.spring.xml文件中,无需再声明action的bean了
<beans> <bean id="userService" class="com.user.service.impl.UserService" ></bean> <!-- <bean id="loginAction" class="com.user.action.LoginAction"> <property name="userService" ref="userService"/> </bean>--> </beans>
参考:http://blog.chinaunix.net/uid-8481993-id-2460742.html