Spring -- spring整合struts2
1. 概述
spring和struts整合:
1.创建web程序
2.引入struts2类库.
3.创建HelloWorldAction
package cn.itcast.struts2.action;
import com.opensymphony.xwork2.ActionSupport;
/**
* HelloWorldAction
*/
public class HelloWorldAction extends ActionSupport {
private static final long serialVersionUID = 6480501738385774728L;
public String reg() {
System.out.println("hello world");
return SUCCESS;
}
}
3.配置web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<filter>
<filter-name>action</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>action</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
4.配置struts.xml
<?xml version="1.0"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
"http://struts.apache.org/dtds/struts-2.1.7.dtd">
<struts>
<constant name="struts.devMode" value="true" />
<package name="helloworldPkg" extends="struts-default">
<action name="HelloWorldAction_*" class="cn.itcast.struts2.action.HelloWorldAction" method="{1}">
<result name="success">/index.jsp</result>
</action>
</package>
</struts> 5.引入spring类库 + struts2-spring-plugin-2.1.8.1.jar
6.配置web.xml
<!-- 通过上下文参数指定spring配置文件的位置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:beans.xml</param-value>
</context-param> <!-- 上下文载入器监听器,确保web服务器启动时,直接完成spring容器的初始化,将ac放到application范围中 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> 7.修改struts.xml配置文件中的action
把action的class改成bean的id.
<!-- class:指定action在spring容器的id -->
<action name="HelloWorldAction_*" class="helloWorldAction" method="{1}"> struts2加载顺寻:
1.struts-core.jar/struts.xml
2.xxx-plugin.xml / struts-plugin.xml
3.project/struts.xml ac = new Classpathxmlac(new String[]{a.xml,b.xml});
2. 示例代码
HelloWorldService.java, service接口
public interface HelloWorldService {
public void sayHello();
}
HelloWorldServiceImpl.java, service实现
//方式二:注解配置
@Service("helloWorldService")
public class HelloWorldServiceImpl implements HelloWorldService {
public void sayHello() {
System.out.println("this is a service");
}
}
HelloWorldAction.java, 处理请求的action
/**
* HelloWorldAction, 方式二:注解配置
*/
@Controller("helloWorldAction")
@Scope("prototype")
public class HelloWorldAction extends ActionSupport {
private static final long serialVersionUID = 6480501738385774728L; @Resource()
private HelloWorldService hws ; public HelloWorldAction(){
System.out.println("new HelloWorldAction()");
}
public String reg() {
ServletActionContext.getServletContext().getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
hws.sayHello();
System.out.println("hello world");
return SUCCESS;
}
}
beans.xml,spring配置bean, 只配置bean
<?xml version="1.0"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd ">
<!-- 多个配置文件的情况,action,service配置分开
<import resource="actions.xml" />
-->
<!-- helloWorldService 方式一,xml配置
<bean id="helloWorldService"
class="cn.itcast.struts2.service.HelloWorldServiceImpl">
</bean>
-->
<!-- 组件扫描 方式二,注解配置 -->
<context:component-scan base-package="cn.itcast.struts2.action,cn.itcast.struts2.service" />
</beans>
actions.xml,spring配置action
<?xml version="1.0"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<!-- helloworldAction -->
<bean id="helloWorldAction"
scope="prototype"
class="cn.itcast.struts2.action.HelloWorldAction">
<property name="hws" ref="helloWorldService" />
</bean>
</beans>
struts.xml, struts配置
<?xml version="1.0"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
"http://struts.apache.org/dtds/struts-2.1.7.dtd">
<struts>
<constant name="struts.devMode" value="true" />
<package name="helloworldPkg" extends="struts-default">
<!--class 应该设置为action.xml中对应的ID,才能有spring的注入属性-->
<action name="HelloWorldAction_*" class="helloWorldAction" method="{1}">
<result name="success">/index.jsp</result>
</action>
</package>
</struts>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<!-- 通过上下文参数指定spring配置文件的位置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:beans.xml</param-value>
</context-param> <!-- 上下文载入器监听器,确保web服务器启动时,直接完成spring容器的初始化,并把ac保存到application中 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<filter>
<filter-name>action</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>action</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
Spring -- spring整合struts2的更多相关文章
- Spring 框架整合Struts2 框架和 Hibernate 框架
1. Spring 框架整合 Struts2 框架 // [第一种整合方式(不推荐)](http://www.cnblogs.com/linkworld/p/7718274.html) // 从 Se ...
- Spring笔记⑥--整合struts2
Spring如何在web应用里面用 需要额外加入的jar包 Spring-web-4.0.0 Spring-webmvc-4.0.0 Spring的配置文件,没什么不同 需要在web.xml下配置 ...
- Spring框架整合Struts2
1,用Spring架构,及Struts2-spring-plugin插件 导入Spring的dist全部所需的jar包 Struts2的spring插件 struts2-spring-plugin.X ...
- Spring框架整合Struts2框架的传统方法
1. 导入CRM项目的UI页面,找到添加客户的页面,修改form表单,访问Action * 将menu.jsp中133行的新增客户的跳转地址改为:href="${pageContext.re ...
- spring整合struts2
1. Spring 如何在 WEB 应用中使用 ? 1). 需要额外加入的 jar 包: spring-web-4.0.0.RELEASE.jarspring-webmvc-4.0.0.RELEASE ...
- Spring 整合 Struts2
1. Spring 如何在 WEB 应用中使用 ? 1). 需要额外加入的 jar 包: spring-web-4.0.0.RELEASE.jar spring-webmvc-4.0.0.RELEAS ...
- struts2 spring mybatis 整合(test)
这几天搭了个spring+struts2+mybatis的架子,练练手,顺便熟悉熟悉struts2. 环境:myEclipse10+tomcat7+jdk1.6(1.8的jre报错,所以换成了1.6) ...
- Struts2的使用以及Spring整合Struts2
一.如何单独使用Struts2 (1)引入struts2的jar包 commons-fileupload-1.2.1.jar freemarker-2.3.15.jar ognl-2.7.3.jar ...
- Spring学习6-Spring整合Struts2
一.Spring为什么要整合Struts2 Struts2与Spring进行整合的根本目的就是要让 Spring为Struts2的Action注入所需的资源对象,它们整合的原理则是只要导入了s ...
- Spring与Struts2整合VS Spring与Spring MVC整合
Spring与Struts2整合,struts.xml在src目录下 1.在web.xml配置监听器 web.xml <!-- 配置Spring的用于初始化ApplicationContext的 ...
随机推荐
- iOS 如何用JSONKit读写JSON文件
如何用JSONKit读写JSON文件 分类: ios2013-04-20 12:46 510人阅读 评论(0) 收藏 举报 JSON文件格式简单,使用方便,值得一用. 目前已经有多个库支持Json文 ...
- 《从零开始学Swift》学习笔记(Day 62)——Core Foundation框架之内存托管对象与非托管对象
原创文章,欢迎转载.转载请注明:关东升的博客 内存托管对象 Swift中调用Core Foundation函数获得对象时候,对象分为:内存托管对象和内存非托管对象. 内存托管对象就是由编译器帮助管理内 ...
- PHP使用SimpleElement创建和解析xml文件
<!-- 使用SimpleXMLElement生成xml文件 --><?php//生成一个xml文件 //xml字符串$_xml = <<<_xml<?xml ...
- 将Android studio的工程导入到eclipse中
自从Android Studio(后面称AS)推出后,越来越多的项目都使用AS开发. AS往eclipse迁移的方法: 其实很简单,代码都是一样的,从AS工程中找到与Eclipse工程对应的文件,放到 ...
- 服务器1M带宽同时能承受多少人在线
最近网站的流量一直在增长,这个肯定是好事.不过也有个麻烦的问题,目前本站用的的虚拟空间,每月流量30G,虽然95%的图片都已外链,但流量还是很吃紧,日均2000ip,4月份流量34g左右,单JS的响应 ...
- Jmeter Aggregate Report 与 Summary Report 分析
关于Aggregate Report和 Summary Report里面每个字段的说明,在网上有很多资料,在此不做说明. 本文主要讲Aggregate Report与Summary Report对比我 ...
- Java方法区和永久代
Java方法区和永久代 目前有三大Java虚拟机:HotSpot,oracle JRockit,IBM J9. JRockit是oracle发明的,用于其WebLogic服务器,IBM JVM是IBM ...
- MongoDB-3: 查询(一)
一.简介 MongoDB提供了db.collection.find() 方法可以实现根据条件查询和指定使用投影运算符返回的字段省略此参数返回匹配文档中的所有字段. 二.db.collection.fi ...
- Angular学习笔记—HttpClient (转载)
HttpClientModule 应用 导入新的 HTTP Module import {HttpClientModule} from '@angular/common/http'; @NgModul ...
- Jquery 实现跨域处理
JS部分代码: $.ajax({ url:url, dataType:'jsonp', data:{title:title}, jsonp:'callback', success:function(l ...