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的 ...
随机推荐
- 获取当前日期和农历的js代码
来自:http://www.cnblogs.com/Gnepner/archive/2011/09/07/2169822.html 获取当前日期 getToday.js: function GetCu ...
- 安装Vmware ESX Server5.5 ——hardware virtualization is not a feature of the cpu or is not enabled in the BIOS
Error信息: hardware virtualization is not a feature of the cpu or is not enabled in the BIOS 解决方案: F2进 ...
- filezilla 读取目录失败
用到FTP,本来一直用主动模式,可以最近老是读取目录失败,425 Can't open data connection 和 读取目录列表失败(搞了好久,一天) 问题解决 这个问题主要是由于使用Pass ...
- CListCtrl消息及解释
对于CListCtrl消息的解释:[来自网络]LVN_BEGINDRAG 鼠标左键正在被触发以便进行拖放操作(当鼠标左键开始拖拽列表视图控件中的项目时产生) LVN_BEGINRDRAG 鼠标右键正在 ...
- php and mysql pear的安装
http://www.cnblogs.com/bugY/archive/2012/07/06/2578972.html 什么是PEAR 来自百度百科:PEAR是PHP扩展与应用库(the PHP Ex ...
- spring 攻略
1.5 指定Bean引用 为了Bean之间相互访问,在Bean配置文件中通过<ref>元素为Bean属性或构造程序参数指定Bean引用. <property name="p ...
- gevent For the Working Python Developer
Gevent指南 gevent程序员指南 由Gevent社区编写 gevent是一个基于libev的并发库.它为各种并发和网络相关的任务提供了整洁的API. 介绍 贡献者 核心部分 Greenle ...
- Flask(5)- Flask-Session组件、WTForms组件、数据库连接池(POOL)
一.Flask-Session 我们使用过flask内置的session,知道它是把session存放在浏览器,即客户端.今天要学习的flask-session是flask的第三方组件,看一下它和fl ...
- iMessenger 2.0.14.0801简述
有些梦,看似遥不可及.但并非不能实现,仅仅要你足够的强!!.人力有时而穷,所以我们可能还须要一些热心人的帮助.这个人可能就是你. 四年来,我们一直在努力,从未放弃. 在我们做好一件事之前.我们永远不知 ...
- java正则匹配
java正则提取需要用到Matcher类,下面给出案例示例供参考 需要提取车牌号中最后一个数字,比如说:苏A7865提取5,苏A876X提取6import java.util.regex.Matche ...