openinview

我原来用struts/spring/hibernate的时候同样使用OpenSessionInView,但是似乎没有robbin所说的问题啊。而且我在使用的时候,是ContextLoaderListener和ContextLoaderPlugIn一起用的。整个配置如下:
1.首先是web.xml

Java代码 复制代码
  1. <filter>   
  2.        <filter-name>OpenSessionInViewFilter</filter-name>   
  3.        <filter-class>org.springframework.orm.hibernate.support.OpenSessionInViewFilter</filter-class>   
  4.    </filter>   
  5.       
  6.    <filter-mapping>   
  7.        <filter-name>OpenSessionInViewFilter</filter-name>   
  8.        <url-pattern>/*</url-pattern>   
  9.    </filter-mapping>   
  10.       
  11.    <listener>   
  12.     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>   
  13. </listener>   
  14.   
  15. .....  
	<filter>
        <filter-name>OpenSessionInViewFilter</filter-name>
        <filter-class>org.springframework.orm.hibernate.support.OpenSessionInViewFilter</filter-class>
    </filter>
    
    <filter-mapping>
        <filter-name>OpenSessionInViewFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    
    <listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>

......



2. 然后是struts-config.xml:

Java代码 复制代码
  1. <plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">   
  2.     <set-property property="contextConfigLocation"    
  3.                   value="/WEB-INF/action-servlet.xml"    
  4.     />   
  5. </plug-in>  
<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
	<set-property property="contextConfigLocation" 
				  value="/WEB-INF/action-servlet.xml" 
	/>
</plug-in>



其余部分省略。

在上述配置下,使用OpenSessionInView似乎没有问题。

不知道robbin所说的ContextLoaderListener和ContextLoaderPlugIn不应该同时使用是不是做得是如下的配置:(struts-config.xml)

Java代码 复制代码
  1. <plug-in   
  2. className="org.springframework.web.struts.ContextLoaderPlugIn">   
  3. <set-property property="contextConfigLocation"  
  4. value="/WEB-INF/applicationContext.xml,   
  5. /WEB-INF/action-servlet.xml"/>   
  6. </plug-in>  
<plug-in
className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation"
value="/WEB-INF/applicationContext.xml,
/WEB-INF/action-servlet.xml"/>
</plug-in>



我尝试了一下,用这种配置时,OpenSessionInView的确失效了。

我猜想,原因大概是这样:struts的这个plugIn,可能只是为了整合一个action-servlet.xml,将action-servlet.xml中的定义当作Spring的bean来使用,因此,在保存时,只要有action-servlet.xml的配置,就被保存到robbin所提到的那个attrName中,而不是WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE中,所以,OpenSessionInView是取不到这个配置的。

那么这个配置什么时候被取到呢?直觉告诉我,可能是和Action的Proxy有关。于是,查看了org.springframework.web.struts.DelegatingActionProxy的源码,果然:

Java代码 复制代码
  1. /**  
  2.      * Return the delegate Action for the given mapping.  
  3.      * <p>The default implementation determines a bean name from the  
  4.      * given ActionMapping and looks up the corresponding bean in the  
  5.      * WebApplicationContext.  
  6.      * @param mapping the Struts ActionMapping  
  7.      * @return the delegate Action  
  8.      * @throws BeansException if thrown by WebApplicationContext methods  
  9.      * @see #determineActionBeanName  
  10.      */  
  11.     protected Action getDelegateAction(ActionMapping mapping); throws BeansException {   
  12.         WebApplicationContext wac = getWebApplicationContext(getServlet();, mapping.getModuleConfig(););;   
  13.         String beanName = determineActionBeanName(mapping);;   
  14.         return (Action); wac.getBean(beanName, Action.class);;   
  15.     }   
  16.   
  17.     /**  
  18.      * Fetch ContextLoaderPlugIn's WebApplicationContext from the  
  19.      * ServletContext, containing the Struts Action beans to delegate to.  
  20.      * @param actionServlet the associated ActionServlet  
  21.      * @param moduleConfig the associated ModuleConfig  
  22.      * @return the WebApplicationContext  
  23.      * @throws IllegalStateException if no WebApplicationContext could be found  
  24.      * @see DelegatingActionUtils#getRequiredWebApplicationContext  
  25.      * @see ContextLoaderPlugIn#SERVLET_CONTEXT_PREFIX  
  26.      */  
  27.     protected WebApplicationContext getWebApplicationContext(   
  28.             ActionServlet actionServlet, ModuleConfig moduleConfig); throws IllegalStateException {   
  29.         return DelegatingActionUtils.getRequiredWebApplicationContext(actionServlet, moduleConfig);;   
  30.     }  
/**
	 * Return the delegate Action for the given mapping.
	 * <p>The default implementation determines a bean name from the
	 * given ActionMapping and looks up the corresponding bean in the
	 * WebApplicationContext.
	 * @param mapping the Struts ActionMapping
	 * @return the delegate Action
	 * @throws BeansException if thrown by WebApplicationContext methods
	 * @see #determineActionBeanName
	 */
	protected Action getDelegateAction(ActionMapping mapping); throws BeansException {
		WebApplicationContext wac = getWebApplicationContext(getServlet();, mapping.getModuleConfig(););;
		String beanName = determineActionBeanName(mapping);;
		return (Action); wac.getBean(beanName, Action.class);;
	}

	/**
	 * Fetch ContextLoaderPlugIn's WebApplicationContext from the
	 * ServletContext, containing the Struts Action beans to delegate to.
	 * @param actionServlet the associated ActionServlet
	 * @param moduleConfig the associated ModuleConfig
	 * @return the WebApplicationContext
	 * @throws IllegalStateException if no WebApplicationContext could be found
	 * @see DelegatingActionUtils#getRequiredWebApplicationContext
	 * @see ContextLoaderPlugIn#SERVLET_CONTEXT_PREFIX
	 */
	protected WebApplicationContext getWebApplicationContext(
			ActionServlet actionServlet, ModuleConfig moduleConfig); throws IllegalStateException {
		return DelegatingActionUtils.getRequiredWebApplicationContext(actionServlet, moduleConfig);;
	}



仔细看其中的取wac的代码,它并不是从WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE取的wac。

由此,我相信,除了robbin讲的修改源码以外,同时使用ContextLoaderListener和ContextLoaderPlugIn,但是不要在ContextLoaderPlugIn里面加入applicationContext.xml,只要加入你的action-servlet.xml,我相信,同样也可以非常流畅的使用OpenSessionInView

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值