Spring MVC multi-parser mapping

Source: Internet
Author: User
Tags tomcat server

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 :

    1. Create a Dynamic Web project called multipleresolver .
    2. com.yiibai.springmvcCreate a Java class under the package HelloController .
    3. jspCreate a view file under sub-folders: hello.jsp .
    4. srcCreate a property file under a views.properties folder.
    5. Download Jstl Library Jstl.jar. Put it in the CLASSPATH middle.
    6. 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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.