一、 springboot整合freemarker+整合thymeleaf

一、 springboot整合freemarker

demo/pom.xml
<!-- 引入 freemarker 模板依赖 -->
	<dependency>
			<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-freemarker</artifactId>
	</dependency>	

src/main/resources/application.properties

############################################################
#
# freemarker 静态资源配置
#
############################################################
#设定ftl模版文件的路径
spring.freemarker.template-loader-path=classpath:/templates
#关闭缓存,即时刷新。上线生产环境需要改为true
spring.freemarker.cache=false
spring.freemarker.charset=UTF-8
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.request-context-attribute=request
spring.freemarker.suffix=.ftl

模版文件
src/main/resources/templates/freemarker/index.ftl

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8" />
    <title></title>
</head>
<body>
FreeMarker模板引擎
<h1>${resource.name}</h1>
<h1>${resource.website}</h1>
<h1>${resource.language}</h1>
</body>
</html>

src/main/resources/templates/freemarker/center/center.ftl

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8" />
    <title></title>
</head>
<body>
FreeMarker模板引擎
<h1>center page</h1>
</body>
</html>

com.oracle.controller/FreemarkerController.java

package com.oracle.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import com.oracle.pojo.Resource;

@Controller
@RequestMapping("ftl")
public class FreemarkerController {

	@Autowired
	private Resource resource;
	
	@RequestMapping("/index")
    public String index(ModelMap map) {
        map.addAttribute("resource", resource);
        return "freemarker/index";
    }
	//测试多层路径,后缀在配置文件中已经配置好。
	@RequestMapping("/center")
    public String center() {
        return "freemarker/center/center";
    }
}

测试:http://localhost:8080/demo/ftl/index

二、 springboot整合thymeleaf

  1. 第一个demo
    demo/pom.xml
<!-- 引入 thymeleaf 模板依赖 -->
	<dependency>
			<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-thymeleaf</artifactId>
	</dependency>

src/main/resources/application.properties

############################################################
#
# thymeleaf 静态资源配置
#
############################################################
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
spring.thymeleaf.mode=HTML5
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.content-type=text/html
#关闭缓存,即时刷新,上线生产环境要改为true
spring.thymeleaf.cache=false

com.oracle.controller/ThymeleafController.java

package com.oracle.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import com.oracle.pojo.User;

@Controller
@RequestMapping("th")
public class ThymeleafController {

	@RequestMapping("/index")
    public String index(ModelMap map) {
        map.addAttribute("name", "tom");
        return "thymeleaf/index";
    }
	
	@RequestMapping("center")
    public String center() {
        return "thymeleaf/center/center";
    }
}

src/main/resources/templates /thymeleaf/index.html

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8" />
    <title></title>
</head>
<body>
Thymeleaf模板引擎
<h1 th:text="${name}">hello world~~~~~~~</h1>
</body>
</html>

src/main/resources/templates/thymeleaf/center/center.html

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8" />
    <title></title>
</head>
<body>
Thymeleaf模板引擎
<h1>center page</h1>
</body>
</html>

Bug分析:
spring-boot-starter-thymeleaf默认使用 Thymeleaf 2.1,而当前SpringBoot版本是1.5.2不支持该版本。
demo/pom.xml

 <parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.10.RELEASE</version>
		<relativePath /> <!-- lookup parent from repository -->
	</parent>
  1. 静态资源引入
    src/main/resources/application.properties
#设定静态文件路径js,css
spring.mvc.static-path-pattern=/static/**

src/main/resources/static/js/test.js

alert("hello js");

com.oracle.controller/ThymeleafController.java

@RequestMapping("/test")
    public String test(ModelMap map) {
        return "thymeleaf/test";
    }

src/main/resources/templates/thymeleaf/test.html

 <!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8" />
    <title></title>
<script th:src="@{/static/js/test.js}"></script>
</head>
<body>
</body>
</html>
  1. 处理对象
    com.oracle.controller/ThymeleafController.java

@RequestMapping("/test")
public String test(ModelMap map) {
User u = new User();
u.setName(“superadmin”);
u.setAge(10);
u.setPassword(“123465”);
u.setBirthday(new Date());
u.setDesc(“hello tom”);

map.addAttribute("user", u);

User u1 = new User();
u1.setAge(19);
u1.setName("jerry");
u1.setPassword("123456");
u1.setBirthday(new Date());

User u2 = new User();
u2.setAge(17);
u2.setName("mike");
u2.setPassword("123456");
u2.setBirthday(new Date());

List<User> userList = new ArrayList<>();
userList.add(u);
userList.add(u1);
userList.add(u2);

map.addAttribute("userList", userList);

return "thymeleaf/test";

}

src/main/resources/templates/thymeleaf/test.html

<div>
	用户姓名:<input th:id="${user.name}" th:name="${user.name}" th:value="${user.name}"/>
	<br/>
	用户年龄:<input th:value="${user.age}"/>
	<br/>
	用户生日:<input th:value="${user.birthday}"/>
	<br/>
	用户生日:<input th:value="${#dates.format(user.birthday, 'yyyy-MM-dd')}"/>
	<br/>
</div>

<br/>

<div th:object="${user}">
	用户姓名:<input th:id="*{name}" th:name="*{name}" th:value="*{name}"/>
	<br/>
	用户年龄:<input th:value="*{age}"/>
	<br/>
	用户生日:<input th:value="*{#dates.format(birthday, 'yyyy-MM-dd hh:mm:ss')}"/>
	<br/>
</div>

<br/>

text 与 utext :<br/>
<span th:text="${user.desc}">abc</span>
<br/>
<span th:utext="${user.desc}">abc</span>
<br/>
  1. 对get提交和post提交的处理
    src/main/resources/templates/thymeleaf/test.html
URL:<br/>
<a href="" th:href="@{http://www.baidu.com}">百度</a>
<br/>

<br/>
<!-- method和th:method两个用其中之一,都可以 -->
<form th:action="@{/th/postform}" th:object="${user}" method="post" th:method="post">
    <input type="text" th:field="*{name}"/>
    <input type="text" th:field="*{age}"/>
    <input type="submit"/>
</form>
com.oracle.controller/ThymeleafController.java
@PostMapping("/postform")
    public String postform(User u) {
		System.out.println("姓名:" + u.getName());
		System.out.println("年龄:" + u.getAge());
        return "redirect:/th/test";
    }
  1. 判断语句,循环语句
    src/main/resources/templates/thymeleaf/test.html
<div th:if="${user.age} == 18">十八岁的天空</div>
<div th:if="${user.age} gt 18">你老了</div>
<div th:if="${user.age} lt 18">你很年轻</div>
<div th:if="${user.age} ge 18">大于等于</div>
<div th:if="${user.age} le 18">小于等于</div>
<br/>

<br/>
<select>
     <option >选择框</option>
     <option th:selected="${user.name eq 'tom'}">tom</option>
     <option th:selected="${user.name eq 'jerry'}">jerry</option>
     <option th:selected="${user.name eq 'mike'}">mike</option>
</select>
<br/>

<br/>

<table>
    <tr>
        <th>姓名</th>
        <th>年龄</th>
        <th>年龄备注</th>
        <th>生日</th>
    </tr>
    <tr th:each="person:${userList}">
        <td th:text="${person.name}"></td>
        <td th:text="${person.age}"></td>
        <td th:text="${person.age gt 18} ? 你老了 : 你很年轻">18岁</td>
        <td th:text="${#dates.format(user.birthday, 'yyyy-MM-dd hh:mm:ss')}"></td>
    </tr>
</table>
<br/>
  1. 读取资源文件
    src/main/resources/application.properties
#配置i18n资源文件,供thymeleaf读取
spring.messages.basename=i18n/messages
spring.messages.cache-seconds=3600
spring.messages.encoding=UTF-8
src/main/resources/i18n/messages.properties
############################################################
#用户自定义权限
#
############################################################
#普通管理员
roles.manager=manager
#超级管理员
roles.superadmin=superadmin
src/main/resources/templates/thymeleaf/test.html
<div th:switch="${user.name}">
  <p th:case="'lee'">lee</p>
  <p th:case="#{roles.manager}">普通管理员</p>
  <p th:case="#{roles.superadmin}">超级管理员</p>
  <p th:case="*">其他用户</p>
</div>
发布了162 篇原创文章 · 获赞 13 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_39088066/article/details/103125581