標籤:boot spring app classpath framework imp cat util rom
freemark模板和thymeleaf模板1.在pom.xml中引入thymeleaf和freemark;
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId></dependency><dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId></dependency>
2.在application.xml中配置
###########################################################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 refreshspring.thymeleaf.cache=false ###########################################################FREEMARKER (FreeMarkerAutoConfiguration)########################################################spring.freemarker.allow-request-override=falsespring.freemarker.cache=truespring.freemarker.check-template-location=truespring.freemarker.charset=UTF-8spring.freemarker.content-type=text/htmlspring.freemarker.expose-request-attributes=falsespring.freemarker.expose-session-attributes=falsespring.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-separated list#spring.freemarker.view-names= # whitelist of view names that can be resolved
3.編寫模板
//注意路徑是在配置中預設的,也是可以修改的<!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>--------freemark模板入下<!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>Freemark</h1> <p>${hello}</p> </body></html>
4.controller層
package com.ithuan.demo.controller;import java.util.Map;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;@Controller//@RequestMapping("/templates")public class TemplateController {@RequestMapping("/hello1111")public String hello1(Map<String,Object> map){System.err.println("----");map.put("hello","from TemplateController.helloHtml");return "hello1";}@RequestMapping("/hello22")public String hello22(Map map){System.err.println("2222");map.put("hello", "最終的大boss");return "freemakr";}}
jsp頁面展內建的解析不再展示
springboot中模板 freemark,thymeleaf,jsp