OA学习笔记-005-Spring2.5与struts2.1整合
2016-02-28 18:05
513 查看
一、单独测试strust
1.action
2.struts.xml
3.test.jsp
4.访问http://localhost:8080/MyOA/test.action
二、单独测试srping
1.java
2.applicationContext.xml
三、整合
1.添加struts2-spring-plugin-2.1.8.1.jar
2.在web.xml中添加spring监听器
ps:整合后,struts.xml的action可以写对象名,不用写全路径
1.action
package cn.itcast.oa.test; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Component; import org.springframework.stereotype.Controller; import org.springframework.stereotype.Repository; import org.springframework.stereotype.Service; import com.opensymphony.xwork2.ActionSupport; //@Component("testAction") //@Service //@Repository @Controller("testAction") @Scope("prototype") public class TestAction extends ActionSupport { @Override public String execute() throws Exception { System.out.println("---> TestAction.execute()"); return "success"; } }
2.struts.xml
<package name="default" namespace="/" extends="struts-default"> <!-- 配置测试用的Action,未与Spring整合,class属性写类的全名 --> <!-- 当Struts2与Spring整合后,class属性可以写bean的名称 --> <action name="test" class="testAction"> <result name="success">/test.jsp</result> </action> </package>
3.test.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> </head> <body> 测试 action. <br> struts与spring整合成功. <br> </body> </html>
4.访问http://localhost:8080/MyOA/test.action
二、单独测试srping
1.java
public class SpringTest { private ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml"); @Test public void testBean() throws Exception { TestAction testAction = (TestAction) ac.getBean("testAction"); System.out.println(testAction); }
2.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: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-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <!-- 自动扫描(包含子包)与装配bean以便可以写注解 --> <context:component-scan base-package="cn.itcast.oa"></context:component-scan>
三、整合
1.添加struts2-spring-plugin-2.1.8.1.jar
2.在web.xml中添加spring监听器
<!-- 配置Spring的用于初始化容器对象的监听器 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext*.xml</param-value> </context-param>
ps:整合后,struts.xml的action可以写对象名,不用写全路径
<package name="default" namespace="/" extends="struts-default"> <!-- 配置测试用的Action,未与Spring整合,class属性写类的全名 --> <!-- 当Struts2与Spring整合后,class属性可以写bean的名称 --> <action name="test" class="testAction"> <result name="success">/test.jsp</result> </action> </package>
相关文章推荐
- Java集合
- MyBatis 实践 -配置
- [转]Spring 注解总结
- JVM系列1:java内存区域分配
- java程序的运行过程
- Java泛型实例
- MyEclipse使用依赖项目在发布项目时报ClassNotFound
- 新手迷惑:复制文件路径报错java.io.FileNotFoundException
- java环境变量设置
- 新手迷惑:复制文件路径报错java.io.FileNotFoundException
- 保护眼睛-eclipse黑色背景设置
- springMVC-自定义数据类型转换器
- eclipse 配置优化
- [转]Spring注解原理的详细剖析与实现
- Java张孝祥视频 学习笔记 反射
- Ubuntu安装JDK
- 用正交多项式作最小二乘拟合的java实现(转)
- 白话Spring(基础篇)---依赖注入
- OA学习笔记-004-Spring2.5配置
- Java枚举单例