I have a program I'm running in a Spring project which always fails because of a java.io.FileNotFoundException
, when it comes to locating the DispatcherServlet
.
The DispatcherServlet
lives in the \WEB-INF
folder and is accessible to the rest of the project without incident.
So at the moment I'm forced to hardcode the path to the DispatcherServlet
as follows:
File config = new File("C:\\project\\build\\web\\WEB-INF\\project-servlet.xml");
boolean exists = Misc.checkFileExists(config.getAbsolutePath());
if (exists) {
System.out.println("File: " + config.getAbsolutePath() + " found.");
}
ConfigurableApplicationContext context = new FileSystemXmlApplicationContext(config.getAbsolutePath());`
Which is not the best way at all.
But if I try to place the DispatcherServlet
in a folder under \WEB-INF
, e.g. \WEB-INF\resources
to satisfy the CLASSPATH
, the file is still not found. Because of this I can't use ClassPathXmlApplicationContext
.
I have resolved this by setting my web.xml
file as follows:
<servlet>
<servlet-name>project</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/project-servlet.xml</param-value>
</init-param>
</servlet>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/project-servlet.xml</param-value>
</context-param>
The application works as does the test program with:
ConfigurableApplicationContext context = new ClassPathXmlApplicationContext("project-servlet.xml");
I should add that I have a single project-servlet.xml
file which configures everything.