Spring—struts2整合
来源:互联网 发布:客户无忧软件 编辑:程序博客网 时间:2024/06/03 07:26
1.整合步骤:
一、整合目标
Spring来管理Struts的Action
二、新建一个web项目,配置web.xml文件如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<!-- 配置 Spring 配置文件的名称和位置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- 启动 IOC 容器的 ServletContextListener -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 配置 Struts2 的 Filter 配置的可以在struts2的web.xml下blank项目下找到 -->
<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>/*</url-pattern>
</filter-mapping>
</web-app>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<!-- 配置 Spring 配置文件的名称和位置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- 启动 IOC 容器的 ServletContextListener -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 配置 Struts2 的 Filter 配置的可以在struts2的web.xml下blank项目下找到 -->
<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>/*</url-pattern>
</filter-mapping>
</web-app>
三、加入相应的jar包:
Spring 在WEB环境下应用,需要加入额外的两个jar包
spring-web-4.0.4.RELEASE.jar
spring-webmvc-4.0.4.RELEASE.jar
spring-webmvc-4.0.4.RELEASE.jar
四、Struts2配置文件(可在下载的Struts2的blank项目下找到)
<?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>
<constant name="struts.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="false" />
<package name="default" extends="struts-default" namespace="/">
<!-- class是指向Spring里面的Bean的名字,不再是全类名,利用反射自己创建,而是将创建的工厂 指向Spring(利用struts2-spring-plugin-2.1.8.jar包实现) -->
<action name="person-save" class="personAction" method="execute">
<result name="success">/success.jsp</result>
</action>
</package>
</struts>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="false" />
<package name="default" extends="struts-default" namespace="/">
<!-- class是指向Spring里面的Bean的名字,不再是全类名,利用反射自己创建,而是将创建的工厂 指向Spring(利用struts2-spring-plugin-2.1.8.jar包实现) -->
<action name="person-save" class="personAction" method="execute">
<result name="success">/success.jsp</result>
</action>
</package>
</struts>
五、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:jee="http://www.springframework.org/schema/jee" 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-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
<bean id="person" class="com.struct2.beans.Person">
<property name="username" value="yebin"></property>
</bean>
<!-- 注意: 在IOC容器配置struts2的Action时,必须要这样配置 scope="prototype" -->
<bean id="personAction" class="com.struct2.action.PersonAction"
scope="prototype">
<property name="personService" ref="personService"></property>
</bean>
<bean id="personService" class="com.struct2.service.PersonService"></bean>
</beans>
<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:jee="http://www.springframework.org/schema/jee" 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-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
<bean id="person" class="com.struct2.beans.Person">
<property name="username" value="yebin"></property>
</bean>
<!-- 注意: 在IOC容器配置struts2的Action时,必须要这样配置 scope="prototype" -->
<bean id="personAction" class="com.struct2.action.PersonAction"
scope="prototype">
<property name="personService" ref="personService"></property>
</bean>
<bean id="personService" class="com.struct2.service.PersonService"></bean>
</beans>
六、各个包的类文件
1>、com.struct2.action下
PersonAction.java
package com.struct2.action;
import com.struct2.service.PersonService;
/**
* person的Action
*
* @author yb
*
*/
public class PersonAction {
// 添加PersonService注入
private PersonService personService;
public void setPersonService(PersonService personService) {
this.personService = personService;
}
public String execute() {
System.out.println("personAction....");
personService.save();
return "success";
}
}
import com.struct2.service.PersonService;
/**
* person的Action
*
* @author yb
*
*/
public class PersonAction {
// 添加PersonService注入
private PersonService personService;
public void setPersonService(PersonService personService) {
this.personService = personService;
}
public String execute() {
System.out.println("personAction....");
personService.save();
return "success";
}
}
2>、com.struct2.beans下
Person.java
package com.struct2.beans;
/**
* person类
* @author yb
*
*/
public class Person {
private String username;
public void setUsername(String username) {
this.username = username;
}
public void hello(){
System.out.println("My name is"+ username);
}
}
/**
* person类
* @author yb
*
*/
public class Person {
private String username;
public void setUsername(String username) {
this.username = username;
}
public void hello(){
System.out.println("My name is"+ username);
}
}
3>、com.struct2.service下
PersonService.java
package com.struct2.service;
public class PersonService {
public void save(){
System.out.println("Personservice...save...");
}
}
public class PersonService {
public void save(){
System.out.println("Personservice...save...");
}
}
0 0
- Spring—struts2整合
- SSH—Struts2整合Spring
- struts2——整合spring与struts2
- Spring -- spring整合struts2
- Spring:Spring整合Struts2
- 【Spring】Spring整合Struts2
- EJB3,Spring,Struts2整合
- struts2+hibernate+spring整合
- Struts2 与 Spring 整合
- struts2 spring hibernate 整合
- Spring struts2 hibernate 整合
- struts2整合spring步骤
- spring/struts2整合
- spring 整合struts2
- spring + struts2 整合
- Struts2与Spring整合
- Struts2和Spring整合
- struts2+spring+hibernate整合
- tcp/ip是什么
- 九大排序算法再总结
- UVA 591 Box of Bricks
- 11 数值的整数次方
- makefile总结 (可做手册使用)
- Spring—struts2整合
- Android N代码分析:requestLayout
- android studio 2.0 导入项目
- 兹瓷分裂合并的线段树~(Segment tree Beats!Ⅱ)
- SMB协议
- leetcode题解-242.Valid Anagram
- 【BJOJ1097】旅游景点 atr (spfa+状压dp)
- iOS 图像压缩 图像选择的使用
- Manacher算法 求最长回文字串