SpringMVC学习(5)—— SpringMVC的视图(转发和重定向)

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

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

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

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

一. ThymeleafView

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

package com.mvc.controller;

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

@Controller
public class ViewController {
    @RequestMapping("/")   //必须要有该映射路径,这样启动Tomcat服务器时才能找到index.html页面
    public String index(){
        return "index";
    }

    @RequestMapping("/thymeleafView")
    public String testThymeleafView(){
        return "test";
    }
}
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <h1>index页面</h1>

    <a th:href="@{/thymeleafView}">测试ThymeleafView</a>
</body>
</html>

二. InternalResourceView (转发视图)

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

package com.mvc.controller;

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

@Controller
public class ViewController {
    @RequestMapping("/")   //必须要有该映射路径,这样启动Tomcat服务器时才能找到index.html页面
    public String index(){
        return "index";
    }

    @RequestMapping("/thymeleafView")
    public String testThymeleafView(){
        return "test";
    }

    @RequestMapping("/testForward")
    public String testForward(){
        return "forward:/thymeleafView";  //不可以用forward:+html来之间跳转到其他页面,可以写成"forward:/"来跳到index页面
    }
}
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <h1>index页面</h1>

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

此时,先创建InternalResourceView视图,再创建ThymeleafView视图,依然可以跳转到test.html页面。此时到test.html页面地址栏是第一次请请求的地址:即 /testForward

三. RedirectView (重定向视图)

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

package com.mvc.controller;

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

@Controller
public class ViewController {
    @RequestMapping("/")   //必须要有该映射路径,这样启动Tomcat服务器时才能找到index.html页面
    public String index(){
        return "index";
    }

    @RequestMapping("/thymeleafView")
    public String testThymeleafView(){
        return "test";
    }

    @RequestMapping("/testRedirect")
    public String testRedirect(){
        return "redirect:/thymeleafView";   //不可以用redirect:+html来之间跳转到其他页面,可以写成"redirect:/"来跳到index页面
    }
}
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <h1>index页面</h1>

    <a th:href="@{/testRedirect}">测试</a>
</body>
</html>

此时到test.html页面地址栏是第一次请请求的地址:即 /thymeleafView

四. view-controller (视图控制器)

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

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

    <mvc:view-controller path="/" view-name="index"></mvc:view-controller>
    <mvc:annotation-driven/>   <!--开启MVC的注解驱动 -->

在SpringMVC的核心配置文件中添加上面的内容可以替代以下内容:

package com.mvc.controller;

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

@Controller
public class ViewController {
    @RequestMapping("/")  
    public String index(){
        return "index";
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值