最近在调试一个Spring Boot向Thymeleaf模板传参数的例子,但踩了很多坑,这里就把详细过程记录下来,以供大家参考。

先说下,这里遇到哪些坑呢?

1 我用的是IDEA社区版,这不支持JSP,我本来向传到JSP的,由于不支持,所以只能传到HTML。

2 HMML里,必须要引入Thymeleaf模板,否则无法从ModelAndView里接收到参数。

好,然后给出我搭建项目的步骤,先创建一个名为ModelAndViewDemo的Maven项目里,而在下表里,给出了重要文件的说明。

重要文件

说明

pom.xml

引入了该项目所用到的依赖包,尤其地,引入了Thymeleaf的依赖包

SpringBootApp.java

启动类

Controller.java

控制器类,在其中通过ModelAndView对象和前端Thymeleaf交互

Application.properties

配置文件,其中包含了Thymeleaf的相关配置

hello.html

包含Thymeleaf模板的前端页面文件,请注意它是在resources目录的templates目录里,这个目录结构需要和配置文件里的配置保持一致

第一步,在pom.xml里,包含本项目的依赖包,关键代码如下所示。其中,通过第6行到第9行的代码,引入了thymeleaf模板的依赖包。

1  <dependencies>
2 <dependency>
3 <groupId>org.springframework.boot</groupId>
4 <artifactId>spring-boot-starter-web</artifactId>
5 </dependency>
6 <dependency>
7 <groupId>org.springframework.boot</groupId>
8 <artifactId>spring-boot-starter-thymeleaf</artifactId>
9 </dependency>
10 </dependencies>

第二步,编写启动类。

1	package prj;
2 import org.springframework.boot.SpringApplication;
3 import org.springframework.boot.autoconfigure.SpringBootApplication;
4 @SpringBootApplication
5 public class SpringBootApp {
6 public static void main(String[] args) {
7 SpringApplication.run(SpringBootApp.class, args);
8 }
9 }

第三步,编写控制器类,代码如下所示。

1	package prj.controller;
2 import org.springframework.web.bind.annotation.RequestMapping;
3 import org.springframework.web.bind.annotation.RestController;
4 import org.springframework.web.servlet.ModelAndView;
5 @RestController
6 public class Controller {
7 @RequestMapping("/welcome")
8 public ModelAndView welcome() {
9 ModelAndView modelAndView = new ModelAndView("hello");
10 modelAndView.getModel().put("name", "Tom");
11 return modelAndView;
12 }
13 }

在第8行的welcome方法里,先是在第9行创建了ModelAndView类型的对象,并通过构造函数,指定该对象里的视图为“hello”,随后通过第10行的代码,在该对象的Model里,以键值对的形式,添加了键是name值是Tom的数据。结合起来看,welcome方法将向hello视图返回一个键值对数据。

第四步,在application.properties里,编写thymeleaf模板的相关参数,具体代码如下。

1	#启用thymeleaf视图
2 spring.thymeleaf.enabled=true
3 #设置Content-Type值
4 spring.thymeleaf.content-type=text/html
5 ## 检查模板是否存在,然后再呈现
6 spring.thymeleaf.check-template-location=true
7 # 不启用缓存
8 spring.thymeleaf.cache=false
9 # 构建前缀
10 spring.thymeleaf.prefix=classpath:/templates/
11 # 构建后缀
12 spring.thymeleaf.suffix=.html

在对应的参数项前都有注释,大家可以自行阅读,不过这里有如下两大注意点。

  1. 为了要使用thymeleaf视图,必须要配置如第2行所示的参数。
  2. 第10行和第12行定义的前缀和后缀,会和ModelAndView对象里的视图整合起来使用。比如在Controller.java里,ModelAndView里返回的视图是hello,所以会对应地加上前后缀,加号以后的值是classpath:/templates/hello.html,这样能指定最终跳转到的视图文件位置。

第五步,需要编写包含thymeleaf模板的hello.html页面,代码如下所示。

1	<!DOCTYPE html>
2 <html lang="en" xmlns:th="http://www.thymeleaf.org">
3 <head>
4 <meta charset="UTF-8">
5 <title>welcome</title>
6 </head>
7 <body>
8 Welcome:<span th:text="${name}"></span>
9 </body>
10 </html>

其中在第2行,指定了第8行要用到的th标签的命名空间,这是来自于thymeleaf模板。

而在第8行里,通过th:text="${name}"的形式,指定了存放${name}参数的占位符,而具体的name参数值,会来自于后端的返回。从这个页面里,大家能看到thymeleaf模板如下的样式特征。

  1. 本范例中,thymeleaf模板是嵌入在HTML5代码里的,在使用时,需要如第2行所示,引入要用到该模板属性元素的命名空间。
  2. 在诸如html5的前端页面里,可以像第8行那样,通过thymeleaf的语法,设置参数的占位符,这样当后端通过ModelAndView等形式传递来参数时,就能在占位符所在的位置,动态展示。

完成开发后启动该项目,并如控制器里welcome方法之前的@RequestMapping注解所示,在浏览器里输入http://localhost:8080/welcome,就能看到输出“Welcome:Tom“的字样。而从发起请求到展示数据,主要经历了如下的流程。

  1. 根据@RequestMapping注解所定义,http://localhost:8080/welcome请求被welcome方法所处理。
  2. 在welcome方法里设置了返回视图为hello,并设置了name参数的值是Tom。
  3. 根据application.properties里的配置,会根据配置好的前后缀,确定待返回的视图页面,这里是resources(由于该目录是在本项目的classpath里)目录下的templates目录里的hello.html。
  4. 最终会展示hello.html,并在其中thymeleaf模板所定义,在name参数占位符所在的位置展示“Tom”字样。由此展示大家最终看到的结果。

spring boot用ModelAndView向Thymeleaf模板传参数的更多相关文章

  1. Spring Boot学习记录(二)--thymeleaf模板 - CSDN博客

    ==他的博客应该不错,没有细看 Spring Boot学习记录(二)--thymeleaf模板 - CSDN博客 http://blog.csdn.net/u012706811/article/det ...

  2. Spring Boot 2.0 整合Thymeleaf 模板引擎

    本节将和大家一起实战Spring Boot 2.0 和thymeleaf 模板引擎 1. 创建项目 2. 使用Spring Initlizr 快速创建Spring Boot 应用程序 3. 填写项目配 ...

  3. Spring Boot学习记录(二)–thymeleaf模板

    自从来公司后都没用过jsp当界面渲染了,因为前后端分离不是很好,反而模板引擎用的比较多,thymeleaf最大的优势后缀为html,就是只需要浏览器就可以展现页面了,还有就是thymeleaf可以很好 ...

  4. Spring Boot Web开发中Thymeleaf模板引擎的使用

    这里使用的是idea 1.新建Spring Boot项目 File-->New-->Project...,然后选择左边的Spring Initializr-->Next,可根据自己的 ...

  5. Spring Boot Web开发与thymeleaf模板引擎

    简介: 使用Springboot应用,选中需要的模块, Spring已经默认将场景配置好了,只需在配置文件中少量配置就可以运行起来 自己编写业务代码 自动配置原理 这个场景Springboot帮我们配 ...

  6. 9、Spring Boot 2.x 集成 Thymeleaf

    1.9 Spring Boot 2.x 集成 Thymeleaf 完整源码: Spring-Boot-Demos 1.9.1 在pom中引入依赖 <dependency> <grou ...

  7. Spring Boot 2.x教程-Thymeleaf 原理是什么

    layout: post title: Spring Boot 2.x教程-Thymeleaf 原理是什么 categories: SpringBoot description: Spring Boo ...

  8. 从零开始的Spring Boot(3、Spring Boot静态资源和文件上传)

    Spring Boot静态资源和文件上传 写在前面 从零开始的Spring Boot(2.在Spring Boot中整合Servlet.Filter.Listener的方式) https://www. ...

  9. spring boot下MultipartHttpServletRequest如何提高上传文件大小的默认值

    前言: 上传下载功能算是一个非常常见的功能,如果使用MultipartHttpServletRequest来做上传功能. 不配置上传大小的话,默认是2M.在有些场景,这个肯定不能满足条件. 上传代码: ...

随机推荐

  1. (七)整合 Redis集群 ,实现消息队列场景

    整合 Redis集群 ,实现消息队列场景 1.Redis集群简介 1.1 RedisCluster概念 2.SpringBoot整合Redis集群 2.1 核心依赖 2.2 核心配置 2.3 参数渲染 ...

  2. Dbeaver 连接 phoenix

    Dbeaver 连接 phoenix 1.新建连接 2.选择连接类型Phoenix 3.设置驱动 4.准备驱动包 5.添加驱动 6.添加 Zookeeper Base Path 7.找到驱动类 8.配 ...

  3. 国产App为什么如此“臃肿”?!

    引言 App是Application的简称,正是因为有了丰富多彩的各类App,人们就可以通过它们来最大限度地发挥手中设备的功能.本文主要讨论手机上的App,因为手机的硬件和软件与十余年前相比早已有了巨 ...

  4. 一周精彩内容分享(第 1 期):"世纪逼空大战"

    这里记录过去一周,我看到的值得分享的东西. 一方面是整理记录一下自己一周的学习,另一方面也是期待自己有更多的输出,有更多的价值. 周刊开源(Github:wmyskxz/weekly),欢迎提交 is ...

  5. A - 规律题

    我们看到过很多直线分割平面的题目,今天的这个题目稍微有些变化,我们要求的是n条折线分割平面的最大数目.比如,一条折线可以将平面分成两部分,两条折线最多可以将平面分成7部分,具体如下所示. Input输 ...

  6. P3803 [模板] 多项式乘法 (FFT)

    Rt 注意len要为2的幂 #include <bits/stdc++.h> using namespace std; const double PI = acos(-1.0); inli ...

  7. Java的awt包的使用实例和Java的一些提示框

    一.awt的一些组件 Label l1=new Label("姓名:"); //标签 Label l2=new Label("密码:"); TextField ...

  8. java中static修改成员变量和函数和其他使用

    一.通过static修饰的成员变量初始化只会初始化一次 //静态变量初始化只会初始化一次 public class zuishuai { public static void main(String[ ...

  9. Codeforces Round #529 (Div. 3) C. Powers Of Two (二进制)

    题意:给你一个数\(n\),问是否能有\(k\)个\(2\)次方的数构成,若满足,输出一种合法的情况. 题解:从高到低枚举二进制的每一位,求出\(n\)的二进制的\(1\)的位置放进优先队列中,因为\ ...

  10. codeforces 7B

    B. Memory Manager time limit per test 1 second memory limit per test 64 megabytes input standard inp ...