If you want to use multiple view parsers in a spring MVC Application, you can use order properties to set the order of precedence. The following example shows how to use and in the Spring Web MVC framework ResourceBundleViewResolver InternalResourceViewResolver .
The multipleresolver-servlet.xml configuration is as follows-
<beanClass="Org.springframework.web.servlet.view.ResourceBundleViewResolver"><propertyName="BaseName"Value="Views"/><propertyName=The order"Value="0"/></bean><beanClass="Org.springframework.web.servlet.view.InternalResourceViewResolver"><propertyName="Prefix"Value="/web-inf/jsp/"/><propertyName="Suffix"Value=". jsp" /> <property name= "order" value="1 " /></bean>
Xml
This order property defines the sort of the view parser. 0as the first parser, 1 as the next parser, and so on.
The views.properties configuration is as follows-
hello.(class)=org.springframework.web.servlet.view.JstlViewhello.url=/WEB-INF/jsp/hello.jsp
Shell
For example, using the above configuration, if URI :
- For a
/hello request, the DispatcherServlet request is forwarded to the corresponding defined in the views.properties hello hello.jsp .
- Here "
hello " is the name of the view to match. classSpecifies the view type, which url is the location of the view.
First, use the Eclipse IDE and follow these steps to develop a dynamic form-based Web application using the Spring Web Framework :
- Create a Dynamic Web project called multipleresolver .
com.yiibai.springmvcCreate a Java class under the package HelloController .
jspCreate a view file under sub-folders: hello.jsp .
srcCreate a property file under a views.properties folder.
- Download Jstl Library Jstl.jar. Put it in the
CLASSPATH middle.
- The final step is to create the contents of all the source and configuration files and run the application, as described in detail below.
The complete project file directory structure is as follows-
The code for Hellocontroller.java is as follows-
Package com. Yiibai. Springmvc;import org. springframework. stereotype. Controller;import org. springframework. Web. bind. annotation. Requestmapping;import org. springframework. Web. bind. annotation. Requestmethod;import org. springframework. UI. Modelmap;@Controller@RequestMapping("/hello")PublicClassHellocontroller{@RequestMapping(Method = Requestmethod. GET) public String Printhello(modelmap model) {model. AddAttribute("message", "hello,spring MVC resource-bound view resolver!") ); return "Hello"; }}< /c2>
Java
The multipleresolver-servlet.xml configuration is as follows-
<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"><Context:component-scanBase-package="Com.yiibai"/><beanClass="Org.springframework.web.servlet.view.ResourceBundleViewResolver"><propertyName="BaseName"Value="Views"/><propertyName=The order"Value="0"/></bean><beanClass="Org.springframework.web.servlet.view.InternalResourceViewResolver"><propertyName="Prefix"Value="/web-inf/jsp/"/><propertyName="Suffix"Value=The. JSP" /> <property name=" Order" value=" 1" /> </ Beans></beans>
Xml
The views.properties configuration is as follows-
hello.(class)=org.springframework.web.servlet.view.JstlViewhello.url=/WEB-INF/jsp/hello.jsp
The code for hello.jsp is as follows-
<%@PageContentType="Text/html; Charset=utf-8"%>>><title>hello World</title>><body> >${message}></ Body>>
Html
After you finish creating the source and configuration files, publish the application to the Tomcat server.
Now start the Tomcat server, when access URL = = Http://localhost:8080/MultipleResolver/hello, if the Spring Web application does not have a problem, you should see the following results:
Spring MVC multi-parser mapping