[刘阳Java]_InternalResourceViewResolver视图解析器_第6讲

SpringMVC在处理器方法中通常返回的是逻辑视图,如何定位到真正的页面,就需要通过视图解析器

InternalResourceViewResolver是SpringMVC中比较常用视图解析器。

网上有一篇文章写得不错,我们也推荐大家去看看,当然要谢谢这篇博客提供的内容,转发地址:http://www.cnblogs.com/liruiloveparents/p/5054605.html

1. InternalResourceViewResolver的配置文件代码如下

<?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/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">
    
    <mvc:annotation-driven></mvc:annotation-driven>
    <context:component-scan base-package="com.gxa.spmvc.controller"></context:component-scan>

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
        p:prefix="/" p:suffix=".jsp"></bean>

</beans>

如果当处理器返回“index”时,InternalResourceViewResolver解析器会自动添加前缀和后缀,则真实的视图路径:/index.jsp 

2. SpringMVC中可以配置多个视图解析器,我们可以通过在配置文件添加order属性来设置他们的优先级 

<?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/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">
    
    <mvc:annotation-driven></mvc:annotation-driven>
    <context:component-scan base-package="com.gxa.spmvc.controller"></context:component-scan>

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
        p:prefix="/" p:suffix=".jsp" p:order="10"></bean>

</beans>

上面配置文件中的p:order="10",就是对当前的InternalResourceViewResolver设置优先级。order取值越小优先级越大(即:0为优先级最高,所以优先进行处理视图)

3. 为什么需要设置视图解析器优先级

当处理器返回逻辑视图时,要经过视图解析器链,前面的解析器能处理的,就不会继续往下传播。如果不能处理就要沿着解析器链继续寻找,直到找到合适的视图解析器(概括为:能解析的,不继续往下找,不能解析的,要继续往下找解析器)

假如有几个视图解析器:视图解析器1、视图解析器2、视图解析器3。则设置了视图解析器后,3个视图解析器的优先级可以通过下图来理解

上图的视图解析器链的原理可以总结如下:

当经过视图解析器1时,如果能解析就解析而且不会再继续往下。如果不能执行就返回null,这样下面的解析器才能处理。但是对于解析器InternalResourceViewResolver来说,不管能不能解析它都不会返回null,也就是说它拦截了所有的逻辑视图,让后续的解析器得不到执行,所以InternalResourceViewResolver必须放在最后

记住一点:不能解析就返回null,这样后续解析器才能解析。

原文地址:https://www.cnblogs.com/liuyangjava/p/6761245.html