Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Could not open ServletContext resource [/WEB-INF/applicationContext.xml]

Ok, I am 500th user asking this question, I read many answers but still having no luck.

parent module pom contains:

<dependency>     <groupId>org.springframework</groupId>     <artifactId>spring-web</artifactId>     <version>${spring.framework.version}</version> </dependency> <dependency>     <groupId>org.springframework</groupId>     <artifactId>spring-webmvc</artifactId>     <version>${spring.framework.version}</version> </dependency> 

Child module has maven-jetty-plugin and I run my webapp module with jetty:run.

web.xml defines standard dispatcher module:

<listener>     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>  <servlet>     <servlet-name>dispatcher</servlet-name>     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>     <load-on-startup>1</load-on-startup> </servlet>  <servlet-mapping>     <servlet-name>dispatcher</servlet-name>     <url-pattern>/</url-pattern> </servlet-mapping> 

I have file dispatcher-servlet.xml under WEB-INF, though start fails with:

FileNotFoundException: Could not open ServletContext resource [/WEB-INF/applicationContext.xml] 

What is wrong? Documentation and everybody says that Spring MVC will search for XX-servlet.xml, where XX is name of servlet. Why does it search for applicationContext.xml?

like image 519
Leos Literak Avatar asked Mar 12 '14 11:03

Leos Literak


2 Answers

ContextLoaderListener has its own context which is shared by all servlets and filters. By default it will search /WEB-INF/applicationContext.xml

You can customize this by using

<context-param>     <param-name>contextConfigLocation</param-name>     <param-value>/WEB-INF/somewhere-else/root-context.xml</param-value> </context-param> 

on web.xml, or remove this listener if you don't need one.

like image 104
gerrytan Avatar answered Oct 17 '22 23:10

gerrytan


Update: This will create a second context same as in applicationContext.xml

or you can add this code snippet to your web.xml

<servlet>     <servlet-name>spring-dispatcher</servlet-name>     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>         <init-param>           <param-name>contextConfigLocation</param-name>           <param-value>classpath:applicationContext.xml</param-value>         </init-param>     <load-on-startup>1</load-on-startup> </servlet> 

instead of

<servlet>     <servlet-name>dispatcher</servlet-name>     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>     <load-on-startup>1</load-on-startup> </servlet> 
like image 22
Aalkhodiry Avatar answered Oct 17 '22 21:10

Aalkhodiry