Chapter 2. Spring MVC Architecture – Architecting Your Web Store

What we saw in the first chapter was nothing but a glimpse of Spring MVC. Our total focus was just to get a Spring MVC application running. Now it's time for us to deep dive into the Spring MVC architecture.

By the end of this chapter, you will have a clear understanding of:

  • The Dispatcher servlet and request mapping
  • Web application context and configuration
  • Spring MVC request flow and Web MVC
  • A typical Spring web application architecture

Dispatcher servlet

In the first chapter, we provided a little introduction to the Dispatcher servlet and you saw how to configure a Dispatcher servlet using the DispatcherServletInitializer class. You learned that every web request first comes to the Dispatcher servlet. The Dispatcher servlet is the thing that decides which controller method the web request should be dispatched to. In the previous chapter, we created a welcome page that will be shown whenever we enter the URL http://localhost:8080/webstore/ in the browser. Mapping a URL to the appropriate controller method is the primary duty of the Dispatcher servlet.

So the Dispatcher servlet reads the web request URL and finds the appropriate controller method that can serve that web request and invokes it. This process of mapping a web request onto a specific controller method is called request mapping. And the Dispatcher servlet is able to do this with the help of the @RequestMapping (org.springframework.web.bind.annotation.RequestMapping) annotation.

Time for action - examining request mapping

Let's observe what will happen when we change the value attribute of the @RequestMapping annotation.

  1. Open your STS and run your webstore project; just right-click on your project and choose Run As | Run on Server. Now you will be able to see the same welcome message in the browser.
  2. Now go to the address bar of the browser and enter the following URL http://localhost:8080/webstore/welcome.
  3. You will see the HTTP Status 404 error page in the browser, and you will also see the following warning in the console:
            WARNING: No mapping found for HTTP request with URI
            [/webstore/welcome] in DispatcherServlet with name
            'DefaultServlet'
    
    Time for action - examining request mapping

    Error showing no mapping found message

  4. Now open your HomeController class and change the @RequestMapping annotation's value attribute to /welcome and save it. Basically, your new request mapping annotation will look like as follows: @RequestMapping("/welcome").
  5. Again, run your application and enter the same URL that you entered in step 2. Now you should be able to see the same welcome message again in the browser without any request mapping error.

What just happened?

After starting our application, when we enter the URL http://localhost:8080/webstore/welcome in the browser; the Dispatcher servlet (org.springframework.web.servlet.DispatcherServlet) immediately tried to find a matching controller's method for the request path /welcome.

Tip

In a Spring MVC application, a URL can be logically divided into five parts; see the figure that is present after this tip. The @RequestMapping annotation only matches against the URL request path; it will omit the scheme, host name, application name, and so on. Here the application name is just a context name where the application is deployed—it is totally under the control of how we configure the web server.

The @RequestMapping annotation has one more attribute called method to further narrow down the mapping based on HTTP request method types (GET, POST, HEAD, OPTIONS, PUT, DELETE, TRACE). If we do not specify the method attribute in the @RequestMapping annotation, the default method would be GET. You will see more about the method attribute of the @RequestMapping annotation in Chapter 4, Working with Spring Tag Libraries under Time for action – serving and processing forms.

What just happened?

The logical parts of a typical Spring MVC application URL

But since we don't have a corresponding request mapping for the given URL path /welcome, we are getting the HTTP status 404 error in the browser and the following error log in the console:

WARNING: No mapping found for HTTP request with URI [/webstore/welcome] in DispatcherServlet with name 'DefaultServlet'

From the error log, we can clearly understand that there is no request mapping for the URL path /webstore/welcome. So we are trying to map this URL path to the existing controller's method; that's why in step 4 we only put the request path value /welcome in the @RequestMapping annotation as the value attribute. Now everything works perfectly fine.

Pop quiz – request mapping

Suppose I have a Spring MVC application for library management called BookPedia and I want to map a web request URL http://localhost:8080/BookPedia/category/fiction to a controller's method—how would you form the @RequestMapping annotation?

  1. @RequestMapping("/fiction")
  2. @RequestMapping("/category/fiction")
  3. @RequestMapping("/BookPedia/category/fiction")

What is the request path in the following URL http://localhost:8080/webstore/?

  1. webstore/
  2. /
  3. 8080/webstore/
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.135.199.27