Spring中的freemaker 和 thymeleaf
freemaker
-
添加依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId> </dependency>
-
添加配置文件
# freemarker配置 #=========================== # 是否开启thymeleaf缓存,本地为false,⽣生产建议为true spring.freemarker.cache=false spring.freemarker.charset=UTF-8 spring.freemarker.allow-request-override=false spring.freemarker.check-template-location=true #类型 spring.freemarker.content-type=text/html spring.freemarker.expose-request-attributes=true spring.freemarker.expose-session-attributes=true #⽂文件后缀 spring.freemarker.suffix=.ftl #路路径 spring.freemarker.template-loader-path=classpath:/templates/
-
取值得命名规则
@GetMapping("freemaker") public String index(ModelMap modelMap) { modelMap.addAttribute("setting", config); // 不需要加后缀 // 因为配置文件已经指定了后缀 // 返回得页面路径 return "user/fm/index"; } // ftl页面中获取属性 <h1>${setting.payAppid}</h1>
thymeleaf
-
添加依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
-
添加配置文件
# thymeleaf配置 #================================== #开发时关闭缓存,不不然没法看到实时⻚页⾯面 spring.thymeleaf.cache=false spring.thymeleaf.mode=HTML5 #前缀 spring.thymeleaf.prefix=classpath:/templates/ #编码 spring.thymeleaf.encoding=UTF-8 #类型 spring.thymeleaf.content-type=text/html #名称的后缀 spring.thymeleaf.suffix=.html
-
取值命名规则
@GetMapping("thymeleaf") public String index2(ModelMap modelMap) { modelMap.addAttribute("setting", config) // 不需要加后缀 // 因为配置文件已经指定了后缀 return "tl/index"; } // html 页面中获取得属性 // 在开头的html中得添加 <html xmlns:th="http://www/thymeleaf.org"> // 如何去获取 <p th:text="${setting.payAppid}"></p> <p th:text="${setting.paySecret}"></p>