springboot-02thymeleaf注意
来源:互联网 发布:淘宝二类医疗器械申请 编辑:程序博客网 时间:2024/06/13 19:27
默认配置
Spring Boot默认提供静态资源目录位置需置于classpath下,规则如下:
- /static
- /public
- /resources
- /META-INF/resources
也就是说资源放在这个下面是可以直接访问到的,如果需要渲染html页面就需要模板引擎了。
模板引擎
Spring Boot提供了多种模板引擎的默认配置支持。
Spring Boot提供的默认配置的模板引擎主要有以下几种:
- Thymeleaf
- FreeMarker
- Velocity
- Groovy
- Mustache
Spring Boot建议使用这些模板引擎,而不使用JSP,若一定要使用JSP将无法实现Spring Boot的多种特性。
当你使用上述模板引擎中的任何一个,它们默认的模板配置路径为:src/main/resources/templates
当然也可以修改这个路径。
Thymeleaf
Thymeleaf是一个XML/XHTML/HTML5模板引擎,可用于Web与非Web环境中的应用开发。它是一个开源的Java库,基于Apache License 2.0许可,由Daniel Fernández创建,该作者还是Java加密库Jasypt的作者。
Thymeleaf提供了一个用于整合Spring MVC的可选模块,在应用开发中,你可以使用Thymeleaf来完全代替JSP或其他模板引擎,如Velocity、FreeMarker等。Thymeleaf的主要目标在于提供一种可被浏览器正确显示的、格式良好的模板创建方式,因此也可以用作静态建模。你可以使用它创建经过验证的XML与HTML模板。相对于编写逻辑或代码,开发者只需将标签属性添加到模板中即可。接下来,这些标签属性就会在DOM(文档对象模型)上执行预先制定好的逻辑。
引入下面依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId></dependency>
Thymeleaf的默认参数配置
如有需要修改默认配置的时候,只需复制下面要修改的属性到application.properties
中,并修改成需要的值,如修改模板文件的扩展名,修改默认的模板路径等。
spring.thymeleaf.cache=true spring.thymeleaf.check-template-location=true spring.thymeleaf.content-type=text/html spring.thymeleaf.enabled=true spring.thymeleaf.encoding=UTF-8 spring.thymeleaf.excluded-view-names= spring.thymeleaf.mode=HTML5 spring.thymeleaf.prefix=classpath:/templates/ spring.thymeleaf.suffix=.html
在默认配置下,thymeleaf对.html的内容要求很严格,比如<input />,如果少最后的标签封闭符号/,就会报错而转到错误页。也比如你在使用Vue.js这样的库,然后有<div v-cloak></div>这样的html代码,也会被thymeleaf认为不符合要求而抛出错误。
因此,建议增加下面这段:
spring.thymeleaf.mode = LEGACYHTML5
spring.thymeleaf.mode的默认值是HTML5,其实是一个很严格的检查,改为LEGACYHTML5可以得到一个可能更友好亲切的格式要求。
需要注意的是,LEGACYHTML5需要搭配一个额外的库NekoHTML才可用。
引入依赖。
<dependency> <groupId>net.sourceforge.nekohtml</groupId> <artifactId>nekohtml</artifactId> <version>1.9.20</version></dependency>
- springboot-02thymeleaf注意
- SpringBoot -- thymeleaf
- springboot-thymeleaf
- SpringBoot-thymeleaf
- SpringBoot + thymeleaf
- springboot thymeleaf
- SpringBoot Thymeleaf 模板引擎
- SpringBoot-thymeleaf模板集成
- SpringBoot项目实战--thymeleaf
- springboot+jooq+thymeleaf
- springboot整合thymeleaf模板
- SpringBoot-mybatis- thymeleaf
- SpringBoot整合thymeleaf模板
- SpringBoot集成Thymeleaf模板
- Springboot集成thymeleaf
- Springboot集成thymeleaf
- springboot整合thymeleaf
- SpringBoot使用thymeleaf
- 4. Lucene各种查询操作(Lucene6.0.0)
- Java IO之字节流
- (58)组件之渲染组件
- HDU 2.1.7
- Lintcode53 Reverse Words in a String solution 题解
- springboot-02thymeleaf注意
- StringBuffer,StringBuilder和String的区别
- Lintcode54 String to Integer II solution 题解
- 如何在MacOS上玩儿汇编?
- 设计模式之单例模式的介绍及C++实现
- 冒泡排序
- JavaScript经典案例
- Lintcode55 Compare Strings solution 题解
- kotlin在Android studio3.0上的配置问题