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

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

当工程引入jstl的依赖,转发视图会自动转换为JstlView若使用的视图技术为Thymeleaf,在SpringMVC的配置文件中配置了Thymeleaf的视图解析器,由此视图解析器解析之后所得到的是ThymeleafView

一、ThymeleafView

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

html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>首页</h1>
<a th:href="@{/testThymeleafView}">测试ThymeleafView</a>
</body>
</html>

java

package com.mcode.api.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping; /**
* ClassName: ViewController
* Package: com.mcode.api.controller
* Description:
*
* @Author: robin
* @Create: 2023/8/7 - 9:06 PM
* @Version: v1.0
*/
@Controller
public class ViewController {
@RequestMapping("/")
public String index(){
return "index";
}
@RequestMapping("/testThymeleafView")
public String testThymeleafView(){
return "success";
}
}
DispatcherServlet.render()

AbstractCachingViewResolver.resolveViewName()

ThymeleafViewResolver.createView()

二、转发视图

SpringMVC中默认的转发视图是InternalResourceView

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

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

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

html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>首页</h1>
<a th:href="@{/testThymeleafView}">测试ThymeleafView</a>
<a th:href="@{/testForward}">测试InternalResourceView</a>
</body>
</html>

java

package com.mcode.api.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping; /**
* ClassName: ViewController
* Package: com.mcode.api.controller
* Description:
*
* @Author: robin
* @Create: 2023/8/7 - 9:06 PM
* @Version: v1.0
*/
@Controller
public class ViewController {
@RequestMapping("/")
public String index(){
return "index";
}
@RequestMapping("/testThymeleafView")
public String testThymeleafView(){
return "success";
} @RequestMapping("/testForward")
public String testForward(){
return "forward:/testThymeleafView";
}
}

三、重定向视图

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

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

html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>首页</h1>
<a th:href="@{/testThymeleafView}">测试ThymeleafView</a>
<a th:href="@{/testForward}">测试InternalResourceView</a>
<a th:href="@{/testRedirect}">测试Redirect</a>
</body>
</html>

java

package com.mcode.api.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping; /**
* ClassName: ViewController
* Package: com.mcode.api.controller
* Description:
*
* @Author: robin
* @Create: 2023/8/7 - 9:06 PM
* @Version: v1.0
*/
@Controller
public class ViewController {
@RequestMapping("/")
public String index(){
return "index";
}
@RequestMapping("/testThymeleafView")
public String testThymeleafView(){
return "success";
} @RequestMapping("/testForward")
public String testForward(){
return "forward:/testThymeleafView";
} @RequestMapping("/testRedirect")
public String testRedirect(){
return "redirect:/testThymeleafView";
}
}

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

四、视图控制器view-controller

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

<?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="com.mcode.api.controller"/> <!-- 配置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注解驱动 -->
<mvc:annotation-driven/>
</beans>
@Controller
public class ViewController {
// @RequestMapping("/")
// public String index(){
// return "index";
// }
}

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

五、配置jsp解析

<?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="com.mcode.api.controller"/> <!-- 配置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> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- jsp前缀 -->
<property name="prefix" value="/jsp/"/>
<!-- jsp后缀 -->
<property name="suffix" value=".jsp"/>
<!-- 配置优先等级,越小优先级越高 -->
<property name="order" value="0"/>
</bean> <!-- path:设置处理的请求地址 view-name:设置请求地址所对应的视图名称 -->
<mvc:view-controller path="/" view-name="index"/> <!-- 开启mvc注解驱动 -->
<mvc:annotation-driven/>
</beans>

index.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>首页</h1>
<a href="${pageContext.request.contextPath}/success">success.jsp</a>
</body>
</html>

web.xml

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">

注:${pageContext.request.contextPath } 无效大多情况下是由于web.xml版本太低导致的,现在普遍都再用web4.0版本,web-app的版本默认是2.3,而要正确解析上面的代码获得当前路径,我们需要把web-app的版本手动改成2.5及以上版本。

控制器

package com.mcode.api.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping; /**
* ClassName: JSPController
* Package: com.mcode.api.controller
* Description:
*
* @Author: robin
* @Create: 2023/8/7 - 10:16 PM
* @Version: v1.0
*/
@Controller
public class JSPController {
@RequestMapping("/success")
public String success(){
return "success";
}
}

spring-mvc 系列:视图(ThymeleafView、InternalResourceView、RedirectView)的更多相关文章

  1. 【Spring MVC系列】--(4)返回JSON

    [Spring MVC系列]--(4)返回JSON 摘要:本文主要介绍如何在控制器中将数据生成JSON格式并返回 1.导入包 (1)spring mvc 3.0不需要任何其他配置,添加一个jackso ...

  2. Spring mvc系列一之 Spring mvc简单配置

    Spring mvc系列一之 Spring mvc简单配置-引用 Spring MVC做为SpringFrameWork的后续产品,Spring 框架提供了构建 Web 应用程序的全功能 MVC 模块 ...

  3. Spring MVC之视图解析器

    Spring MVC提供的视图解析器使用ViewResolver进行视图解析,实现浏览器中渲染模型.ViewResolver能够解析JSP.Velocity模板.FreeMarker模板和XSLT等多 ...

  4. Spring MVC 的视图转发

    Spring MVC 默认采用的是转发来定位视图,如果要使用重定向,可以如下操作 1.使用RedirectView public ModelAndView login(){ RedirectView ...

  5. Spring MVC的视图解析器

    一.视图解析器简介 在Spring MVC中,当Controller将请求处理结果放入到ModelAndView中以后,DispatcherServlet会根据ModelAndView选择合适的视图进 ...

  6. Spring MVC Xml视图解析器

    XmlViewResolver用于在xml文件中定义的视图bean来解析视图名称.以下示例演示如何在Spring Web MVC框架使用XmlViewResolver. XmlViewResolver ...

  7. 我看Spring MVC系列(一)

    1.Spring MVC是什么: Spring MVC:Spring框架提供了构建Web应用程序的全功能MVC模块. 2.Spring helloWorld应用(基于Spring 4.2) 1.添加S ...

  8. Spring MVC系列之模型绑定(SpringBoot)(七)

    前言 上一节我们在SpringBoot中启用了Spring MVC最终输出了HelloWorld,本节我们来讲讲Spring MVC中的模型绑定,这个名称来源于.NET或.NET Core,不知是否恰 ...

  9. Spring MVC系列-(1) Spring概述

    1. Spring概述 本章主要介绍Spring中的体系结构和常见概念,比如bean.控制反转(Inverse of Control,IoC)等. 1.1 体系结构 Spring 框架提供约 20 个 ...

  10. Spring MVC系列之JDBC Demo(SpringBoot)(七)

    前言 前面我们了解了Spring MVC的基本使用,其实和.NET或.NET Core MVC无异,只是语法不同而已罢了,本节我们将和和数据库打交道,从最基础的JDBC讲解起,文中若有错误之处,还望指 ...

随机推荐

  1. #PowerBi 1分钟学会,powerbi中行列值拼接(COMBINEVALUES与CONCATENATEX)

    在日常的工作中,我们往往需要对表格数据的拼接,用来生成一些复合数据列,如下图类似场景. 其实,在powerbi中,我们同样也可以对表格文本进行拼接.今天我们就介绍两个DAX函数,COMBINEVALU ...

  2. Event Tables for Efficient Experience Replay

    Abstract 事件表分层抽样(SSET),它将ER缓冲区划分为事件表,每个事件表捕获最优行为的重要子序列. 我们证明了一种优于传统单片缓冲方法的理论优势,并将SSET与现有的优先采样策略相结合,以 ...

  3. 一个.Net Core开发的开源动态壁纸软件

    推荐一个Github上Start超过10.8K的超火.好用.强大的.内置很多优美的动态壁纸软件. 项目简介 这是基于.Net Core+WPF开发的.开源的动态壁纸软件,壁纸设置支持任何文件形式,包括 ...

  4. List的拆分的几种方式

    开发中我们可能会遇到一个大的集合,然后我们需要对集合进行拆分,然后再对拆分的集合进行相关的操作.当然我们可以自己写一个拆分的方法,我自己写过用了不少代码,但是感觉还不是很好,最近看了不少工具才发现很多 ...

  5. 2023-06-11:redis中,如何在100个亿URL中快速判断某URL是否存在?

    2023-06-11:redis中,如何在100个亿URL中快速判断某URL是否存在? 答案2023-06-11: 传统数据结构的不足 当然有人会想,我直接将网页URL存入数据库进行查找不就好了,或者 ...

  6. 解决NAT模式下SSH连接虚拟机

    解决NAT模式下SSH连接虚拟机 简介: 用到的有软件:VirtualBox6.1,RetHat7.4 , SmartTTY 来由: 刚开始使用桥接模式(Bridged)网络连接,但是虚拟机没有网络. ...

  7. 尚医通day11-Java中阿里云对象存储OSS

    页面预览 用户认证 用户登录成功后都要进行身份认证,认证通过后才可以预约挂号. 认证过程:用户填写基本信息(姓名.证件类型.证件号码和证件照片),提交平台审核 用户认证相关接口: (1)上传证件图片 ...

  8. 【QCustomPlot】使用方法(源码方式)

    说明 使用 QCustomPlot 绘图库辅助开发时整理的学习笔记.同系列文章目录可见 <绘图库 QCustomPlot 学习笔记>目录.本篇介绍 QCustomPlot 的一种使用方法, ...

  9. 第四章 VIVIM编辑器

    1. 是什么 ‍ VI 是 Unix 操作系统和类 Unix 操作系统中最通用的文本编辑器. ‍ VIM 编辑器是从 VI 发展出来的一个性能更强大的文本编辑器. ‍ 2. 一般模式 ‍ 以 vi/v ...

  10. FPGA加速技术在游戏和娱乐系统中的应用:实现高效的游戏和娱乐系统

    目录 1. 引言 2. 技术原理及概念 3. 实现步骤与流程 4. 应用示例与代码实现讲解 <35. FPGA加速技术在游戏和娱乐系统中的应用:实现高效的游戏和娱乐系统>这篇文章是一篇针对 ...