7. SpringMVC-视图

一、什么是SpringMVC的视图

  1. SpringMVC中的视图是View接口,视图的作用渲染数据,将模型Model中的数据展示给用户。
  2. SpringMVC视图的种类很多,默认有转发视图InternalResourceView和重定向视图RedirectView。
  3. 当工程引入jstl的依赖,转发视图会自动转换为JstlView。
  4. 若使用的视图技术为Thymeleaf,在SpringMVC的配置文件中配置了Thymeleaf的视图解析器,由此视图解析器解析之后所得到的是ThymeleafView。

二、ThymeleafView

1. SpringMVC.xml

<?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: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 https://www.springframework.org/schema/context/spring-context.xsd
                    http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <!--扫描组件-->
    <context:component-scan base-package="znb.mvc.controller"></context:component-scan>

    <!-- 配置Thymeleaf视图解析器 -->
    <bean id="viewResolver" class="org.thymeleaf.spring5.view.ThymeleafViewResolver">
        <property name="order" value="1"/>
        <property name="characterEncoding" value="UTF-8"/>
        <property name="templateEngine">
            <bean class="org.thymeleaf.spring5.SpringTemplateEngine">
                <property name="templateResolver">
                    <bean class="org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver">

                        <!-- 视图前缀 -->
                        <property name="prefix" value="/WEB-INF/templates/"/>

                        <!-- 视图后缀 -->
                        <property name="suffix" value=".html"/>
                        <property name="templateMode" value="HTML5"/>
                        <property name="characterEncoding" value="UTF-8" />
                    </bean>
                </property>
            </bean>
        </property>
    </bean>

    <mvc:view-controller path="/" view-name="index"></mvc:view-controller>
    <mvc:view-controller path="/test_rest" view-name="test_rest"></mvc:view-controller>

    <!--开启mvc的注解驱动-->
    <mvc:annotation-driven></mvc:annotation-driven>
    
</beans>

2. 转发实现跳转

  1. 在配置好Thymeleaf的视图解析器后,当控制器方法中所设置的视图名称没有任何前缀时,此时的视图名称会被SpringMVC配置文件中所配置的视图解析器解析,视图名称拼接视图前缀和视图后缀所得到的最终路径,会通过转发的方式实现跳转。
  2. 例如有如下方法:
	@RequestMapping("/testThymeleafView")
    public String testThymeleafView(){
        //转发-创建ThymeleafView
        return "success";
    }
  1. 该方法的请求路径为,视图前缀(/WEB-INF/templates/) + hello + 视图后缀(.html)
  2. 访问链接:http://localhost:8080/SpringMvc03/testThymeleafView
    在这里插入图片描述

三、转发视图

  1. SpringMVC中默认的转发视图是InternalResourceView
  2. SpringMVC中创建转发视图的情况:
    当控制器方法中所设置的视图名称以"forward:"为前缀时,创建InternalResourceView视图, 此时的视图名称不会被SpringMVC配置文件中所配置的视图解析器解析,而是会将前缀"forward:"去掉,剩余部分作为最终路径通过转发的方式实现跳转
  3. 例如有如下方法:
	@RequestMapping("/testForword")
    public String testForword(){
        //转发,会访问 /testThymeleafView进行转发
        return "forward:/testThymeleafView";
    }
  1. 访问链接:http://localhost:8080/SpringMvc03/testForword
    在这里插入图片描述

四、重定向视图

  1. SpringMVC中默认的重定向视图是RedirectView

  2. 当控制器方法中所设置的视图名称以"redirect:"为前缀时,创建RedirectView视图,此时的视图名称不会被SpringMVC配置文件中所配置的视图解析器解析,而是会将前缀"redirect:"去掉,剩余部分作为最终路径通过重定向的方式实现跳转

     重定向视图在解析时,会先将redirect:前缀去掉,然后会判断剩余部分是否以/开头,
     若是则会自动拼接上下文路径
    
  3. 例如有如下方法:

	@RequestMapping("/testRedirect")
    public String testRedirect(){
        //重定向-/testThymeleafView
        return "redirect:/testThymeleafView";
    }
  1. 访问链接:http://localhost:8080/SpringMvc03/testRedirect
    在这里插入图片描述

1. 转发和重定向的区分

  1. 转发是一次请求,第一次是浏览器请求,第二次是服务器内部的请求;重定向是两次请求第一次是浏览器访问servlet,第二次是浏览器访问重定向的地址。
  2. 转发第二次是服务器内部的跳转,地址栏中的地址保持不变还是第一次发生请求的地址;重定向是浏览器发生了两次请求,最终地址栏中的地址是第二次请求的地址。
  3. 转发可以获取请求域中的数据,转发是一次请求用到的request对象是同一个;重定向不可以,两次请求对应两个request对象。

五、视图控制器view-controller

  1. 当控制器方法中,仅仅用来实现页面跳转,即只需要设置视图名称时,可以将处理器方法使用view-controller标签进行表示
<!--
	path:设置处理的请求地址
	view-name:设置请求地址所对应的视图名称
-->
<mvc:view-controller path="/" view-name="index"></mvc:view-controller>
  1. 测试
/*@RequestMapping("/")
    public String index(){
        return "index";
    }*/

在这里插入图片描述

  1. 注意事项

     当SpringMVC中设置任何一个view-controller时,其他控制器中的请求映射将全部失效,
     此时需要在SpringMVC的核心配置文件中设置开启mvc注解驱动的标签:
    
 	<!--开启mvc的注解驱动-->
    <mvc:annotation-driven></mvc:annotation-driven>
  1. 完整的配置文件
<?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: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 https://www.springframework.org/schema/context/spring-context.xsd
                    http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <!--扫描组件-->
    <context:component-scan base-package="znb.mvc.controller"></context:component-scan>

    <!-- 配置Thymeleaf视图解析器 -->
    <bean id="viewResolver" class="org.thymeleaf.spring5.view.ThymeleafViewResolver">
        <property name="order" value="1"/>
        <property name="characterEncoding" value="UTF-8"/>
        <property name="templateEngine">
            <bean class="org.thymeleaf.spring5.SpringTemplateEngine">
                <property name="templateResolver">
                    <bean class="org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver">

                        <!-- 视图前缀 -->
                        <property name="prefix" value="/WEB-INF/templates/"/>

                        <!-- 视图后缀 -->
                        <property name="suffix" value=".html"/>
                        <property name="templateMode" value="HTML5"/>
                        <property name="characterEncoding" value="UTF-8" />
                    </bean>
                </property>
            </bean>
        </property>
    </bean>

    <mvc:view-controller path="/" view-name="index"></mvc:view-controller>
    <mvc:view-controller path="/test_rest" view-name="test_rest"></mvc:view-controller>

    <!--开启mvc的注解驱动-->
    <mvc:annotation-driven></mvc:annotation-driven>
    
</beans>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

CAFEBABE 34

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值