ArticleDirectory
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.