The following content is translated from: https://www.tutorialspoint.com/springmvc/springmvc_parameterizableviewcontroller.htm
Description: The sample is based on spring MVC 4.1.6.
The following example shows how to use the Parameterizable View controller method of a multi-action controller using the Spring WEB MVC framework. A parameterized view allows Web pages to be mapped using requests.
PackageCom.tutorialspoint;Importjavax.servlet.http.HttpServletRequest;ImportJavax.servlet.http.HttpServletResponse;ImportOrg.springframework.web.servlet.ModelAndView;ImportOrg.springframework.web.servlet.mvc.multiaction.MultiActionController; Public classUsercontrollerextendsmultiactioncontroller{ PublicModelandview Home (httpservletrequest request, httpservletresponse response)throwsException {Modelandview model=NewModelandview ("User"); Model.addobject ("Message", "Home"); returnmodel; } }
<Beanclass= "Org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> < Propertyname= "Mappings"> <value>Index.htm=usercontroller</value> </ Property></Bean><BeanID= "Usercontroller"class= "Org.springframework.web.servlet.mvc.ParameterizableViewController"> < Propertyname= "ViewName"value= "User"/></Bean>
For example, using the above configuration, if it is a URI
/index.htm is requested, dispatcherservlet forwards the request to the Usercontroller controller, where ViewName is set to user.jsp.
First, let's use the Eclipse IDE and follow these steps to develop a dynamic form-based Web application using the Spring Web framework:
Steps |
Description |
1 |
Create a project named TestWeb, under a package com.tutorialspoint, as described in the Spring Mvc-hello World Example section. |
2 |
Create a Java class Usercontroller under the Com.tutorialspoint package. |
3 |
Create a view file under the JSP subfolder user.jsp. |
4 |
The final step is to create the contents of all the source and configuration files and export the application as described below. |
Usercontroller.java
PackageCom.tutorialspoint;Importjavax.servlet.http.HttpServletRequest;ImportJavax.servlet.http.HttpServletResponse;ImportOrg.springframework.web.servlet.ModelAndView;ImportOrg.springframework.web.servlet.mvc.multiaction.MultiActionController; Public classUsercontrollerextendsmultiactioncontroller{ PublicModelandview Home (httpservletrequest request, httpservletresponse response)throwsException {Modelandview model=NewModelandview ("User"); Model.addobject ("Message", "Home"); returnmodel; }}
Testweb-servlet.xml
<Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:context= "Http://www.springframework.org/schema/context"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-3.0. XSD Http://www.springframework.org/schema/context http://www.springframework.org/schema/context/ Spring-context-3.0.xsd "> <Beanclass= "Org.springframework.web.servlet.view.InternalResourceViewResolver"> < Propertyname= "prefix"value= "/web-inf/jsp/"/> < Propertyname= "suffix"value= ". jsp"/> </Bean> <Beanclass= "Org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> < Propertyname= "Mappings"> <value>Index.htm=usercontroller</value> </ Property> </Bean> <BeanID= "Usercontroller"class= "Org.springframework.web.servlet.mvc.ParameterizableViewController"> < Propertyname= "ViewName"value= "User"/> </Bean></Beans>
user.jsp
<%@ Page ContentType="text/html; Charset=utf-8" %><HTML><Head><title>Hello World</title></Head><Body> <H2>Hello World</H2> </Body></HTML>
After you finish creating the source files and profiles, export the application. Right-click the application and use the Export > WAR file option and save your testweb.war file in the Tomcat WebApps folder.
Now start your Tomcat server and make sure you can access other pages from the WebApps folder using a standard browser. Now try URL http://localhost:8080/TestWeb/index.htm, if everything in your spring Web application is fine, you should see the following results:
Maven Example:
Spring mvc-Controller-parametric View Controller (parameterizable) example (reprint practice)