spring mvc 渲染html,在Spring MVC中使用Thymeleaf模板渲染Web视图
Thymeleaf模板是原生的,不依赖于标签库。它能在接受原始HTML的地方进行编辑和渲染。由于没有与Servlet规范耦合,因此Thymeleaf模板能够进入JSP所无法涉及的领域
如果想要在Spring MVC项目中使用Thymeleaf模板,那么只需要做到以下几步即可:
(1)配置使用Thymeleaf模板渲染Web视图:
在没有配置各种模板渲染技术之前,通常我们会这样配置:
1
2
3
4
因此现在首先需要注释掉上面的配置,同时使用JavaConfig来引入Thymeleaf模板渲染Web视图:
Java
package cn.zifangsky.config;
import javax.servlet.ServletContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.thymeleaf.spring4.SpringTemplateEngine;
import org.thymeleaf.spring4.view.ThymeleafViewResolver;
import org.thymeleaf.templateresolver.ServletContextTemplateResolver;
/**
* 使用Thymeleaf模板渲染Web视图
* @author zifangsky
*
*/
@Configuration
public class TemplateConfig {
@Bean
public ServletContextTemplateResolver templateResolver(ServletContext servletContext){
ServletContextTemplateResolver templateResolver = new ServletContextTemplateResolver(servletContext);
templateResolver.setPrefix("/WEB-INF/templates/");
templateResolver.setSuffix(".html");
templateResolver.setTemplateMode("HTML5");
templateResolver.setCharacterEncoding("UTF-8");
return templateResolver;
}
@Bean
public SpringTemplateEngine templateEngine(ServletContextTemplateResolver templateResolver){
SpringTemplateEngine templateEngine = new SpringTemplateEngine();
templateEngine.setTemplateResolver(templateResolver);
return templateEngine;
}
@Bean
public ViewResolver viewResolver(SpringTemplateEngine templateEngine){
ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
viewResolver.setTemplateEngine(templateEngine);
viewResolver.setCharacterEncoding("UTF-8");
return viewResolver;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49packagecn.zifangsky.config;
importjavax.servlet.ServletContext;
importorg.springframework.context.annotation.Bean;
importorg.springframework.context.annotation.Configuration;
importorg.springframework.web.servlet.ViewResolver;
importorg.thymeleaf.spring4.SpringTemplateEngine;
importorg.thymeleaf.spring4.view.ThymeleafViewResolver;
importorg.thymeleaf.templateresolver.ServletContextTemplateResolver;
/**
* 使用Thymeleaf模板渲染Web视图
* @author zifangsky
*
*/
@Configuration
publicclassTemplateConfig{
@Bean
publicServletContextTemplateResolvertemplateResolver(ServletContextservletContext){
ServletContextTemplateResolvertemplateResolver=newServletContextTemplateResolver(servletContext);
templateResolver.setPrefix("/WEB-INF/templates/");
templateResolver.setSuffix(".html");
templateResolver.setTemplateMode("HTML5");
templateResolver.setCharacterEncoding("UTF-8");
returntemplateResolver;
}
@Bean
publicSpringTemplateEnginetemplateEngine(ServletContextTemplateResolvertemplateResolver){
SpringTemplateEnginetemplateEngine=newSpringTemplateEngine();
templateEngine.setTemplateResolver(templateResolver);
returntemplateEngine;
}
@Bean
publicViewResolverviewResolver(SpringTemplateEnginetemplateEngine){
ThymeleafViewResolverviewResolver=newThymeleafViewResolver();
viewResolver.setTemplateEngine(templateEngine);
viewResolver.setCharacterEncoding("UTF-8");
returnviewResolver;
}
}
注:这里还需要在Spring MVC的配置文件中配置自动扫描上面的JavaConfig所在的包,也就是:
XHTML
1
(2)定义一个简单模板home.html:
从上面的配置可知,需要将模板文件放置到/WebContent/WEB-INF/templates/home.html路径下,其内容如下:
XHTML
标题
Hello
The year is 1949.
1
2
3
4
5
6
7
8
9
10
11
12
13
标题
Hello
The year is1949.
spring mvc 渲染html,在Spring MVC中使用Thymeleaf模板渲染Web视图相关推荐
- Spring MVC应用程序中的Thymeleaf模板布局,无扩展
在使用JSP / JSTL和Apache Tiles几年之后,我开始为我的Spring MVC应用程序发现Thymeleaf. Thymeleaf是一个非常出色的视图引擎,尽管目前缺乏良好的Intel ...
- 第3章 Thymeleaf模板渲染
文章目录 第3章 Thymeleaf模板渲染 3.2 Thymeleaf编程起步 3.4 读取资源文件 3.5 路径处理 3.6 内置对象操作支持 3.7 对象输出 3.8 页面逻辑处理 3.9 数据 ...
- 【SpringBoot】3、SpringBoot中整合Thymeleaf模板引擎
SpringBoot 为我们提供了 Thymeleaf 自动化配置解决方案,所以我们在 SpringBoot 中使用 Thymeleaf 非常方便 一.简介 Thymeleaf是一个流行的模板引擎,该 ...
- flask mysql项目模板渲染_[Flask] Flask问题集(后端模板渲染项目)
1.redirect和render_template的区别? redirect:重定向,会改变url render_template:模板渲染,用模板来渲染当前页,不会改变url 2.关于 'g' 对 ...
- Idea开发springBoot 中使用 thymeleaf 模板$表达式报红波浪线解决方案
html页面设置前如下显示: 我的Idea版本是2021.2.4,解决过程如下: 1.在idea上方菜单栏中选择File --> Settings 2.在settinngs --> In ...
- javascript --- vue中简单的模板渲染
一层的渲染 将下面的模板中的mustache语法使用给定数据渲染. 模板如下 <div id="root"><div><div><p> ...
- SpringBoot中使用thymeleaf模板时select下拉框怎样查询数据库赋值
场景 实现效果 数据库中码表设计 实现 thymeleaf页面代码 当前页面为: receiveOrder/wmsReceiveOrder.html <div class="col-s ...
- thymeleaf html模块化,SpringBoot中使用Thymeleaf模板开发的后台管理框架
系统简介 JavaWeb专业版,企业级开发框架SpringBoot+Layui+Thymeleaf+MybatisPlus开发权限(RBAC)及内容管理框架,框架中集成了权限管理.模块管理,数据库管理 ...
- 《Spring In Action(第4版)》阅读总结(四)渲染Web视图
渲染Web视图 将模型数据渲染为HTML 使用JSP视图 通过tiles定义视图布局 使用Thymealeaf视图 理解视图渲染 控制器只负责处理请求并返回模型数据和视图名 将控制器中请求处理逻辑与视 ...
最新文章
- win10 UWP 应用设置
- c语言统计最长单词长度,求3个字符串中最长单词的长度 求救 会一个的
- tomcat双击startup.bat启动时闪退
- 一台机器上运行多个ActiveMq
- web.xml上下文配置
- 网站访问过程HTML
- 项上的温柔,冬季围巾8大打法图解(组图)
- 使用Python+md5删除本地重复(同一张不重名)的照片
- HTML代码页面无法跳转为什么,html跳转新页面代码_html页面跳转代码
- cpu压测 windows_小白求问怎么用AIDA64进行CPU压力测试
- kindle看pdf不清楚_你不知道的kindle技巧:如何优雅地用kindle看pdf文档?
- 计算机绘画社团活动教案,电脑绘画社团教案
- Linux之父:不担心微软接管Linux
- 计算机一级二级三级缓存,CPU一级缓存,二级缓存和三级缓存是什么意思? CPU缓存有什么用?...
- WordPress安装时未选择语言导致变成英文无法修改成中文
- 【我的Android进阶之旅】解决使用DiskLruCache报错: keys must match regex [a-z0-9_-]{1,64}
- mysql的安装使用(免安装版)
- 画出Android的大体架构图
- 打码兼职练习程序下载及说明
- android手机最低内存,安卓想用很久不卡顿?12GB内存是最低标准,这6款硬核配置还便宜...
热门文章
- 百度seo指南_什么是SEO?网站怎么做SEO优化(问答篇)
- python中的pass语句_Python中pass的作用与使用代码示例
- 让对方qq崩溃的代码2020_解决 KUbuntu 18.04 下 Deepin Wine QQ 运行十分钟后崩溃的问题...
- 计算机网络管理第三版试卷,《计算机网络管理技术》试卷.doc
- python下载百度云盘中的数据_python爬虫:爬取百度云盘资料,保存下载地址、链接标题、链接详情...
- 2014-05-08 总结(补充)
- 洛谷P3810 【模板】三维偏序(陌上花开) CDQ分治初探
- pyenv 让 python 版本完美切换
- C#方式操作Cookie
- 壊小子的学习【日常阅读篇】(四)与阅读有关的那些事儿