Spring MVC Beginner’s Guide
上QQ阅读APP看书,第一时间看更新

Time for action – understanding the web application context

You have received enough of an introduction on the web application context; now, tweak a little bit with the name and location of the web application context configuration file (DefaultServlet-servlet.xml) and observe the effect. Perform the following steps:

  1. Rename the DefaultServlet-servlet.xml file to DispatcherServlet-servlet.xml; you can find DefaultServlet-servlet.xml under the src/main/webapp/WEB-INF/ directory.
  2. Then, run your webstore project again and enter the URL, http://localhost:8080/webstore/; you will see an HTTP Status 500 error message on your web page and a FileNotFoundException error in the stack trace as follows:
    java.io.FileNotFoundException: Could not open ServletContext resource [/WEB-INF/DefaultServlet-servlet.xml]
    
    Time for action – understanding the web application context

    An error message displaying FileNotFoundException for DefaultServlet-servlet.xml

  3. To fix this error, change the name of DefaultServlet to DispatcherServlet in web.xml; basically, after changing the name to DispatcherServlet, your servlet configuration will look like the following in the web.xml file:
    <servlet>
      <servlet-name>DispatcherServlet</servlet-name>
      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    </servlet>
      
    <servlet-mapping>
      <servlet-name>DispatcherServlet</servlet-name>
      <url-pattern>/</url-pattern>
    </servlet-mapping>
  4. Now, run your application and enter the URL, http://localhost:8080/webstore/; you will see the welcome message again.
  5. Rename your DispatcherServlet-servlet.xml file to DispatcherServlet-context.xml once more.
  6. Next, create a directory structure spring/webcontext/ under the WEB-INF directory and move the DispatcherServlet-context.xml file to the src/main/webapp/WEB-INF/spring/webcontext/ directory.
  7. Then, run your application, and you will see an HTTP Status 500 error message on your web page again and a FileNotFoundException error message in the stack trace:
    java.io.FileNotFoundException: Could not open ServletContext resource [/WEB-INF/DispatcherServlet-servlet.xml]
    
  8. To fix this error, add the following tags within the <servlet> and </ servlet> tags in web.xml as shown in the following code:
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>
        /WEB-INF/spring/webcontext/DispatcherServlet-context.xml
      </param-value>
    </init-param>
  9. Now, run the application again and enter the URL, http://localhost:8080/webstore/; you will be able to see the welcome message again.

What just happened?

So, what we did first was renamed the DefaultServlet-servlet.xml file to DispatcherServlet-servlet.xml, and we got a FileNotFoundException error at runtime, as follows:

java.io.FileNotFoundException: Could not open ServletContext resource [/WEB-INF/DefaultServlet-servlet.xml]

To fix the error, we changed our dispatcher servlet configuration, as follows, in the web.xml file:

<servlet>
  <servlet-name>DispatcherServlet</servlet-name>
  <servlet-class> org.springframework.web.servlet.DispatcherServlet 
  </servlet-class>
</servlet>

<servlet-mapping>
  <servlet-name>DispatcherServlet</servlet-name>
  <url-pattern>/</url-pattern>
</servlet-mapping>

We changed the servlet name to DispatcherServlet in order to align with the web application context configuration file named DispatcherServlet-servlet.xml. So, based on this exercise, we can learn that during the start-up of any Spring MVC project, the dispatcher servlet will look for a web application context configuration file of the pattern <Configured dispatcher Servlet Name>-servlet.xml under the WEB-INF directory. It is our responsibility to keep the web application context configuration file under the WEB-INF directory with the right name. However, what if we wish to keep the file in some other directory?

Tip

One of the important things to be noted in <servlet-mapping> is the value of the <url-pattern>/</url-pattern> tag. By assigning / as the URL pattern for the dispatcher servlet, we make DispatcherServlet the default servlet for our web application. So, every web request coming to our web application will be handled by DispatcherServlet.

For instance, in steps 5 and 6, we renamed the web application context configuration file and moved it to a completely new directory (src/main/webapp/WEB-INF/spring/webcontext/). In that case, how did we fix the HTTP Status 500 error? The answer lies within a property called contextConfigLocation. For the dispatcher servlet to locate the web context configuration file easily, we gave the location of this file to the dispatcher servlet through a property called contextConfigLocation. That's why we added this property to the dispatcher servlet in step 8, as follows:

<servlet>
  <servlet-name>DispatcherServlet</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <init-param>
 <param-name>contextConfigLocation</param-name>
 <param-value>
 /WEB-INF/spring/webcontext/DispatcherServlet-context.xml
 </param-value>
 </init-param>
</servlet>

Now, we are able to run our application without any problem. Okay, we played a lot with the web application context configuration file and learned that the dispatcher servlet should know about the web application context configuration file during the start-up of our project. So the next question is: why is the dispatcher servlet looking for this web context configuration file, and what is defined inside this file? Let's find out the answer, but before that, you may answer the following pop quiz questions to make sure you understand the concept of the web application context configuration.

Pop quiz – the web application context

Q1. If the contextConfigLocation property was not configured in our dispatcher servlet configuration, under which location would Spring MVC look for the web application context configuration file?

  1. In the WEB-INF directory
  2. In WEB-INF/spring
  3. In WEB-INF/spring/appServlet

Q2. If we do not want to provide contextConfigLocation to the following dispatcher servlet configuration, how do we avoid the HTTP Status 500 error?

<servlet>
  <servlet-name>FrontController</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

</servlet>
  1. By creating a context file called FrontController-context.xml in the WEB-INF directory
  2. By creating a file called DispatcherServlet-context.xml in WEB-INF
  3. By creating a file called FrontController-servlet.xml in WEB-INF