整合Struts2框架和Spring框架

-----------------------siwuxie095

   

   

   

   

   

   

   

   

整合 Struts2 框架和 Spring 框架

   

   

1、导入相关 jar 包(共 27 个)

   

1)导入 Struts2 的基本 jar 包(13 个)

   

   

   

其中:

   

Struts2 和 Hibernate 中,都有 javassist,会产生冲突,

选择高版本,删除低版本即可

   

   

   

2)导入 Spring 的核心 jar 包和日志相关的 jar 包(6 个)

   

   

   

Commons Logging 下载链接:

   

http://commons.apache.org/proper/commons-logging/download_logging.cgi

   

   

LOG4J 下载链接:

   

https://www.apache.org/dist/logging/log4j/

   

   

   

3)导入 Spring 的 AOP 开发的 jar 包(4 个)

   

   

   

AOP Alliance 下载链接:

   

http://mvnrepository.com/artifact/aopalliance/aopalliance

   

   

AspectJ Weaver 下载链接:

   

http://mvnrepository.com/artifact/org.aspectj/aspectjweaver

   

   

   

4)导入 Spring 的 JDBC 开发的 jar 包(2 个)

   

   

   

   

5)导入 Spring 整合 Web 项目的 jar 包(1 个)

   

   

   

   

6)导入 Struts2 整合 Spring 的 jar 包(1 个)

   

   

   

   

   

2、测试

   

1)编写一个 Action 类

   

UserAction.java:

   

package com.siwuxie095.action;

   

import com.opensymphony.xwork2.ActionSupport;

   

public class UserAction extends ActionSupport {

   

@Override

public String execute() throws Exception {

System.out.println("----- UserAction -----");

return "none";

}

 

}

   

   

   

2)在 Spring 核心配置文件中进行配置

   

applicationContext.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:aop="http://www.springframework.org/schema/aop"

xmlns:context="http://www.springframework.org/schema/context"

xmlns:tx="http://www.springframework.org/schema/tx"

xsi:schemaLocation="

http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans.xsd

http://www.springframework.org/schema/aop

http://www.springframework.org/schema/aop/spring-aop.xsd

http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context.xsd

http://www.springframework.org/schema/tx

http://www.springframework.org/schema/tx/spring-tx.xsd">

 

 

<!-- 配置 Action 对象 -->

<bean id="userAction" class="com.siwuxie095.action.UserAction" scope="prototype"></bean>

 

   

</beans>

   

   

   

3)在 Struts2 核心配置文件中进行配置

   

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>

 

<package name="demo" extends="struts-default" namespace="/">

 

<!--

此时,class 属性对应 Spring 核心配置文件中 Bean id

 

如果还写 Action 类的全限定名,Action 对象就会创建两次

-->

<action name="user" class="userAction"></action>

 

</package>

   

</struts>

   

   

   

4)在部署描述文件中进行配置

   

web.xml:

   

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">

<welcome-file-list>

<welcome-file>index.jsp</welcome-file>

</welcome-file-list>

 

 

<filter>

<!-- 配置 Struts2 的核心过滤器 -->

<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>/*</url-pattern>

</filter-mapping>

 

 

<!-- 配置 Spring 的监听器 ContextLoaderListener -->

<listener>

<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

</listener>

 

 

<!-- 配置 Spring 核心配置文件的位置(路径) -->

<context-param>

<param-name>contextConfigLocation</param-name>

<param-value>classpath:applicationContext.xml</param-value>

</context-param>

 

 

</web-app>

   

   

   

5)访问路径

   

http://localhost:8080/工程名/user.action

   

   

   

   

   

   

   

   

   

【made by siwuxie095】

原文地址:https://www.cnblogs.com/siwuxie095/p/7437051.html