spring boot用ModelAndView向Thymeleaf模板传参数
最近在调试一个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
在对应的参数项前都有注释,大家可以自行阅读,不过这里有如下两大注意点。
- 为了要使用thymeleaf视图,必须要配置如第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模板如下的样式特征。
- 本范例中,thymeleaf模板是嵌入在HTML5代码里的,在使用时,需要如第2行所示,引入要用到该模板属性元素的命名空间。
- 在诸如html5的前端页面里,可以像第8行那样,通过thymeleaf的语法,设置参数的占位符,这样当后端通过ModelAndView等形式传递来参数时,就能在占位符所在的位置,动态展示。
完成开发后启动该项目,并如控制器里welcome方法之前的@RequestMapping注解所示,在浏览器里输入http://localhost:8080/welcome,就能看到输出“Welcome:Tom“的字样。而从发起请求到展示数据,主要经历了如下的流程。
- 根据@RequestMapping注解所定义,http://localhost:8080/welcome请求被welcome方法所处理。
- 在welcome方法里设置了返回视图为hello,并设置了name参数的值是Tom。
- 根据application.properties里的配置,会根据配置好的前后缀,确定待返回的视图页面,这里是resources(由于该目录是在本项目的classpath里)目录下的templates目录里的hello.html。
- 最终会展示hello.html,并在其中thymeleaf模板所定义,在name参数占位符所在的位置展示“Tom”字样。由此展示大家最终看到的结果。
spring boot用ModelAndView向Thymeleaf模板传参数的更多相关文章
- Spring Boot学习记录(二)--thymeleaf模板 - CSDN博客
==他的博客应该不错,没有细看 Spring Boot学习记录(二)--thymeleaf模板 - CSDN博客 http://blog.csdn.net/u012706811/article/det ...
- Spring Boot 2.0 整合Thymeleaf 模板引擎
本节将和大家一起实战Spring Boot 2.0 和thymeleaf 模板引擎 1. 创建项目 2. 使用Spring Initlizr 快速创建Spring Boot 应用程序 3. 填写项目配 ...
- Spring Boot学习记录(二)–thymeleaf模板
自从来公司后都没用过jsp当界面渲染了,因为前后端分离不是很好,反而模板引擎用的比较多,thymeleaf最大的优势后缀为html,就是只需要浏览器就可以展现页面了,还有就是thymeleaf可以很好 ...
- Spring Boot Web开发中Thymeleaf模板引擎的使用
这里使用的是idea 1.新建Spring Boot项目 File-->New-->Project...,然后选择左边的Spring Initializr-->Next,可根据自己的 ...
- Spring Boot Web开发与thymeleaf模板引擎
简介: 使用Springboot应用,选中需要的模块, Spring已经默认将场景配置好了,只需在配置文件中少量配置就可以运行起来 自己编写业务代码 自动配置原理 这个场景Springboot帮我们配 ...
- 9、Spring Boot 2.x 集成 Thymeleaf
1.9 Spring Boot 2.x 集成 Thymeleaf 完整源码: Spring-Boot-Demos 1.9.1 在pom中引入依赖 <dependency> <grou ...
- Spring Boot 2.x教程-Thymeleaf 原理是什么
layout: post title: Spring Boot 2.x教程-Thymeleaf 原理是什么 categories: SpringBoot description: Spring Boo ...
- 从零开始的Spring Boot(3、Spring Boot静态资源和文件上传)
Spring Boot静态资源和文件上传 写在前面 从零开始的Spring Boot(2.在Spring Boot中整合Servlet.Filter.Listener的方式) https://www. ...
- spring boot下MultipartHttpServletRequest如何提高上传文件大小的默认值
前言: 上传下载功能算是一个非常常见的功能,如果使用MultipartHttpServletRequest来做上传功能. 不配置上传大小的话,默认是2M.在有些场景,这个肯定不能满足条件. 上传代码: ...
随机推荐
- (四)Springboot以jar包方式启动、关闭、重启脚本
Springboot以jar包方式启动.关闭.重启脚本 1.启动 2.关闭 3.重启 4.脚本授权 SpringBoot: nohup java -jar zlkj-data-server-1.0-S ...
- Spring Boot 两种多数据源配置:JdbcTemplate、Spring-data-jpa
多数据源配置 JdbcTemplate支持 Spring-data-jpa支持 多数据源配置 创建一个Spring配置类,定义两个DataSource用来读取application.propertie ...
- Spring Boot配置,读取配置文件
Spring Boot配置,读取配置文件 一.配置Spring Boot 1.1 服务器配置 1.2 使用其他Web服务器 1.3 配置启动信息 1.4 配置浏览器显示ico 1.5 Yaml语法 1 ...
- java切割~~百万 十万 万 千 百 十 个 角 分
/** * @param value * @return */ @SuppressWarnings("unused") public static void convertLoan ...
- 1.VLAN
1.定位:VLAN,即虚拟局域网(Virtual Local Area Network),一种将局域网设备从逻辑上划分成一个个网段,从而实现虚拟工作组的新兴数据交换技术.VLAN是将一个物理的LAN在 ...
- DEDECMS:修改网站底部出现的POWER BY DEDECMS
在include/dedesql.classs.php文件中找到第588行: $arrs1 = array(0x63,0x66,0x67,0x5f,0x70,0x6f,0x77,0x65,0x72,0 ...
- Linux 防火墙相关操作
目录 1.查看防火墙状态 2.部署防火墙 3.常用操作 4.其他操作 1.查看防火墙状态 systemctl status firewalld 绿字部分 Active:active(running) ...
- AYIT-2020 609暑假集训第一周周赛题 A - A计划
可怜的公主在一次次被魔王掳走一次次被骑士们救回来之后,而今,不幸的她再一次面临生命的考验.魔王已经发出消息说将在T时刻吃掉公主,因为他听信谣言说吃公主的肉也能长生不老.年迈的国王正是心急如焚,告招天下 ...
- CF-311B Cats Transport(斜率优化DP)
题目链接 题目描述 小S是农场主,他养了 \(M\)只猫,雇了 \(P\) 位饲养员. 农场中有一条笔直的路,路边有 \(N\) 座山,从 \(1\) 到 \(N\)编号. 第 \(i\) 座山与第 ...
- Educational Codeforces Round 2 E. Lomsat gelral(dsu)
题目链接 题意:给你一棵以1为根n个点的树,问你以i为根的子树的众数和是多少 思路:dsu是一种优化暴力的手段 首先进行轻重链剖分 然后只记录重链的信息 轻链的信息就直接暴力查找 经过证明这样复杂度可 ...