您的位置:首页 > 编程语言 > Java开发

小结spring和struts整合的三类方式

2008-08-27 21:06 423 查看
整合spring和hibernate的三种方式,小结之.



1) 在struts中使用webapplicationcontext调用spring

声明web.xml,声明一个contextloadlistener,让在启动时执行该listener,读spring的配置文件

<listener>

<listener-class>

org.springframework.web.context.ContextLoaderListener

</listener-class>

</listener>

再增加一个contextConfigLocation

<context-param>

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

<param-value>/WEB-INF/applicationContext*.xml</param-value>

</context-param>

WebApplicationContext ctx=WebApplicationContextUtils.getWebApplicationContext(servletContext);

UserBean userbean=(UserBean)ctx.getBean("userbean");



2) 将struts的action托管给spring

这也是很经常用的.用法是

在struts-config.xml中,加载contextloaderplugin插件,加载spring配置



<controller processorClass="org.springframework.web.struts.DelegatingRequestProcessor" />

<plug-in

className="org.springframework.web.struts.ContextLoaderPlugIn">

<set-property property="contextConfigLocation"

value="/WEB-INF/applicationContext.xml" />

</plug-in>

这样的话,把struts的action完全托管给spring了,在struts-config.xml中

<action path="/user/"..>,这里不需要再用class了,

但在application-context.xml中,则要有

<bean name="/user"..../>了.



3 继承spring的actionsupport类

比如

public class aaaa extends DispatchActionSupport

{

......



WebApplicationContext ctx=WebApplicationContextUtils.getWebApplicationContext(servletContext);

UserBean userbean=(UserBean)ctx.getBean("userbean");







}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: