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. 猿人学内部js练习平台习题记录

    猿人学内部js练习平台习题记录 根据课程更新 当前先完成第7题和第10题 第7题 骚操作 请求规律检测1 - post 1)通过fiddler抓包,看看请求头和请求体有什么骚操作的地方,如果没有反爬就 ...

  2. 2023-04-01:当Go语言遇见FFmpeg视频解码器,使用Go语言改写decode_video.c文件,提升视频解码效率与开发体验。

    2023-04-01:当Go语言遇见FFmpeg视频解码器,使用Go语言改写decode_video.c文件,提升视频解码效率与开发体验. 答案2023-04-01: 步骤如下: 1.导入必要的依赖库 ...

  3. 2022-01-22:力扣411,最短独占单词缩写。 给一个字符串数组strs和一个目标字符串target。target的简写不能跟strs打架。 strs是[“abcdefg“,“ccc“],tar

    2022-01-22:力扣411,最短独占单词缩写. 给一个字符串数组strs和一个目标字符串target.target的简写不能跟strs打架. strs是["abcdefg", ...

  4. MMCM and PLL Dynamic Reconfiguration

    Reconfiguration is performed through the DRP. The DRP provides access to the configuration bits that ...

  5. 去掉DosBox烦人的Status Windows

    首先我们上成品动态图 很干净,很清爽有没有! 步骤 1.找到并选中DosBox快捷方式,鼠标右键点击选择属性 2.修改目标后面的参数,默认是 -userconf ,再添加一个 -noconsole 就 ...

  6. k8s calico网络

  7. 基于Quartz的可视化UI操作组件GZY.Quartz.MUI更新说明(附:在ABP中集成GZY.Quartz.MUI可视化操作组件)

    前言 时隔2年.(PS:其实陆陆续续在优化,不过没发博客).. .本组件又迎来了新的更新... 很久没更新博客了.生了娃,换了工作单位,太忙了..实在抱歉 NET Core 基于Quartz的UI可视 ...

  8. 一天吃透Spring面试八股文

    内容摘自我的学习网站:topjavaer.cn Spring是一个轻量级的开源开发框架,主要用于管理 Java 应用程序中的组件和对象,并提供各种服务,如事务管理.安全控制.面向切面编程和远程访问等. ...

  9. 龙芯下如何进行.net core程序开发部署

    龙芯LoongArch64已经发布了对.NETCore的支持,相关工具链也已完成,目前支持.NETCore3.1..NET6..NET7版本.本文以.NETCore3.1在loongnix-serve ...

  10. Java 实战介绍 Cookie 和 Session 的区别

    HTTP 是一种不保存状态的协议,即无状态协议,HTTP 协议不会保存请求和响应之间的通信状态,协议对于发送过的请求和响应都不会做持久化处理. 无状态协议减少了对服务压力,如果一个服务器需要处理百万级 ...