user1865162
user1865162

Reputation:

Where is the exact location of spring config file and web.xml?

Getting the Class Not Found Exception for DispatcherServlet while rendering index.jsp which is in WEB-CONTENT/WEB-INF/jsp/index.jsp

Following are how the project is structured.

  1. web.xml is under WEB-CONTENT.
  2. abc is the name of my Dispatcher servlet. So config file will be abc-servlet.xml which will contain the bean tag with all the namespace and schemas defined.
  3. Where should i place the abc-servlet.xml file? Should it be in the classes folder or where the web.xml is?
  4. Is the exception arising because of the location of the spring config file?
  5. Also, what if i place the config file in other location how do i let the project know it is at that particular path in the project?

I am using annotation driven controller in the smaple project.

Upvotes: 6

Views: 29737

Answers (2)

Kevin Bowersox
Kevin Bowersox

Reputation: 94459

From the documentation:

Upon initialization of a DispatcherServlet, Spring MVC looks for a file named [servlet-name]-servlet.xml in the WEB-INF directory of your web application and creates the beans defined there, overriding the definitions of any beans defined with the same name in the global scope.

So placing abc-servlet.xml within WEB-INF should allow the dispatcher servlet to pick up your configuration.

If you didn't want your dispatcher servlet to use the default name or wanted it to reside in another directory besides WEB-INF you would specify this configuration in web.xml. The location and name of the dispatcher servlets configuration can be changed by setting the contextConfigLocation init-param within the DispatcherServlet

<servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>WEB-INF/spring/servlet-context.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

This information can be found within the Spring Documentation

Upvotes: 8

Dhanush Gopinath
Dhanush Gopinath

Reputation: 5739

web.xml is placed under WEB-INF and then in that you can refer your spring xml like this:

<servlet>
    <servlet-name>myservlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/abc-servlet.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
    <async-supported>true</async-supported>
</servlet>

Upvotes: 3

Related Questions