2011-09-14 8 views
1

のDispatcherServletのみMVC-config設定を管理するための責任がある、とのContextLoaderListenerはapplicationContext.xmlをなぜ私のapplicationContext.xmlがDispatcherServletに必要ですか?

これはweb.xmlの私の古い設定だったために責任があるではありません。

<?xml version="1.0" encoding="ISO-8859-1"?> 
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"> 

    <!-- start up and shut down Spring's root WebApplicationContext (Interface to provide configuration for a web application) --> 
    <listener> 
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
    </listener> 

    <!-- Central dispatcher for HTTP request handlers/controllers: take an incoming URI and find the right combination of handlers (generally methods on Controller classes) 
    and views (generally JSPs) that combine to form the page or resource that's supposed to be found at that location. --> 
    <servlet> 
    <servlet-name>p</servlet-name> 
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
    <init-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value> 
      /WEB-INF/spring/webmvc-config.xml  
      </param-value> 
    </init-param> 
    <load-on-startup>1</load-on-startup> 
    </servlet> 

    <servlet-mapping> 
    <servlet-name>p</servlet-name> 
    <url-pattern>/p/*</url-pattern> 
    </servlet-mapping> 

    <!-- allows one to specify a character encoding for requests. 
    This is useful because current browsers typically do not set a character encoding even if specified in the HTML page or form --> 
    <filter> 
    <filter-name>encoding-filter</filter-name> 
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> 
    <init-param> 
     <param-name>encoding</param-name> 
     <param-value>utf-8</param-value> 
    </init-param> 
    <init-param> 
     <param-name>forceEncoding</param-name> 
     <param-value>true</param-value> 
    </init-param> 
    </filter> 

    <filter-mapping> 
    <filter-name>encoding-filter</filter-name> 
    <url-pattern>/*</url-pattern> 
    <dispatcher>REQUEST</dispatcher> 
    <dispatcher>FORWARD</dispatcher> 
    </filter-mapping> 

    <context-param> 
    <param-name>contextConfigLocation</param-name> 
    <param-value> 
     /WEB-INF/spring/applicationContext.xml 
    </param-value> 
    </context-param> 

    <!-- 
    <filter> 
    <filter-name>springSecurityFilterChain</filter-name> 
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> 
    </filter> 

<filter-mapping> 
    <filter-name>springSecurityFilterChain</filter-name> 
    <url-pattern>/*</url-pattern> 
    </filter-mapping> 
    --> 


    <!-- Based on the popular and very useful mod_rewrite for apache, UrlRewriteFilter is a Java Web Filter for any J2EE 
     compliant web application server (such as Resin or Tomcat), which allows you to rewrite URLs before they get to your 
     code. It is a very powerful tool just like Apache's mod_rewrite. --> 
    <filter> 
    <filter-name>UrlRewriteFilter</filter-name> 
    <filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class> 
    </filter> 

    <filter-mapping> 
    <filter-name>UrlRewriteFilter</filter-name> 
    <url-pattern>/*</url-pattern> 
    </filter-mapping> 


</web-app> 

が、この設定で

org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here 
    org.springframework.orm.hibernate3.SpringSessionContext.currentSession(SpringSessionContext.java:63) 
    org.hibernate.impl.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:574) 
    com.spring.sample.dao.impl.PersonDaoImpl.getCurrentSession(PersonDaoImpl.java:30) 
    com.spring.sample.dao.impl.PersonDaoImpl.getAllPersons(PersonDaoImpl.java:69) 
    com.spring.sample.controller.PersonsController.get(PersonsController.java:34) 
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) 
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) 
    java.lang.reflect.Method.invoke(Method.java:597) 
    org.springframework.web.bind.annotation.support.HandlerMethodInvoker.doInvokeMethod(HandlerMethodInvoker.java:710) 
    org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:167) 
    org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:414) 
    org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:402) 
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:771) 
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:716) 
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:647) 
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:552) 
    javax.servlet.http.HttpServlet.service(HttpServlet.java:617) 
    javax.servlet.http.HttpServlet.service(HttpServlet.java:717) 
    org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:70) 
    org.tuckey.web.filters.urlrewrite.NormalRewrittenUrl.doRewrite(NormalRewrittenUrl.java:195) 
    org.tuckey.web.filters.urlrewrite.RuleChain.handleRewrite(RuleChain.java:159) 
    org.tuckey.web.filters.urlrewrite.RuleChain.doRules(RuleChain.java:141) 
    org.tuckey.web.filters.urlrewrite.UrlRewriter.processRequest(UrlRewriter.java:90) 
    org.tuckey.web.filters.urlrewrite.UrlRewriteFilter.doFilter(UrlRewriteFilter.java:417) 
    org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88) 
    org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76) 

webmvc-config.xmlに:

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:mvc="http://www.springframework.org/schema/mvc" 
    xmlns:p="http://www.springframework.org/schema/p" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xsi:schemaLocation=" 
     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
     http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd 
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> 

    <!-- webmvc-config.xml: is for web configuration, and it's loaded with Dispatacher Servlet --> 

    <context:component-scan base-package="com.spring.sample" /> 

    <!-- - It declares explicit support for annotation-driven MVC controllers (i.e. @RequestMapping, @Controller, etc). 
     - configures support for new Spring MVC features such as declarative validation with @Valid, HTTP message conversion with @RequestBody/@ResponseBody. 
     - supports Spring Jackson JSON. --> 
    <mvc:annotation-driven /> 

    <!-- Changes the locale when a 'lang' request parameter is sent; e.g. /?lang=ar --> 
    <mvc:interceptors> 
     <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" 
      p:paramName="lang" /> 
    </mvc:interceptors> 

    <!-- uses a locale attribute in the user's session --> 
    <bean id="localeResolver" 
     class="org.springframework.web.servlet.i18n.SessionLocaleResolver" /> 

     <!-- - translates from view name to view class. 
      - makes views avaiable to controllers, when controller return a view name, the view name is rendered. --> 
    <bean class="org.springframework.web.servlet.view.BeanNameViewResolver" /> 

     <!-- viewResolver for jsp --> 
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
     <property name="prefix" value="/WEB-INF/views/"/> 
     <property name="suffix" value=".jsp"/> 
    </bean> 

</beans> 
、私は次の例外を得ました

この例外は、web.xmlのDispatcherServletにapplicationContext.xmlを追加した後に解決されました。

+0

webmvc-config.xmlの内容は何ですか? –

+0

投稿を編集して上に追加しました。 –

答えて

1

@Transactionalと思っています。その場合、<tx:annotation-driven />の効果は宣言されているコンテキストに限定されていることに注意してください。

@Transactionalの豆がwebmvc-config.xmlで宣言されている場合は、<tx:annotation-driven />も追加する必要があります。 <tx:annotation-driven />applicationContext.xmlにのみ指定されていると、記述した動作が発生する可能性があります。私はそれが自分のコンテキストの持つ各ディスパッチサーブレットに関係していると思います

+0

はい@TransactionalでBeanを持っていますが、mvc-configで定義されていないbeanを持っています。ファイルを追加するために私の投稿を編集しました。 –

+0

まあ、mvc-configにを追加してもうまくいきましたが、理由は分かりません(mvc-config.xmlで@Transactionalを使用するBeanはありません)。 –

+0

@Jsword:おそらく ''は '@ Transactional'でいくつかのBeanを見つけますか?もしそうなら、 'mvc-config.xml'でも暗黙的に宣言されています。 – axtavt

0

は、このブログで

http://www.dotkam.com/2008/07/09/spring-web-application-context-visibility/

を見て、私は、私はすべての私をロードするために、リスナーを使用して、このようにいくつかのことをすることによって、この問題を解決しましたディスパッチサーブレットは、コンテキストコンフィギュレーション

を持っていないの不平を言うので、私はそれに定義されていない構成でダミーコンテキストの設定を作成し、この

<context-param> 
    <param-name>contextConfigLocation</param-name> 
    <param-value> 
     /WEB-INF/spring/applicationContext.xml, /WEB-INF/spring/applicationContext-MVC.xml 
    </param-value> 
    </context-param> 

のようなコンテキストファイルは、私がこれをしなければなりませんでした

<init-param> 
    <param-name>contextConfigLocation</param-name> 
    <param-value> 
     /WEB-INF/spring/dummy-config.xml  
     </param-value> 
    </init-param> 
関連する問題