Parameterize View Name with ParameterizableViewController in Spring MVC
- Details
- Written by Nam Ha Minh
- Last Updated on 20 June 2019   |   Print Email
Normally in Spring MVC, we specify view names in handler methods of a controller class. For example:
@RequestMapping("/") public String visitHome() { return "home"; }
This handler method returns a logical view named home. What if we want to change this view name after deployment without touching the source code and re-compiling the project? Spring provides the ParameterizableViewController class which is used for parameterizing view name in configuration file instead of writing hard-coded view name in the handler method.
Typically, a ParameterizableViewController bean should be used in conjunction with a view resolver (e.g. InternalResourceViewResolver) and a URL handler mapping (e.g. SimpleUrlHandlerMapping). Let’s see a couple of examples.
1. ParameterizableViewController with XML Configuration
Firstly, declare a view resolver as following code snippet:
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/" /> <property name="suffix" value=".jsp" /> </bean>
This is a very typical view resolver in Spring MVC.
Secondly, declare a ParameterizableViewController bean as the following code:
<bean id="homeViewController" class="org.springframework.web.servlet.mvc.ParameterizableViewController"> <property name="viewName" value="home"/> </bean>
As you can see, this bean defines the value “home” for the view name. This name can be changed when needed without re-compiling code, hence the parameterization.
Thirdly, we need to declare a URL handler mapping which makes a certain URL pattern to be handled by a controller - the ParameterizableViewControllerin this case. Here’s the XML code:
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="mappings"> <value> index.html=homeViewController </value> </property> </bean>
The ParameterizableViewControllerclass also has a statusCodeproperty which can be useful when you want to return a specific HTTP status code for the matching URL. For example:
<bean id="homeViewController" class="org.springframework.web.servlet.mvc.ParameterizableViewController"> <property name="viewName" value="home"/> <property name="statusCode"> <value type="org.springframework.http.HttpStatus"> INTERNAL_SERVER_ERROR </value> </property> </bean>
That sets HTTP status code 500 (Internal Server Error) for the response. In this case, you should change the view name to your HTTP 500 error page.
2. ParameterizableViewController with Java-based Configuration
For those who prefer Java-based configuration over XML, here’s a Java configuration class that does the same thing as the XML above:
package net.codejava.spring; import java.util.Properties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.handler.SimpleUrlHandlerMapping; import org.springframework.web.servlet.mvc.ParameterizableViewController; import org.springframework.web.servlet.view.InternalResourceViewResolver; @Configuration public class MvcConfiguration { @Bean public InternalResourceViewResolver getViewResolver() { InternalResourceViewResolver viewResolver = new InternalResourceViewResolver(); viewResolver.setPrefix("/WEB-INF/views/"); viewResolver.setSuffix(".jsp"); return viewResolver; } @Bean(name = "homeViewController") public ParameterizableViewController getHomeViewController() { ParameterizableViewController viewController = new ParameterizableViewController(); viewController.setViewName("home"); return viewController; } @Bean public SimpleUrlHandlerMapping getUrlHandlerMapping() { SimpleUrlHandlerMapping handlerMapping = new SimpleUrlHandlerMapping(); Properties mappings = new Properties(); mappings.put("/index.html", "homeViewController"); handlerMapping.setMappings(mappings); return handlerMapping; } }
For your convenience, we provide a sample project which can be downloaded from the Attachments section.
Reference:Class ParameterizableViewController
Related Spring Tutorials:
- Spring MVC XstlView and XsltViewResolver Example
- Understand the core of Spring framework
- Understand Spring MVC
- Spring MVC beginner tutorial with Spring Tool Suite IDE
- Spring MVC Form Handling Tutorial
- Spring MVC Form Validation Tutorial
- Spring MVC with JdbcTemplate Example
- Spring MVC + Spring Data JPA + Hibernate - CRUD Example
- 14 Tips for Writing Spring MVC Controller
- Spring and Hibernate Integration Tutorial Part (XML Configuration)
Comments