`
footman265
  • 浏览: 114296 次
  • 性别: Icon_minigender_1
  • 来自: 宁波
社区版块
存档分类
最新评论

Spring如何与struts2结合

    博客分类:
  • SSH
阅读更多

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> 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics