Spring MVC view Parser: configure the priority of multiple view Resolvers

Source: Internet
Author: User
ArticleDirectory
    • Problem
    • Solution
Problem

Application in spring MVCProgramIn, we often need to apply some view parser policies to parse view names. For example, three view Resolvers are used together: internalresourceviewresolver, resourcebundleviewresolver, and xmlviewresolver.

However, if a view name is returned, which view parser policy is used?

Solution

If multiple view parser policies are applied, the priority must be declared through the "order" attribute. The lower the order value, the higher the priority. For example:

<? XML version = "1.0" encoding = "UTF-8"?> <Beans xmlns = "http://www.springframework.org/schema/beans" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns: P = "http://www.springframework.org/schema/p" xmlns: context = "http://www.springframework.org/schema/context" xmlns: MVC = "http://www.springframework.org/schema/mvc" xsi: schemalocation = "http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3. 0. xsdhttp: // www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd "> <! -- Scan the Web package and apply the spring annotation --> <context: component-scan base-package = "com. qunar. training "/> <Bean class =" org. springframework. web. servlet. view. resourcebundleviewresolver "> <property name =" basename "> <value> spring-views </value> </property> <property name =" order "value =" 0 "/> </bean> <Bean class = "org. springframework. web. servlet. view. xmlviewresolver "> <property name =" location "> <value>/WEB-INF/spring-views.xml </value> </property> <property name =" order "value =" 1 "/> </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> <property name = "order" value = "2"/> </bean> </beans>

Note: internalresourceviewresolver must always give the lowest priority (the maximum order value), because no matter what view name is returned, it will parse the view. If its priority is higher than the priority of other parsers, it will make other parsers with lower priority have no chance to parse the view.

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.