Close

Spring MVC - ParameterizableViewController Example

[Last Updated: 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 1.5.4.RELEASE
    Corresponding Spring Version 4.3.9.RELEASE
  • spring-boot-starter-web : Starter for building web, including RESTful, applications using Spring MVC. Uses Tomcat as the default embedded container.
  • 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
                • Main.java
          • resources
          • webapp
            • WEB-INF
              • pages

    See Also