博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring MVC视图解析器:配置多个视图解析器的优先级
阅读量:7225 次
发布时间:2019-06-29

本文共 1606 字,大约阅读时间需要 5 分钟。

问题

在Spring MVC应用程序中,我们经常需要应用一些视图解析器策略来解析视图名称。例如,联合使用三个视图解析器:InternalResourceViewResolver、ResourceBundleViewResolver和XmlViewResolver。

但是,如果返回了一个视图的名称,那么,使用哪一个视图解析器策略?

解决方法

如果应用了多个视图解析器策略,那么就必须通过“order”属性来声明优先级,order值越低,则优先级越高。例如:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<?xml version=
"1.0" 
encoding=
"UTF-8" 
?>
<beans xmlns=
""
       
xmlns:xsi=
"" 
xmlns:p=
""
       
xmlns:context=
""
       
xmlns:mvc=
""
       
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  ">
 
    
<!-- 扫描web包,应用Spring的注解 -->
    
<context:component-scan base-
package
=
"com.xxx.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>

  注意:InternalResourceViewResolver必须总是赋予最低的优先级(最大的order值),因为不管返回什么视图名称,它都将解析视图。如果它的优先级高于其它解析器的优先级的话,它将使得其它具有较低优先级的解析器没有机会解析视图。

转载地址:http://gieym.baihongyu.com/

你可能感兴趣的文章
免费的编程中文书籍索引
查看>>
使用scp在windows和Linux之间互传文件
查看>>
Struts ActionContext和ServletActionContext小结
查看>>
LDAP操作过程中出现的错误代码
查看>>
降低手机开发人员门槛的必须性
查看>>
强大的rsync
查看>>
运维工程师的职责和前景
查看>>
phpcms v9表单向导中怎么加入验证码
查看>>
微信公众帐号开发教程第4篇-----开发模式启用及接口配置Java
查看>>
列出文件目录结构
查看>>
使用 Clozure CL 在 Cocoa 窗口组件中实现显示中文字符串的简单方法
查看>>
UML关系(泛化,实现,依赖,关联(聚合,组合))
查看>>
关于Flutter初始化流程,我必须告诉你的是...
查看>>
VMWare
查看>>
Java 基础
查看>>
Spring的代理选择
查看>>
PHP搭建简易留言板
查看>>
Test
查看>>
我的友情链接
查看>>
Open***在linux上的完美实现
查看>>