Spring MVC - ParameterizableViewController Example

[Updated: Jul 24, 2017, Created: Jul 24, 2017]

In this example we are going to use ParameterizableViewController directly to map URL to a view instead of using ViewControllerRegistry.

Example

The configuration class

@Configuration
@SpringBootApplication
public class Main {
  
  @Bean
  public ParameterizableViewController myViewController () {
      ParameterizableViewController c = new ParameterizableViewController();
      c.setViewName("myView");
      c.setStatusCode(HttpStatus.OK);
      return c;
  }
  
  @Bean
  public HandlerMapping myHandlerMapping () {
      SimpleUrlHandlerMapping m = new SimpleUrlHandlerMapping();
      Map<String, Object> map = new HashMap<>();
      map.put("/test", myViewController());
      m.setUrlMap(map);
      m.setOrder(1);
      return m;
  }
  
  public static void main (String[] args) {
      SpringApplication.run(Main.class, args);
  }
}

In above configuration, we are mapping path '/test' to view 'myView'.

The View

src/main/webapp/WEB-INF/pages/myView.jsp

<html>
  <body style="margin:20px;">
  JSP view
   <p>

   </p>
   <p>
      Response Status: <%=response.getStatus() %>
   </p>
  </body>
</html>

Spring Boot properties

src/main/resources/application.properties

spring.mvc.view.prefix= /WEB-INF/pages/
spring.mvc.view.suffix= .jsp

Running web application

mvn spring-boot:run

Output

Example Project

Dependencies and Technologies Used :

  • spring-boot-starter-web 1.5.4.RELEASE: Starter for building web, including RESTful, applications using Spring MVC. Uses Tomcat as the default embedded container.
    Corresponding Spring version: 4.3.9.RELEASE
  • tomcat-embed-jasper 8.5.15: Core Tomcat implementation.
  • JDK 1.8
  • Maven 3.3.9

Spring Parameterizable View Controller Example Select All Download
  • parameterizable-view-controller-example
    • src
      • main
        • java
          • com
            • logicbig
              • example
        • resources
        • webapp
          • WEB-INF
            • pages

See Also