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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了OA学习笔记-005-Spring2.5与struts2.1整合相关的知识,希望对你有一定的参考价值。

一、单独测试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整合的主要内容,如果未能解决你的问题,请参考以下文章

OA学习笔记-007-Dao层设计

OA学习笔记-002-Sruts2.1配置

OA学习笔记-008-岗位管理Action层实现

OA学习笔记-004-Spring2.5配置

J2EE核心开发学习笔记 003struts2的搭建及连接数据库实现用户注册与登录

OA项目实战学习