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. dede网站flash中图片不能正常显示解决办法

    专业微信开发 网站制作  就在西安格创网络  联系电话:18009249661  原因:因为服务器或虚拟主机用了CDN加速,导致图片是调用远程缓存,只能显示FLASH控件,但图片不能正常显示,常见于西 ...

  2. 2023-04-05:做甜点需要购买配料,目前共有n种基料和m种配料可供选购。 制作甜点需要遵循以下几条规则: 必须选择1种基料;可以添加0种、1种或多种配料,每种类型的配料最多添加2份, 给定长度为

    2023-04-05:做甜点需要购买配料,目前共有n种基料和m种配料可供选购. 制作甜点需要遵循以下几条规则: 必须选择1种基料:可以添加0种.1种或多种配料,每种类型的配料最多添加2份, 给定长度为 ...

  3. 2020-12-15:mysql的回滚机制是怎么实现的?

    福哥答案2020-12-15:[答案来自此链接:](https://www.cnblogs.com/ld-swust/p/5607983.html)在 MySQL 中,恢复机制是通过回滚日志(undo ...

  4. Grafana系列-统一展示-9-Jaeger数据源

    系列文章 Grafana 系列文章 配置 Jaeger data source Grafana内置了对Jaeger的支持,它提供了开源的端到端分布式跟踪.本文解释了针对Jaeger数据源的配置和查询. ...

  5. vue全家桶进阶之路34:Vue3 路由基本配置

    在Vue3中,路由的基本配置是通过使用Vue Router库来实现的.以下是Vue3中路由的基本配置步骤: 安装Vue Router 使用npm或yarn在项目中安装Vue Router: npm i ...

  6. 深度学习基础入门篇[8]::计算机视觉与卷积神经网络、卷积模型CNN综述、池化讲解、CNN参数计算

    深度学习基础入门篇[8]::计算机视觉与卷积神经网络.卷积模型CNN综述.池化讲解.CNN参数计算 1.计算机视觉与卷积神经网络 1.1计算机视觉综述 计算机视觉作为一门让机器学会如何去"看 ...

  7. odoo开发教程十五:仪表板

    仪表盘可以通过外部ID引用其他视图文件的内容,整合到一个界面进行显示. 一:建立仪表盘视图文件 views/session_board.xml: 通过外部id引入要展示的视图文件--定义仪表板form ...

  8. 发布自己的项目到Maven中央仓库中

    注册账号和生成GPG生成密钥教程 主要看注册账号和生成GPG密匙部分就行了,出现问题可以先在这两个地方找 gpg加密发布jar包到maven中央仓库详细过程以及踩的坑_佛系猿秦大昊的博客-CSDN博客 ...

  9. 沉思篇-剖析Jetpack的ViewModel

    ViewModel做为架构组件的三元老之一,是实现MVVM的有力武器. ViewModel的设计目标 ViewModel的基本功能就是管理UI的数据.其实,从职责上来说,这又是对Activity和Fr ...

  10. auto main()-> int的含义是什么?

    42 https://stackoverflow.com/questions/21085446/what-is-the-meaning-of-auto-main-int/21085530   C++1 ...