2011-09-14 24 views
1

不DispatcherServlet的只負責管理MVC-配置,和的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中添加applicationContext.xml到DispatcherServlet後解決的,有什麼想法爲什麼?

+0

webmvc-config.xml的內容是什麼? –

+0

我編輯了帖子並在上面添加了它。 –

回答

1

我想你使用@Transactional。如果是這樣,請注意<tx:annotation-driven />的影響僅限於聲明它的環境。

因此,如果您有任何在webmvc-config.xml中聲明@Transactional的豆,您還應該在其中添加<tx:annotation-driven />。如果僅在applicationContext.xml中指定<tx:annotation-driven />,則可能會導致您描述的行爲。

+0

是的,我有@Transactional豆,但沒有在mvc-config中定義,我編輯我的帖子上面添加文件,請指教。 –

+0

好吧,當我向mvc-config添加時它工作正常,但我不知道爲什麼(儘管沒有在mvc-config.xml中使用@Transactional的bean),請告知。 –

+0

@Jword:也許''用'@ Transactional'找到一些bean?如果是這樣,它們也在'mvc-config.xml'中隱式聲明。 – axtavt

0

我認爲它必須做有它自己的上下文中的每個調度的servlet,看看這個博客

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> 

我創建沒有在它定義的配置一個虛擬的情境配置方面的文件,我不得不這樣做,因爲調度的servlet沒有上下文配置

的抱怨
<init-param> 
    <param-name>contextConfigLocation</param-name> 
    <param-value> 
     /WEB-INF/spring/dummy-config.xml  
     </param-value> 
    </init-param> 
相關問題