4.Spring如何与struts2结合

1. Struts2的action由Spring来负责进行实例化

Struts.xml
<!-- 指定Struts 2默认的ObjectFactory Bean,该属性默认值是spring -->
<constant name="struts.objectFactory" value="spring"/>
<!-- 该属性指定整合Spring框架时,是否缓存Bean实例,该属性只允许使用true和 false两个属性值,它的默认值是true。通常不建议修改该属性值 -->
<constant name="struts.objectFactory.spring.useClassCache" value="true"/>
<!-- 指定spring框架的装配模式装配方式有: name, type, auto, and constructor (name 是默认装配模式) -->
<constant name="struts.objectFactory.spring.autoWire"
value="name"/>

2. 项目启动时由装载Spring

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


3. 配置多个applicationContext.xml配置文件

applicationContext.xml
/*
* 在com/syy文件夹下以applicationContext开头的xml文件都将被加载进去
*/
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath*:com/syy/applicationContext*.xml
</param-value>
</context-param>


完整配置信息


Struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<!-- 指定Struts 2默认的ObjectFactory Bean,该属性默认值是spring -->
<constant name="struts.objectFactory" value="spring"/>
<!-- 该属性指定整合Spring框架时,是否缓存Bean实例,该属性只允许使用true和false两个属性值,它的默认值是true。通常不建议修改该属性值 -->
<constant name="struts.objectFactory.spring.useClassCache" value="true"/>
<!-- 指定spring框架的装配模式装配方式有: name, type, auto, and constructor (name 是默认装配模式) -->
<constant name="struts.objectFactory.spring.autoWire" value="name"></constant>
<package name="struts2" extends="struts-default">
<action name="teacher" class="com.syy.action.TeacherAction">
<result name="selectAllOK">/WEB-INF/teacher/selectAll.jsp</result>
<result name="insertInputOK">/WEB-INF/teacher/insert.jsp</result>
<result name="getOK">/WEB-INF/teacher/update.jsp</result>
<result name="selectInputOK">/WEB-INF/teacher/selectInput.jsp</result>
<result name="selectOK">/WEB-INF/teacher/select.jsp</result>
</action>
</package>
</struts>


Web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_9" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
<init-param>
<param-name>actionPackages</param-name>
<param-value>com.syy.action</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath*:com/syy/applicationContext*.xml
</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
</web-app>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值