springboot之模板
转:http://jisonami.iteye.com/blog/2301387,http://412887952-qq-com.iteye.com/blog/2292402
整体步骤:
(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
使用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";
- }
访问地址:http://127.0.0.1:8080/helloFtl
Hello.v.2
from TemplateController.helloFtl
thymeleaf和freemarker是可以共存的。
------------------------------------------------------------------------------------------------------------------------------------------------
本文记录一下几点:
一、资源文件的约定目录结构
二、Maven配置
三、开发时修改thymeleaf模板自动重新加载配置
四、thymeleaf常用基础知识点
一、资源文件的约定目录结构
Maven的资源文件目录:/src/Java/resources
spring-boot项目静态文件目录:/src/java/resources/static
spring-boot项目模板文件目录:/src/java/resources/templates
spring-boot静态首页的支持,即index.html放在以下目录结构会直接映射到应用的根目录下:
- classpath:/META-INF/resources/index.html
- classpath:/resources/index.html
- classpath:/static/index.html
- calsspath:/public/index.html
由于使用thymeleaf的HTML5模板,所以我将index.html模板文件直接放到了/src/java/resources/templates目录下。然而这个目录并不是首页文件的默认目录,所以我们需要手动将应用根路径映射到/src/java/resources/templates/index.html下。这个在spring-mvc的Controller下映射一下就可以了。
- @RequestMapping("/")
- public String index(){
- return "index";
- }
在spring-boot下,默认约定了Controller试图跳转中thymeleaf模板文件的的前缀prefix是”classpath:/templates/”,后缀suffix是”.html”
这个在application.properties配置文件中是可以修改的。
如下配置可以修改试图跳转的前缀和后缀
- spring.thymeleaf.prefix: /templates/
- spring.thymeleaf.suffix: .html
更过有关thymeleaf中的默认这是可以查看org.springframework.boot.autoconfigure.thymeleaf.ThymeleafProperties这个类的属性
二、Maven配置
在pom.xml中加入如下依赖
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-thymeleaf</artifactId>
- </dependency>
原来关于spring-boot-starter-web等的依赖就可以去掉了,因为spring-boot-starter-thymeleaf是包含这些依赖的。而关于jsp的依赖也可以去掉了,因为我们已经完全抛弃jsp了。
三、开发时修改thymeleaf模板自动重新加载配置
Spring-boot使用thymeleaf时默认是有缓存的,即你把一个页面代码改了不会刷新页面的效果,你必须重新运行spring-boot的main()方法才能看到页面更改的效果。我们可以把thymeleaf的缓存关掉,用于支持页面修改后重新发布到spring-boot内嵌的tomcat中去。在application.properties配置文件中加入以下配置。
- # Allow Thymeleaf templates to be reloaded at dev time
- spring.thymeleaf.cache: false
- server.tomcat.access_log_enabled: true
- server.tomcat.basedir: target/tomcat
四、thymeleaf常用基础知识点
1、在html页面中引入thymeleaf命名空间,即<html xmlns:th=http://www.thymeleaf.org></html>,此时在html模板文件中动态的属性使用th:命名空间修饰
2、引用静态资源文件,比如CSS和JS文件,语法格式为“@{}”,如@{/js/blog/blog.js}会引入/static目录下的/js/blog/blog.js文件
3、访问spring-mvc中model的属性,语法格式为“${}”,如${user.id}可以获取model里的user对象的id属性
4、循环,在html的标签中,加入th:each=“value:${list}”形式的属性,如<span th:each=”user:${users}”></span>可以迭代users的数据
5、判断,在html标签中,加入th:if=”表达式”可以根据条件显示html元素
<span th:if="${not #lists.isEmpty(blog.publishTime)}">
<span id="publishtime" th:text="${#dates.format(blog.publishTime, 'yyyy-MM-dd HH:mm:ss')}"></span>
</span>
以上代码表示若blog.publishTime时间不为空,则显示时间
6、时间的格式化,
${#dates.format(blog.publishTime,'yyyy-MM-dd HH:mm:ss')}
表示将时间格式化为”yyyy-MM-dd HH:mm:ss”格式化写法与Java格式化Date的写法是一致的。
7、字符串拼接,有两种形式
比如拼接这样一个URL:/blog/delete/{blogId}
第一种:th:href="'/blog/delete/' + ${blog.id }"
第二种:th:href="${'/blog/delete/' + blog.id }"
springboot之模板的更多相关文章
- SpringBoot thymeleaf模板页面没提示,SpringBoot thymeleaf模板插件安装
SpringBoot thymeleaf模板插件安装 SpringBoot thymeleaf模板Html页面没提示 SpringBoot thymeleaf模板页面没提示 SpringBoot t ...
- SpringBoot thymeleaf模板版本,thymeleaf模板更换版本
SpringBoot thymeleaf模板版本 thymeleaf模板更换版本 修改thymeleaf模板版本 ================================ ©Copyright ...
- springboot集成模板引擎freemarker和thymeleaf
freemarkder和thymeleaf都是java的模板引擎,这里只介绍这两种模板引擎如何在sprongboot中配置: 1. freemarkder 1.1 在pom.xml中添加依赖包 < ...
- SpringBoot前端模板
Springboot支持thymeleaf.freemarker.JSP,但是官方不建议使用JSP,因为有些功能会受限制,这里介绍thymeleaf和freemarker. 一.thymeleaf模板 ...
- SpringBoot整合模板引擎
一.SpringBoot整合freemarker: 1.引入freemarker模板依赖: <dependency> <groupId>org.springframework. ...
- springboot+thymeleaf 模板中传递参数误报错误 红色波浪线
在使用IDEA开发SpringBoot项目时,使用了Thymeleaf模板引擎,在使用动态传参数时,HTML页面的动态参数出现了红色波浪线,情况如下如: 解决办法: 选择 File -> Set ...
- SpringBoot:模板引擎 thymeleaf、ContentNegotiatingViewResolver、格式化转换器
目录 模板引擎 thymeleaf ContentNegotiatingViewResolver 格式化转换器 模板引擎 thymeleaf.ContentNegotiatingViewResolve ...
- SpringBoot—单元测试模板(controller层和service层)
介绍 概述 在开发过程中,我们经常会一股脑的写各种业务逻辑,经常等全部大功告成的时候,打个jar包放环境里跑跑看看能不能通,殊不知在各个业务方法中已经漏洞百出,修复一个打一个包,再继续修复,这种效 ...
- springboot 新模板 呵呵了
<html> <head> <title>批处理任务管理</title> <meta name="decorator" con ...
随机推荐
- xshell怎样打印
Xshell提供用本地打印机打印终端窗口文本的功能.在Xshell打印时可以沿用终端窗口使用的字体及颜色.且在页面设置对话框可以设置打印纸的边距. 如何设置打印纸的大小和方向: 1.打开xshell ...
- Java线程和多线程(十)——TimerTask
Java中的java.util.Timer是一个工具类,可以用于调度一个线程在将来的某一个时刻执行特定的任务.Java Timer类可以将一个任务定时执行一次,或者是以后以每隔一定的时间间隔来触发一次 ...
- PIC24 通过USB在线升级 -- USB CDC bootloader
了解bootloader的实现,请加QQ: 1273623966 (验证填bootloader):欢迎咨询或定制bootloader:我的博客主页www.cnblogs.com/geekygeek 今 ...
- Oracle 完全理解connect by-详细脚本-可实战
狒狒Q971751392 未来星开发团队--狒狒(QQ:9715234) oracle树查询的最重要的就是select…start with…connect by…prior语法了.依托于该语法,我们 ...
- golang 小知识点记录
获取url中的参数及输出到页面的几种方式 func SayHello(w http.ResponseWriter, req *http.Request) { req.Method //获取url的方法 ...
- 构建Http服务器
可以通过多种途径来构建服务器用以响应客户端请求(~不提供实现源码,网上有相应资源~) (1)使用ServerSocket构建服务器 (2)使用Servlet构建服务器 (3)使用HttpServer构 ...
- 孤荷凌寒自学python第六十七天初步了解Python爬虫初识requests模块
孤荷凌寒自学python第六十七天初步了解Python爬虫初识requests模块 (完整学习过程屏幕记录视频地址在文末) 从今天起开始正式学习Python的爬虫. 今天已经初步了解了两个主要的模块: ...
- (原)自定义资源预览工具:DZAssetPreviewPlugin(1)
@author: 白袍小道 转载请说明,谢谢 题记 后续工具制作单独作为一本(小道用的是OneNote,这样发布简单点.*--*) 总计放到写完后. 目的 1.快速预览资源(因为大部分 ...
- Spark实战练习03--Pair RDD
一.场景 现有某网站的网站日志,内容为用户对网站的请求,包含user ID.IP address.datetime……等等 另有一份文件中包含用户的账户详细信息数据,包含User ID.creatio ...
- Spring 学习笔记(八)—— 注解使用整合
@Autowired —— 自动装配 需先在配置文件中,配置一个org.springframework.beans.factory.annotation. AutowiredAnnotationBe ...