Skip to content

Commit

Permalink
spring boot 集成 thymeleaf 模板引擎
Browse files Browse the repository at this point in the history
  • Loading branch information
xkcoding committed Nov 27, 2017
1 parent e99dc57 commit f83ee1e
Show file tree
Hide file tree
Showing 9 changed files with 197 additions and 3 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Spring Boot Demo

spring boot demo 是一个用来学习 spring boot 的项目,已经集成 actuator(监控)、admin(可视化监控)、logback(日志)、aopLog(通过 AOP 记录 web 请求日志)、统一异常处理( json 级别和页面级别)、freemarker、JPA(ORM 框架)、mybatis(ORM 框架)、redis-cache(缓存)、swagger(API 接口管理测试)、ureport2(中国式报表)模块,后续会集成activemq,email,shiro,websocket,quartz,netty等模块。
spring boot demo 是一个用来学习 spring boot 的项目,已经集成 actuator(监控)、admin(可视化监控)、logback(日志)、aopLog(通过 AOP 记录 web 请求日志)、统一异常处理( json 级别和页面级别)、freemarker(模板引擎)、thymeleaf(模板引擎)、JPA(ORM 框架)、mybatis(ORM 框架)、redis-cache(缓存)、swagger(API 接口管理测试)、ureport2(中国式报表)模块,后续会集成activemq,email,shiro,websocket,quartz,netty等模块。

依赖的 Spring Boot 版本:

Expand Down Expand Up @@ -53,6 +53,7 @@ spring boot demo 是一个用来学习 spring boot 的项目,已经集成 actu
<module>../spring-boot-demo-aoplog</module>
<module>../spring-boot-demo-exceptionhandler</module>
<module>../spring-boot-demo-template-freemarker</module>
<module>../spring-boot-demo-template-thymeleaf</module>
<module>../spring-boot-demo-orm-jpa</module>
<module>../spring-boot-demo-orm-mybatis</module>
<module>../spring-boot-demo-cache-redis</module>
Expand Down Expand Up @@ -152,6 +153,7 @@ spring boot demo 是一个用来学习 spring boot 的项目,已经集成 actu
| [spring-boot-demo-aoplog](./spring-boot-demo-aoplog) | spring-boot 使用 AOP 切面的方式记录 web 请求日志 |
| [spring-boot-demo-exceptionhandler](./spring-boot-demo-exceptionhandler) | spring-boot 统一异常处理,包括2种,一种返回统一的 json 格式,第二种统一跳转到异常页面 |
| [spring-boot-demo-template-freemarker](./spring-boot-demo-template-freemarker) | spring-boot 集成 freemarker 模板引擎 |
| [spring-boot-demo-template-thymeleaf](./spring-boot-demo-template-thymeleaf) | spring-boot 集成 thymeleaf 模板引擎 |
| [spring-boot-demo-orm-jpa](./spring-boot-demo-orm-jpa) | spring-boot 集成 spring-boot-starter-data-jpa 操作数据库 |
| [spring-boot-demo-orm-mybatis](./spring-boot-demo-orm-mybatis) | spring-boot 集成 [mybatis-spring-boot-starter](https://github.com/mybatis/spring-boot-starter)[mybatis-spring-boot-starter](https://github.com/alibaba/druid/tree/master/druid-spring-boot-starter) |
| [spring-boot-demo-cache-redis](./spring-boot-demo-cache-redis) | spring-boot 使用 Redis 做缓存 |
Expand Down
4 changes: 2 additions & 2 deletions TODO.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# spring-boot-demo 项目

## 模块计划(已完成:13 / 35)
## 模块计划(已完成:14 / 35)

- [x] ~~spring-boot-demo-helloworld(Helloworld 示例)~~
- [x] ~~spring-boot-demo-properties(读取配置文件信息)~~
Expand All @@ -10,7 +10,7 @@
- [x] ~~spring-boot-demo-aoplog(使用 AOP 拦截请求日志信息)~~
- [x] ~~spring-boot-demo-exceptionhandler(统一异常处理)~~
- [x] ~~spring-boot-demo-template-freemarker(使用模板引擎 - Freemarker)~~
- [ ] spring-boot-demo-template-thymeleaf(使用模板引擎 - thymeleaf)
- [x] ~~spring-boot-demo-template-thymeleaf(使用模板引擎 - thymeleaf)~~
- [ ] spring-boot-demo-template-beetl(使用模板引擎 - beetl)
- [ ] spring-boot-demo-template-enjoy(使用模板引擎 - JFinal-Enjoy)
- [ ] spring-boot-demo-orm-jdbctemplate(操作 SQL 关系型数据库 - JdbcTemplate)
Expand Down
1 change: 1 addition & 0 deletions spring-boot-demo-parent/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
<module>../spring-boot-demo-aoplog</module>
<module>../spring-boot-demo-exceptionhandler</module>
<module>../spring-boot-demo-template-freemarker</module>
<module>../spring-boot-demo-template-thymeleaf</module>
<module>../spring-boot-demo-orm-jpa</module>
<module>../spring-boot-demo-orm-mybatis</module>
<module>../spring-boot-demo-cache-redis</module>
Expand Down
100 changes: 100 additions & 0 deletions spring-boot-demo-template-thymeleaf/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
# spring-boot-demo-template-thymeleaf
依赖 [spring-boot-demo-parent](../spring-boot-demo-parent)`spring-boot-starter-thymeleaf`

### pom.xml

```xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<artifactId>spring-boot-demo-template-thymeleaf</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>spring-boot-demo-template-thymeleaf</name>
<description>Demo project for Spring Boot</description>

<parent>
<groupId>com.xkcoding</groupId>
<artifactId>spring-boot-demo-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../spring-boot-demo-parent/pom.xml</relativePath>
</parent>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
</dependencies>

<build>
<finalName>spring-boot-demo-template-thymeleaf</finalName>
</build>

</project>
```

### application.yml

```yaml
server:
port: 8080
context-path: /demo
spring:
thymeleaf:
mode: HTML5
encoding: UTF-8
content-type: text/html
cache: false
```
### SpringBootDemoTemplateThymeleafApplication.java
```java
@SpringBootApplication
@Controller
public class SpringBootDemoTemplateThymeleafApplication {

public static void main(String[] args) {
SpringApplication.run(SpringBootDemoTemplateThymeleafApplication.class, args);
}

@GetMapping({"", "/"})
public ModelAndView index(@RequestParam(defaultValue = "thymeleaf") String name) {
ModelAndView mv = new ModelAndView();
mv.addObject("name", name);
mv.setViewName("index");
return mv;
}
}
```

### index.html

```html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8"/>
<title>spring-boot-demo-template-thymeleaf</title>
</head>
<body>
<span th:text="'Hello,'+${name}+'!'"></span>
</body>
</html>
```

### 测试

浏览器分别输入:

http://localhost:8080/demo → Hello,thymeleaf!

http://localhost:8080/demo?name=xkcoding → Hello,xkcoding!

### Thymeleaf 语法

待补充。。。
31 changes: 31 additions & 0 deletions spring-boot-demo-template-thymeleaf/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<artifactId>spring-boot-demo-template-thymeleaf</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>spring-boot-demo-template-thymeleaf</name>
<description>Demo project for Spring Boot</description>

<parent>
<groupId>com.xkcoding</groupId>
<artifactId>spring-boot-demo-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../spring-boot-demo-parent/pom.xml</relativePath>
</parent>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
</dependencies>

<build>
<finalName>spring-boot-demo-template-thymeleaf</finalName>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.xkcoding.springbootdemotemplatethymeleaf;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

@SpringBootApplication
@Controller
public class SpringBootDemoTemplateThymeleafApplication {

public static void main(String[] args) {
SpringApplication.run(SpringBootDemoTemplateThymeleafApplication.class, args);
}

@GetMapping({"", "/"})
public ModelAndView index(@RequestParam(defaultValue = "thymeleaf") String name) {
ModelAndView mv = new ModelAndView();
mv.addObject("name", name);
mv.setViewName("index");
return mv;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
server:
port: 8080
context-path: /demo
spring:
thymeleaf:
mode: HTML5
encoding: UTF-8
content-type: text/html
cache: false
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8"/>
<title>spring-boot-demo-template-thymeleaf</title>
</head>
<body>
<span th:text="'Hello,'+${name}+'!'"></span>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.xkcoding.springbootdemotemplatethymeleaf;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringBootDemoTemplateThymeleafApplicationTests {

@Test
public void contextLoads() {
}

}

0 comments on commit f83ee1e

Please sign in to comment.