Adding ContextLoaderListener to web.xml in Spring MVC

I am new to Spring MVC. I have a web application. I have the following configuration:

<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> 


Do I need to add the following line to the web.xml file?

 <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> 
+14
java spring spring-mvc
Jun 13 2018-12-12T00:
source share
4 answers

Yes, you need to add ContextLoaderListener to web.xml , only if you want to load other Spring xml files, and also when loading the application and you can specify them as

 <context-param> <param-name>contextConfigLocation</param-name> <param-value> /WEB-INF/spring-security.xml </param-value> </context-param> 
+21
Jun 13 2018-12-12T00:
source share

Only if you have two xml configuration files. One with / DAO services and the other with a controller. If you configured everything in one spring configuration file, you do not need ContextLoaderListener , a servlet dispatcher is enough.

It is recommended that you split the configuration into two and use the ContextLoaderListener to create the root context of the application and the dispatcher servlet to create the web-level application context.

+13
Jun 13 '12 at 12:30
source share

This is optional, you really don't need it just for Spring MVC ( DispatcherServlet will do). But adding Spring security to your Spring MVC should be done with

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

Just one note, if you are using ContextLoaderListener , you will need to add 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> 

in your web.xml. Sorry for being too late. Greetings

+4
Aug 08 '16 at 19:30
source share
 <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> 

It worked for me.

+3
Jul 07 '13 at 17:31
source share



All Articles