Struts2 Spring2 整合

要实现与Struts与Spring的集成有如下几步

<!--[if !supportLists]-->1. <!--[endif]-->将[color=red]struts2-spring-plugin-2.0.8.jar[/color]文件包含到我们的应用中,放到WEB-INF/lib目录下面即可.

2.配置Spring监听器.

将下面一段话插入到web.xml文件中,同时将spring.jar文件放到WEB-INF/lib目录下面。
<listener>



<listener-class>



[color=red]org.springframework.web.context.ContextLoaderListener[/color]



</listener-class>



</listener>

3.利用Spring配置文件来注册对象



<!--[if !supportLists]-->(1)<!--[endif]-->这里有一点得注意:使用spring配置文件注册action对象,

<bean>中使用 [color=red]ID [/color]来定义,class=“action包引用类名”

例如:
<bean [color=red]id="registerAction" [/color]class="com.tcit.struts2.action.RegisterAction"

abstract="false" lazy-init="default" autowire="default">

</bean>

<!--[if !supportLists]-->(2) <!--[endif]-->在struts.xml文件中:<action>中的class为spring
配置文件<bean>中的 ID
 <action name="register" [color=red]class="registerAction"[/color] >

<result name="success">/result.jsp</result>

</action>

这之后的步骤就和我们使用单独使用Spring一样了,现在我们就已经完成了 Struts与Spring的集成了。这里有一点要注意的是,我们还要指定Spring配置文件的地点,默认情况下,容器会到WEB-INF目录下面去寻 找applicationContext.xml文件。如果我们想指定别的地方的配置文件或者指定多个配置文件,可以通过在web.xml文件中定义 [color=red]context-param[/color]元素来指定,如下所示:
[color=red]<context-param>



<param-name>contextConfigLocation</param-name>



<param-value>



/WEB-INF/applicationContext.xml,classpath:applicationContext-*.xml



</param-value>



</context-param>[/color]

上面一段话表示WEB-INF下面的applicationContext.xml文件以及classpath下面的所有匹配applicationContext-*.xml模式的文件都会作为Spring配置文件被装载

web.xml 的源代码清单
<?xml version="1.0" encoding="UTF-8"?><web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">    <!-- 用来定位Spring XML文件的上下文配置 --><context-param>    <param-name>contextConfigLocation</param-name>    <param-value>/WEB-INF/applicationContext*.xml,classpath*:applicationContext*.xml</param-value></context-param>        <filter>        <filter-name>struts2</filter-name>        <filter-class>            org.apache.struts2.dispatcher.FilterDispatcher        </filter-class>    </filter>     <filter-mapping>        <filter-name>struts2</filter-name>        <url-pattern>/*</url-pattern>    </filter-mapping>     <welcome-file-list>        <welcome-file>index.jsp</welcome-file>    </welcome-file-list>     <listener>        <listener-class>            org.springframework.web.context.ContextLoaderListener        </listener-class>    </listener></web-app>

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/jamesjun/archive/2008/04/17/2300901.aspx

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> <constant name="struts.objectFactory.spring.autoWire" value="type" /> <constant name="struts.objectFactory" value="spring" /> <package name="default" extends="struts-default"> <action name="HelloPOJO" class="helloPOJO" > <result>/HelloPOJO.jsp</result> </action> </package> </struts>

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/jamesjun/archive/2008/04/17/2300901.aspx

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?><beans default-autowire="byType"    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.0.xsd">        <bean id="helloPOJO" class="example.HelloPOJO" >        <property name="message">            <value>Spring Struts 2 注入</value>        </property>    </bean> </beans>

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/jamesjun/archive/2008/04/17/2300901.aspx
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值