kingpub

          海內存知己,博客若比鄰

           

          Hibernate/Spring/Struts架構使用OpenSessionInView的問題


          我原來用struts/spring/hibernate的時候同樣使用OpenSessionInView,但是似乎沒有robbin所說的問題啊。而且我在使用的時候,是ContextLoaderListener和ContextLoaderPlugIn一起用的。整個配置如下:
          1.首先是web.xml
          java代碼:?


          ? ? ? ? <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代碼:?


          <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代碼:?


          <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代碼:?


          /**
          ? ? ? ? * 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
          ? ? ? ? */

          ? ? ? ? protectedAction 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)throwsIllegalStateException{
          ? ? ? ? ? ? ? ? return DelegatingActionUtils.getRequiredWebApplicationContext(actionServlet, moduleConfig);
          ? ? ? ? }



          仔細看其中的取wac的代碼,它并不是從WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE取的wac。

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

          posted on 2006-06-20 12:40 xiaofeng 閱讀(140) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           

          導航

          統計

          常用鏈接

          留言簿(2)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          收藏夾

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 呼图壁县| 靖远县| 北川| 鄂托克前旗| 洛浦县| 满洲里市| 汪清县| 顺平县| 江城| 梅州市| 镇坪县| 许昌县| 大厂| 利川市| 卓尼县| 建湖县| 罗田县| 桐庐县| 自贡市| 永宁县| 双鸭山市| 南康市| 长顺县| 霍州市| 松溪县| 临邑县| 古田县| 光山县| 枣庄市| 泗水县| 牡丹江市| 开鲁县| 伊吾县| 炉霍县| 潜山县| 清镇市| 寿光市| 河西区| 屯昌县| 扶余县| 聂荣县|