您的位置:首页 > 编程语言 > Java开发

OA学习笔记-005-Spring2.5与struts2.1整合

2016-02-28 18:05 513 查看
一、单独测试strust

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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: