(18)使用模板(thymeleaf-freemarker)【从零开始学Spring Boot】
整体步骤:
(1) 在pom.xml中引入thymeleaf;
(2) 如何关闭thymeleaf缓存
(3) 编写模板文件.html
Spring Boot默认就是使用thymeleaf模板引擎的,所以只需要在pom.xml加入依赖即可:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
Thymeleaf缓存在开发过程中,肯定是不行的,那么就要在开发的时候把缓存关闭,只需要在application.properties进行配置即可:
########################################################
###THYMELEAF (ThymeleafAutoConfiguration)
########################################################
#spring.thymeleaf.prefix=classpath:/templates/
#spring.thymeleaf.suffix=.html
#spring.thymeleaf.mode=HTML5
#spring.thymeleaf.encoding=UTF-8
# ;charset=<encoding> is added
#spring.thymeleaf.content-type=text/html
# set to false for hot refresh
spring.thymeleaf.cache=false
编写模板文件src/main/resouces/templates/helloHtml.html
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
<head>
<title>Hello World!</title>
</head>
<body>
<h1 th:inline="text">Hello.v.2</h1>
<p th:text="${hello}"></p>
</body>
</html>
编写访问路径(com.kfit.test.web.TemplateController):
package com.kfit.test.web;
import java.util.Map;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* 模板测试.
* @author Administrator
*
*/
@Controller
publicclass TemplateController {
/**
* 返回html模板.
*/
@RequestMapping("/helloHtml")
public String helloHtml(Map<String,Object> map){
map.put("hello","from TemplateController.helloHtml");
return"/helloHtml";
}
}
启动应用,输入地址:http://127.0.0.1:8080/helloHtml 会输出:
Hello.v.2
from TemplateController.helloHtml
18.2 使用freemarker
使用freemarker也很简单,
在pom.xml加入freemarker的依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
剩下的编码部分都是一样的,说下application.properties文件:
########################################################
###FREEMARKER (FreeMarkerAutoConfiguration)
########################################################
spring.freemarker.allow-request-override=false
spring.freemarker.cache=true
spring.freemarker.check-template-location=true
spring.freemarker.charset=UTF-8
spring.freemarker.content-type=text/html
spring.freemarker.expose-request-attributes=false
spring.freemarker.expose-session-attributes=false
spring.freemarker.expose-spring-macro-helpers=false
#spring.freemarker.prefix=
#spring.freemarker.request-context-attribute=
#spring.freemarker.settings.*=
#spring.freemarker.suffix=.ftl
#spring.freemarker.template-loader-path=classpath:/templates/#comma-separatedlist
#spring.freemarker.view-names= #whitelistofviewnamesthatcanberesolved
com.kfit.test.web.TemplateController:
/**
* 返回html模板.
*/
@RequestMapping("/helloFtl")
public String helloFtl(Map<String,Object> map){
map.put("hello","from TemplateController.helloFtl");
return"/helloFtl";
}
src/main/resouces/templates/helloFtl.ftl
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
<head>
<title>Hello World!</title>
</head>
<body>
<h1>Hello.v.2</h1>
<p>${hello}</p>
</body>
</html>
访问地址:http://127.0.0.1:8080/helloFtl
Hello.v.2
from TemplateController.helloFtl
thymeleaf和freemarker是可以共存的。
【Spring Boot 系列博客】
58. Spring Boot国际化(i18n)【从零开始学Spring Boot】
57. Spring 自定义properties升级篇【从零开始学Spring Boot】
56. spring boot中使用@Async实现异步调用【从零开始学Spring Boot】
55. spring boot 服务配置和部署【从零开始学Spring Boot】
54. spring boot日志升级篇—logback【从零开始学Spring Boot】
52. spring boot日志升级篇—log4j多环境不同日志级别的控制【从零开始学Spring Boot】
51. spring boot属性文件之多环境配置【从零开始学Spring Boot】
50. Spring Boot日志升级篇—log4j【从零开始学Spring Boot】
49. spring boot日志升级篇—理论【从零开始学Spring Boot】
48. spring boot单元测试restfull API【从零开始学Spring Boot】
47. Spring Boot发送邮件【从零开始学Spring Boot】
46. Spring Boot中使用AOP统一处理Web请求日志
45. Spring Boot MyBatis连接Mysql数据库【从零开始学Spring Boot】
44. Spring Boot日志记录SLF4J【从零开始学Spring Boot】
43. Spring Boot动态数据源(多数据源自动切换)【从零开始学Spring Boot】
42. Spring Boot多数据源【从零开始学Spring Boot】
41. Spring Boot 使用Java代码创建Bean并注册到Spring中【从零开始学Spring Boot】
40. springboot + devtools(热部署)【从零开始学Spring Boot】
39.4 Spring Boot Shiro权限管理【从零开始学Spring Boot】
39.3 Spring Boot Shiro权限管理【从零开始学Spring Boot】
39.2. Spring Boot Shiro权限管理【从零开始学Spring Boot】
39.1 Spring Boot Shiro权限管理【从零开始学Spring Boot】
38 Spring Boot分布式Session状态保存Redis【从零开始学Spring Boot】
37 Spring Boot集成EHCache实现缓存机制【从零开始学Spring Boot】
36 Spring Boot Cache理论篇【从零开始学Spring Boot】
35 Spring Boot集成Redis实现缓存机制【从零开始学Spring Boot】
34Spring Boot的启动器Starter详解【从零开始学Spring Boot】
33 Spring Boot 监控和管理生产环境【从零开始学Spring Boot】
32 Spring Boot使用@SpringBootApplication注解【从零开始学Spring Boot】
(18)使用模板(thymeleaf-freemarker)【从零开始学Spring Boot】的更多相关文章
- Spring Boot使用模板freemarker【从零开始学Spring Boot(转)
视频&交流平台: à SpringBoot网易云课堂视频 http://study.163.com/course/introduction.htm?courseId=1004329008 à ...
- 77. Spring Boot Use Thymeleaf 3【从零开始学Spring Boot】
[原创文章,转载请注明出处] Spring Boot默认选择的Thymeleaf是2.0版本的,那么如果我们就想要使用3.0版本或者说指定版本呢,那么怎么操作呢?在这里要说明下 3.0的配置在spri ...
- 68. 使用thymeleaf报异常:Not Found, status=404【从零开始学Spring Boot】
[从零开始学习Spirng Boot-常见异常汇总] 我们按照正常的流程编码好了 controller访问访问方法/hello,对应的是/templates/hello.html文件,但是在页面中还是 ...
- 59. Spring Boot Validator校验【从零开始学Spring Boot】
大纲: (1) 入门例子: (2) 国际化: (3) 在代码中添加错误信息: (1) 入门例子: Validator主要是校验用户提交的数据的合理性的,比如是否为空了,密码长度是否大于6位,是否是纯数 ...
- 58. Spring Boot国际化(i18n)【从零开始学Spring Boot】
国际化(internationalization)是设计和制造容易适应不同区域要求的产品的一种方式.它要求从产品中抽离所有地域语言,国家/地区和文化相关的元素.换言之,应用程序的功能和代码设计考虑在不 ...
- 47. Spring Boot发送邮件【从零开始学Spring Boot】
(提供源代码) Spring提供了非常好用的JavaMailSender接口实现邮件发送.在Spring Boot的Starter模块中也为此提供了自动化配置.下面通过实例看看如何在Spring Bo ...
- (35)Spring Boot集成Redis实现缓存机制【从零开始学Spring Boot】
[本文章是否对你有用以及是否有好的建议,请留言] 本文章牵涉到的技术点比较多:Spring Data JPA.Redis.Spring MVC,Spirng Cache,所以在看这篇文章的时候,需要对 ...
- (29)Spring boot 文件上传(多文件上传)【从零开始学Spring Boot】
文件上传主要分以下几个步骤: (1)新建maven java project: (2)在pom.xml加入相应依赖: (3)新建一个表单页面(这里使用thymleaf); (4)编写controlle ...
- (19)Spring Boot 添加JSP支持【从零开始学Spring Boot】
[来也匆匆,去也匆匆,在此留下您的脚印吧,转发点赞评论: 您的认可是我最大的动力,感谢您的支持] 看完本文章您可能会有些疑问,可以查看之后的一篇博客: 81. Spring Boot集成JSP疑问[从 ...
随机推荐
- spring与springboot中,如何在static方法里使用自动注入的属性
第一步:写注解@Component 使当前类成为一个bean对象.(@Controller,@service都行) 第二步:写个static的变量 第三步:写个@PostConstruct注解注解注释 ...
- HDU 5536/ 2015长春区域 J.Chip Factory Trie
Chip Factory Problem Description John is a manager of a CPU chip factory, the factory produces lots ...
- 用JS将指定时间转化成用户当地时区的时间
公司的项目是面向海外用户的,但是最初的设计没考虑到时差问题,存入数据库的时间都是东八区的时间,导致现在补救有点坑爹...... 有一个需求是,产品详细页需要注明此款产品的开售时间,当海外的用户来访问这 ...
- Hardwood Species(map)
http://poj.org/problem?id=2418 题意:给定一系列字符串,要求按字典序升序输出每个串,并输出每个串出现的百分比. 用map做的,交c++A了,G++ WA..so sad. ...
- Django day14(二) Session
Session 1. Session干什么用的? 解决cookie不安全的问题, 2. 原理是: 1) 存在服务器上的键值对{'sdaf随机字符串':{name:prince,pwd:123}} 2 ...
- Django day02
一:Django 中 app 的概念 每个项目里面都会 有很多不同的模块,我们可以把它们写在一个项目里,我们把模块分成一个一个不同的app,这样写可以便于管理,写的一些项目也可能不单单是一个页面,还可 ...
- Python迭代器(斐波拉切数列实例)
将一个容器通过iter()函数处理后,就变成了迭代器.迭代器有2个魔法方法__iter__.__next__,一个迭代器必须实现__iter__,这个方法实际上是返回迭代器本身(return self ...
- X - Vasya and Socks
Problem description Vasya has n pairs of socks. In the morning of each day Vasya has to put on a pai ...
- java selenium启动火狐浏览器报错:Cannot find firefox binary in PATH. Make sure firefox is installed. OS appears to be: VISTA Build info: version: '3.8.1', revision: '6e95a6684b', time: '2017-12-01T19:05:14.666Z
Cannot find firefox binary in PATH. Make sure firefox is installed. OS appears to be: VISTA Build in ...
- 解决QQ未启用状态,QQ留言图标未启用
最近由于腾讯升级QQ一些东西,导致QQ图标成未启用状态:如图 解决方法,到腾讯此站点登陆一下即可, http://wp.qq.com/set.html 另外设置 没有保存按钮,如果选择完全公开,到自己 ...