SpringMVC的视图

目录

1、ThymeleafView

2、转发视图

3、重定向视图

转发:

重定向

4、视图控制器view-controller


SpringMVC中的视图是View接口,视图的作用渲染数据,将模型Model中的数据展示给用户

SpringMVC视图的种类很多,默认有转发视图internalResourceView和重定向视图RedirectView

当工程引入jstl的依赖,转发视图会自动转换为JstlView

若使用的视图技术为Thymeleaf,在SpringMVC的配置文件中配置了Thymeleaf的视图解析器,由此视 图解析器解析之后所得到的是ThymeleafView

1、ThymeleafView

index.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
首页

<br>
<a th:href="@{/testRequestByServletAPI}">使用ServletAPI向request域对象共享数据</a>
<br>
<a th:href="@{/testModelAndView}">使用ModelAndView向request域对象共享数据</a>
<br>
<a th:href="@{/testModel}">使用Model向request域对象共享数据</a>

<br>
<a th:href="@{/testMap}">4、使用map向request域对象共享数据</a>

<br>
<a th:href="@{/testModelMap}">5、使用ModelMap向request域对象共享数据</a>


<br>
<a th:href="@{/testSession}">7、通过ServletAPI向向session域共享数据</a>

<br>
<a th:href="@{/testApplication}">8、向application域共享数据</a>

<h1>##########################################################</h1>

<a th:href="@{/test_view}">视图测试</a>





</body>
</html>

TestController.java

package controller;


import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class TestController {


    @RequestMapping("/")
    public String index() {
        return "index";
    }

    @RequestMapping("/test_view")
    public String tsetView() {
        return "test_view";
    }


}

ViewController.java
package controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class ViewController {



    @RequestMapping("/testThymeleafView")
    public String testThymeleafView(){


        return "success";
    }

}

test_view.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

<a th:href="@{/testThymeleafView}">测试testThymeleafView</a>


</body>
</html>

success.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
成功 success<br>
<p th:text="${testRequestScope}"></p>

<!--通过ServletAPI向向session域共享数据采用此方法-->
<p th:text="${session.testSessionScope}"></p>

<p th:text="${application.testApplicationScope}"></p>


</body>
</html>

当控制器方法中所设置的视图名称没有任何前缀时,此时的视图名称会被SpringMVC配置文件中所配置 的视图解析器解析,视图名称拼接视图前缀和视图后缀所得到的最终路径,会通过转发的方式实现跳转

2、转发视图

SpringMVC中默认的转发视图是InternalResourceView

SpringMVC中创建转发视图的情况:

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

例如"forward:/","forward:/employee"

ViewController.java

    @RequestMapping("/testForward")
    public String testForward(){

        return "forward:/testThymeleafView";
    }

test_view.html

<br>
<a th:href="@{/testForward}">测试testForward</a>

3、重定向视图

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

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

例如"redirect:/"到首页,"redirect:/employee"

转发:

一次请求,浏览器发送   地址不变    可以获取请求域中的对象   可以访问webinf下的资源

   不可以跨域

重定向

:两次请求,第一次访问servlet,第二次访问重定向的地址  地址改变,改变为重定向的地址       不可以获取请求域中的对象      不可以访问webinf下的资源   可以跨域

ViewController.java

    @RequestMapping("/testredirect")
    public String testredirect() {

        return "redirect:/";//到首页
    }

test_view.html

<br>
<a th:href="@{/testredirect}">测试重定向视图redirect</a>

4、视图控制器view-controller

控制器方法中,仅仅用来实现页面跳转,即只需要设置视图名称时,可以将处理器方法使用viewcontroller标签进行表示

为mvc添加命名空间在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 http://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="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>

    <!--
path:设置处理的请求地址
view-name:设置请求地址所对应的视图名称
-->


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

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


</beans>

同时注释掉

TestController.Java中的index

注: 当SpringMVC中设置任何一个view-controller时,其他控制器中的请求映射将全部失效,此时需 要在SpringMVC的核心配置文件中设置开启mvc注解驱动的标签:

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值