Spring MVC ParameterizableViewController example

In general, to return a view or page in Spring MVC application, you need to create a class, which extends the AbstractController , and return a ModelAndView() object.

public class WelcomeController extends AbstractController{

    @Override
    protected ModelAndView handleRequestInternal(HttpServletRequest request,
        HttpServletResponse response) throws Exception {

        ModelAndView model = new ModelAndView("WelcomePage");
        return model;

    }

}

In the bean configuration file, declared a ControllerClassNameHandlerMapping to auto detect the mapping.

<bean 
 class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping" />

<bean class="com.mkyong.common.controller.WelcomeController" />

But, don’t you think it’s too much configuration for a simple redirect task? Fortunately, Spring comes with ParameterizableViewController to simplify the above processes. With ParameterizableViewController, you don’t need to hard code the view name in the controller class anymore, instead, you put view name declarative through the ParameterizableViewController’s “viewName” property.

Note
The ParameterizableViewController is a subclass of AbstractController, and return a ModelAndView based on the “viewName” property, it’s purely a redirect class, nothing more, nothing less :)

ParameterizableViewController.java
public class ParameterizableViewController extends AbstractController{
//...
protected ModelAndView handleRequestInternal(
    HttpServletRequest request, HttpServletResponse response)
    throws Exception {

    return new ModelAndView(getViewName());
}

Tutorial

In this tutorial, it shows the use of ParameterizableViewController controller to do a page redirection in the Spring MVC application.

1. ParameterizableViewController

No controller class is required, just declared the ParameterizableViewController bean and specify the view name through the “viewName” property. Additionally, you have to define an explicit mapping for it.

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

    <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="mappings">
            <props>
                <prop key="/welcome.htm">welcomeController</prop>
            </props>
        </property>
    </bean>

   <bean name="welcomeController" 
            class="org.springframework.web.servlet.mvc.ParameterizableViewController">
        <property name="viewName" value="WelcomePage" />
   </bean>

   <bean id="viewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
            <property name="prefix">
              <value>/WEB-INF/pages/</value>
            </property>
            <property name="suffix">
              <value>.jsp</value>
            </property>
    </bean>

</beans>

Define an explicit mapping is required.

<beans ...>
//...
<bean 
class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping" />

<bean name="welcomeController" 
    class="org.springframework.web.servlet.mvc.ParameterizableViewController">
    <property name="viewName" value="WelcomePage" />
</bean>
//...
</beans>

In above snippet, are you expect a view name “welcome” will return a “WelcomePage”? Sorry, it’s not, you have to define an explicit mapping, because the ControllerClassNameHandlerMapping won’t generate a mapping for any built-in Spring MVC controller.

2. View

Just a simple JSP to display a head line.

WelcomePage.jsp.jsp

<html>
<body>
<h2>ParameterizableViewController Example</h2>
</body>
</html>

3. Demo

Access it via “http://localhost:8080/SpringMVC/welcome.htm“, the “welcome.htm” will return back a “/WEB-INF/pages/WelcomPage.jsp“.
SpringMVC-ParameterizableViewController-Example-1

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值