整合Struts2框架和Spring框架

来源:互联网 发布:linux 虚拟机不能上网 编辑:程序博客网 时间:2024/05/22 15:52

-----------------------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 UserActionextends ActionSupport {

  

@Override

public String execute()throws Exception {

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

return"none";

}

 

}

  

  

  

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

  

applicationContext.xml:

  

<?xmlversion="1.0"encoding="UTF-8"?>

<beansxmlns="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对象 -->

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

 

  

</beans>

  

  

  

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

  

struts.xml:

  

<?xmlversion="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>

 

<packagename="demo"extends="struts-default"namespace="/">

 

<!--

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

 

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

-->

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

 

</package>

  

</struts>

  

  

  

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

  

web.xml:

  

<?xmlversion="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/javaeehttp://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】

原创粉丝点击