2012-06-13 10 views
14

Jestem nowy w Spring MVC. Mam aplikację internetową. Mam następującą konfigurację:Dodawanie ContextLoaderListener do web.xml w Spring MVC

<welcome-file-list> 
    <welcome-file>list.html</welcome-file> 
</welcome-file-list> 
<servlet> 
    <servlet-name>spring</servlet-name> 
    <servlet-class> 
     org.springframework.web.servlet.DispatcherServlet 
    </servlet-class> 
    <load-on-startup>1</load-on-startup> 
</servlet> 
<servlet-mapping> 
    <servlet-name>spring</servlet-name> 
    <url-pattern>/</url-pattern> 
</servlet-mapping> 


muszę dodać następujący wiersz do web.xml pliku?

<listener> 
    <listener-class> 
     org.springframework.web.context.ContextLoaderListener 
    </listener-class> 
</listener> 
+2

Czy to działa z lub bez? Po prostu musisz spróbować ':)' – sp00m

Odpowiedz

21

Tak trzeba dodać ContextLoaderListener w web.xml, tylko jeśli chcesz załadować inne pliki kontekst Wiosna xml, jak również podczas ładowania aplikacji i można je określić jako

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

Tylko jeśli masz dwa pliki xml config. Jeden z usługami/DAO i inny z kontrolerem. Jeśli wszystko skonfigurowałeś w jednym pliku konfiguracyjnym, nie potrzebujesz ContextLoaderListener, wystarczy serwlet dispashchera.

Zaleca się podzielenie konfiguracji na dwie części i użycie ContextLoaderListener do utworzenia kontekstu aplikacji root i serwletu dispatchera w celu utworzenia kontekstu aplikacji warstwy internetowej.

3
<servlet> 
     <servlet-name>spring</servlet-name> 
     <servlet-class> 
      org.springframework.web.servlet.DispatcherServlet 
     </servlet-class> 
     <init-param> 
      <param-name>contextConfigLocation</param-name> 
      <param-value>classpath:applicationContext.xml,WEB-INF/spring-security.xml</param-value> 
     </init-param> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 
    <servlet-mapping> 
     <servlet-name>spring</servlet-name> 
     <url-pattern>UR_PATTERN</url-pattern> 
    </servlet-mapping> 

To zadziałało dobrze dla mnie.

4

Jest opcjonalny, nie potrzebujesz go tylko do Spring MVC (DispatcherServlet). Ale dodanie do wiosny bezpieczeństwo wiosennym MVC musi być wykonane z

<listener> 
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
</listener> 

tylko jedna uwaga, jeśli przy użyciu ContextLoaderListener trzeba będzie dodać DelegatingFilterProxy:

<listener> 
    <listener-class> 
     org.springframework.web.context.ContextLoaderListener 
    </listener-class> 
</listener> 

<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>/admin</url-pattern> 
</filter-mapping> 

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

w Twojej web.xml jak również. Przepraszam, że spóźniłem się o cztery lata. Pozdrowienia

Powiązane problemy