以前培训的时候讲到过这个,整合正常的做法是导入struts2-spring-plugin.jar包,然后在struts.xml里面配置action标签的class属性时直接写创建action类的bean id的名字,而不是直接写类的路径,当然new action类是通过spring来创建bean的(不管是注解方式还是xml方式),但是实验结果是就算不导入struts2-spring-plugin.jar包,struts.xml里面配置action标签的class属性时直接写类的路径,并且也不用spring来new action类也是可以将struts2和spring整合起来的,下面我贴下一个实验代码:
(没有导入struts2-spring-plugin.jar)
applicationContext.xml:
<?xml version="1.0" encoding="UTF-8"?>
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
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
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
<!-- 设置需要进行Spring注解扫描的类包 -->
struts2.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">
<package name="root" namespace="/" extends="struts-default">
<action name="user" class="com.niit.test.UserAction" >
<result name="res">/res.jsp</result>
</action>
</package>
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
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">
index.jsp
struts2
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
struts2
/*
<!-- 定义Spring配置文件位置 -->
contextConfigLocation
classpath:applicationContext.xml
<!-- 对Spring容器进行实例化 -->
org.springframework.web.context.ContextLoaderListener
index.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
111
res.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">