首页 > 代码库 > [刘阳Java]_InternalResourceViewResolver视图解析器_第6讲

[刘阳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为优先级最高,所以优先进行处理视图)

 

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