Spring MVC ParameterizableViewController example
来源:互联网 发布:小学数学网络研修日志 编辑:程序博客网 时间:2024/06/11 00:53
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
TheParameterizableViewController
is a subclass ofAbstractController
, and return aModelAndView
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
“.
- Spring MVC ParameterizableViewController example
- Spring MVC ParameterizableViewController example
- Spring MVC with ParameterizableViewController Tutorial and Example
- Spring MVC - DispatcherServlet Example
- Spring MVC Model Example
- Spring MVC MultiActionController example
- Spring MVC PropertiesMethodNameResolver example
- Spring MVC ParameterMethodNameResolver example
- Spring MVC ParameterMethodNameResolver example
- Spring MVC InternalResourceViewResolver example
- Spring MVC XmlViewResolver example
- Spring MVC ResourceBundleViewResolver example
- Spring MVC BeanNameUrlHandlerMapping example
- Spring MVC ControllerClassNameHandlerMapping example
- Spring MVC SimpleUrlHandlerMapping example
- Spring MVC RedirectView example
- Spring MVC textbox example
- Spring MVC BeanNameUrlHandlerMapping example
- Hadoop学习笔记1
- slick使用普通的sql操作数据
- 如何搭建mybatis3项目
- 打印昨天的现在时刻
- 音频录制
- Spring MVC ParameterizableViewController example
- uva 508
- OpenCV学习笔记(一)——OpenCV数据结构Mat详解
- TOMCAT配置HTTPS和SSL并HTTP请求强转为HTTPS请求
- 4、Logistic回归
- 关注分离的艺术
- 大数据性能调优之HBase的RowKey设计
- Error:No suitable device found: no device found for connection "System eth0"
- 开学第一周