读书人

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

发布时间: 2012-12-25 16:18:28 作者: rapoo

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

?整合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 processor/>


??<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");

?

?

?

?? }
???

读书人网 >编程

热点推荐