Register ViewResolver with Spring MVC

Let's register the JSP-related ViewResolver, that is, configure InternalResourceViewResolver in the Spring web application, as follows:

     package com.packt.patterninspring.chapter10.bankapp.web.mvc; 
 
     import org.springframework.context.annotation.Bean; 
     import org.springframework.context.annotation.ComponentScan; 
     import org.springframework.context.annotation.Configuration; 
     import org.springframework.web.servlet.ViewResolver; 
     import org.springframework.web.servlet.config.annotation.EnableWebMvc; 
     import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; 
     import org.springframework.web.servlet.view.InternalResourceViewResolver; 
 
     @Configuration 
     @ComponentScan(basePackages = {"com.packt.patterninspring.chapter10.bankapp.web.controller"})       
     @EnableWebMvc 
     public class SpringMvcConfig extends WebMvcConfigurerAdapter{ 
       .... 
        @Bean 
         public ViewResolver viewResolver(){ 
         InternalResourceViewResolver viewResolver = new InternalResourceViewResolver(); 
         viewResolver.setPrefix("/WEB-INF/views/"); 
         viewResolver.setSuffix(".jsp"); 
         return viewResolver; 
     } 
      .... 
   } 

As in the preceding code, suppose the controller returns with the logical view name, accountDetails. All the JSP files for views are placed in the /WEB-INF/views/ directory of the web application. The accountDetails.jsp view file for account details. As per the preceding configuration file, the actual view name is derived by adding the prefix /WEB-INF/views/ and the postfix .jsp to the logical view name returned by the application controller. If the application controller returns accountDetails as the logical view name, then ViewResolver changes it to the physical by adding a prefix and postfix to the logical view name; finally, it is changed to /WEB-INF/views/accountDetails.jsp in the our application. The following diagram illustrates how Spring MVC's Front Controller resolves the view in a Spring web application:

This last diagram illustrates the whole picture of the Spring MVC request flow with all the components (Model, View, and Controllers) of the MVC pattern, and the Front controller pattern. Any request, either HTTP GET or POST, lands at the Front Controller first, which is, actually, the DispatcherServlet in Spring MVC. The controllers in a Spring web application are responsible for generating and updating the Model, and the Model is another component of the MVC pattern. Finally, the controller returns that model along with the logical view name to the DispatcherServlet. It consults with the configured view resolver, and resolves the physical path of the view. The View is another component of the MVC pattern.

In the next section, we'll elaborate on the View Helper pattern, and how Spring support the pattern in a Spring web application.

..................Content has been hidden....................

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