Spring—struts2整合
2016-07-14 21:57
351 查看
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>
三、加入相应的jar包:
Spring 在WEB环境下应用,需要加入额外的两个jar包
spring-web-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>
五、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>
六、各个包的类文件
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";
}
}
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);
}
}
3>、com.struct2.service下
PersonService.java
package com.struct2.service;
public class PersonService {
public void save(){
System.out.println("Personservice...save...");
}
}
相关文章推荐
- Java中hashCode()方法以及HashMap()中hash()方法
- Spring Mvc那点事---(30)Spring Mvc传统AOP自动代理实现
- JFrame设置最大尺寸
- JAVA8 十大新特性详解
- 编程中遇到的因脑残造成的错误--struts2(2016/7/14)
- java异常处理
- java语言及其垃圾回收机制简单概述
- Java工程师需要掌握哪些知识呢?
- struts2 提供的校验器列表
- JAVA的IO操作(四)
- struts2 校验数据的有效性 2种方式
- Java多线程--线程简单认识1
- JDK 7 新特性 - 总览
- 基于spring security的用户单点登录
- Spring学习笔记四: 从Spring容器中获取Bean
- Spring学习笔记四: 从Spring容器中获取Bean
- springmvc+spring+mybatis+mysql框架整合(maven模块化分层)02
- 为什么要使用Java泛型
- 《Java编程思想》学习笔记(第二十一章):并发
- 【Java】重载与重写的区别