码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2016-02-28 19:53:44      阅读:199      评论:0      收藏:0      [点我收藏+]

标签:

一、单独测试strust

1.action

 1 package cn.itcast.oa.test;
 2 
 3 import org.springframework.context.annotation.Scope;
 4 import org.springframework.stereotype.Component;
 5 import org.springframework.stereotype.Controller;
 6 import org.springframework.stereotype.Repository;
 7 import org.springframework.stereotype.Service;
 8 
 9 import com.opensymphony.xwork2.ActionSupport;
10 
11 //@Component("testAction")
12 //@Service
13 //@Repository
14 @Controller("testAction")
15 @Scope("prototype")
16 public class TestAction extends ActionSupport {
17 
18 
19     @Override
20     public String execute() throws Exception {
21         System.out.println("---> TestAction.execute()");
22         return "success";
23     }
24 }

 

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

 1 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
 2 
 3 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 4 <html>
 5   <head>
 6   </head>
 7   
 8   <body>
 9    测试 action. <br>
10    struts与spring整合成功. <br>
11   </body>
12 </html>

4.访问http://localhost:8080/MyOA/test.action

二、单独测试srping

1.java

1 public class SpringTest {
2 
3     private ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
4 
5     @Test
6     public void testBean() throws Exception {
7         TestAction testAction = (TestAction) ac.getBean("testAction");
8         System.out.println(testAction);
9     }

 

2.applicationContext.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
 4     xmlns:tx="http://www.springframework.org/schema/tx"
 5     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
 6                 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
 7                 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
 8 
 9     <!-- 自动扫描(包含子包)与装配bean以便可以写注解 -->
10     <context:component-scan base-package="cn.itcast.oa"></context:component-scan>

 

三、整合

1.添加struts2-spring-plugin-2.1.8.1.jar

2.在web.xml中添加spring监听器

1     <!-- 配置Spring的用于初始化容器对象的监听器 -->
2     <listener>
3         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
4     </listener>
5     <context-param>
6         <param-name>contextConfigLocation</param-name>
7         <param-value>classpath:applicationContext*.xml</param-value>
8     </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>

 

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

标签:

原文地址:http://www.cnblogs.com/shamgod/p/5225255.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!