我用的整合方式,是通过Maven进行管理的
首先是通过Maven将Spring和Struts2需要的包准备好,可以通过 http://search.maven.org/找到需要的包
配置spring.xml和struts.xml
spring.xml
<?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:context="http://www.springframework.org/schema/context" xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
">
<!-- 引入属性文件 -->
<context:property-placeholder location="classpath:config.properties" />
<!-- 自动扫描dao和service包(自动注入) -->
<context:component-scan base-package="com.fonxian.dao,com.fonxian.service,com.fonxian.action" />
</beans>
struts.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<!-- 指定由spring负责action对象的创建 -->
<constant name="struts.objectFactory" value="spring" />
<!-- 所有匹配*.action的请求都由struts2处理 -->
<constant name="struts.action.extension" value="action" />
<!-- 是否启用开发模式 -->
<constant name="struts.devMode" value="true" />
<!-- struts配置文件改动后,是否重新加载 -->
<constant name="struts.configuration.xml.reload" value="true" />
<!-- 设置浏览器是否缓存静态内容 -->
<constant name="struts.serve.static.browserCache" value="false" />
<!-- 请求参数的编码方式 -->
<constant name="struts.i18n.encoding" value="utf-8" />
<!-- 每次HTTP请求系统都重新加载资源文件,有助于开发 -->
<constant name="struts.i18n.reload" value="true" />
<!-- 文件上传最大值 -->
<constant name="struts.multipart.maxSize" value="104857600" />
<!-- 让struts2支持动态方法调用 -->
<constant name="struts.enable.DynamicMethodInvocation" value="true" />
<!-- Action名称中是否还是用斜线 -->
<constant name="struts.enable.SlashesInActionNames" value="false" />
<!-- 允许标签中使用表达式语法 -->
<constant name="struts.tag.altSyntax" value="true" />
<!-- 对于WebLogic,Orion,OC4J此属性应该设置成true -->
<constant name="struts.dispatcher.parametersWorkaround" value="false" />
<package name="basePackage" extends="struts-default">
</package>
</struts>
以及配置web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 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_3_0.xsd">
<display-name>ssh01</display-name>
<!-- spring配置文件位置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring.xml</param-value>
</context-param>
<!-- Struts2配置 -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
<!-- spring监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 修复数据库监听器 -->
<welcome-file-list>
<welcome-file>/index.jsp</welcome-file>
</welcome-file-list>
</web-app>
新建一个类UserAction测试一下
@Namespace()是Struts2的注解,是命名空间的意思,使用/的话,例如我的项目名叫ssh,我访问的时候可以通过
localhost:8080/ssh/userAction!test.action
如果是@Namespace(“/demo”),需要输入的路径是localhost:8080/ssh/demo/userAction!test.action,才可以访问
@ParentPackage("basePackage")
@Namespace("/")
@Action(value="userAction")
public class UserAction {
/**
* Logger for this class
*/
private static final Logger logger = Logger.getLogger(UserAction.class);
UserServerI userService;
public UserServerI getUserService() {
return userService;
}
@Autowired
public void setUserService(UserServerI userService) {
this.userService = userService;
}
public void test(){
logger.info("love program");
// ApplicationContext ac = WebApplicationContextUtils.getWebApplicationContext(ServletActionContext.getServletContext());
// UserServerI userService = (UserServerI)ac.getBean("userService");
userService.test();
}
}
UserServiceImp
package com.fonxian.service;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
@Service(value="userService")
public class UserServerImp implements UserServerI {
/**
* Logger for this class
*/
private static final Logger logger = Logger.getLogger(UserServerImp.class);
@Override
public void test() {
// TODO Auto-generated method stub
logger.info("时光一去不复返");
}
}
运行成功则表明strut和spring整合成功。