Struts学习day3-spring与Struts整合方式

17.1介绍两种整合Spring的方式

A、第一种方式(Struts2框架从Spring中拉)

Struts2中的Action及Result对象都是由一个叫做ObjectFactory来创建和管理的。

Struts2诞生要比Spring晚,所以Struts2知道Spring的运作,于是Struts2提供了集成Spring的插件struts2-spring-plugin-2.3.15.3.jar。

注:在Struts2的default.properties中有介绍关于替换掉ObjectFactory的内容,比如更改配置参数:struts.objectFactory = spring,于是乎很多文档都要求在struts.xml中通过<constant/>元素来更改该参数,但这不是必须的,因为struts2-spring-plugin-2.3.15.3.jar中的struts-plugin.xml中已经做好了这样的配置,因此只需要把jar包搞过来即可。

常量struts.objectFactory.spring.autoWire = name指定按名字装配。

此时,Struts2交给了org.apache.struts2.spring.StrutsSpringObjectFactory来创建Action,同时会从Spring拉取需要的对象。

 

具体使用:

  • 搭建Struts2(2.3.15.3)的环境
  1. 在JavaWeb应用的lib目录中拷贝运行Struts2必要的jar包。
  2. 在web.xml中配置核心过滤器

<filter>

           <filter-name>struts2</filter-name>

           <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>

</filter>

<filter-mapping>

           <filter-name>struts2</filter-name>

           <url-pattern>/*</url-pattern>

</filter-mapping>

c、在WEB-INF/classes目录下建立struts.xml的配置文件

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE struts PUBLIC

           "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"

           "http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>

    <constant name="struts.devMode" value="true" />

    <package name="p1" extends="struts-default">

       <action name="test" class="com.itheima.actions.TestAction"/>

    </package>

</struts>

d、分别建立TestAction,调用一个XxxService的某个方法,编写XxxServiceImpl的实现。

-------------------------------------------------------------------------

TestAction:

package com.itheima.actions;

import com.itheima.service.XxxService;

public class TestAction {

           private XxxService xxxService;

           public void setXxxService(XxxService xxxService) {

                      this.xxxService = xxxService;

           }

           public String execute(){

                      xxxService.doSomething();

                      return "none";

           }

}

-------------------------------------------------------------------------

XxxService

package com.itheima.service;

public interface XxxService {

           void doSomething();

}

-------------------------------------------------------------------------

package com.itheima.service.impl;

import com.itheima.service.XxxService;

public class XxxServiceImpl implements XxxService {

           public void doSomething() {

                      System.out.println("我做了,怎么着?");

           }

}

-------------------------------------------------------------------------

 

启动后访问http://localhost:8080/YourAppName/test.action会包空指针异常。

  • 在Struts2中集成Spring:主要用到Spring的Bean装配
  1. 拷贝Spring(2.5.6)所需jar包及Struts2与Spring的插件包:

Spring.jar;struts2-spring-plugin-2.3.15.3.jar

  1. 配置监听器完成Spring容器的初始化

<listener>

           <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

</listener>

  1. 在WEB-INF目录下建立applicationContext.xml的Spring配置文件

<?xml version="1.0" encoding="UTF-8"?>

<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">

           <bean name="xxxService" class="com.itheima.service.impl.XxxServiceImpl"/>

</beans>

 

启动后访问http://localhost:8080/YourAppName/test.action,一切正常。

 

B、第二种方式

主要思路是Struts2中的Action实例也交给Spring容器进行管理,在Spring配置时就给Action的属性进行装配。而Struts2中执行某个动作时要找某个动作类,此时把class指定为Spring中的Bean的名称既可以找到。稍作配置如下:

-------------------------------------------------------------------------

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"

           xsi:schemaLocation="

                                 http://www.springframework.org/schema/beans

                                 http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

           <bean name="xxxService" class="com.itheima.service.impl.XxxServiceImpl"/>

           <bean name="testAction" class="com.itheima.actions.TestAction" scope="prototype">

                      <property name="xxxService" ref="xxxService"></property>

           </bean>

</beans>

-------------------------------------------------------------------------

 

Struts.xml

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE struts PUBLIC

           "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"

           "http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>

    <constant name="struts.devMode" value="true" />

    <package name="p1" extends="struts-default">

       <action name="test" class="testAction"/>

    </package>

</struts>

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值