Browse Source

spring boot 集成 freemarker 模板引擎

v-1.5.x
yangkai.shen 7 years ago
parent
commit
92d000eed0
14 changed files with 325 additions and 9 deletions
  1. +3
    -1
      README.md
  2. +7
    -7
      TODO.md
  3. +1
    -1
      spring-boot-demo-orm-mybatis/src/main/resources/application.yml
  4. +1
    -0
      spring-boot-demo-parent/pom.xml
  5. +137
    -0
      spring-boot-demo-template-freemarker/README.md
  6. +31
    -0
      spring-boot-demo-template-freemarker/pom.xml
  7. +21
    -0
      spring-boot-demo-template-freemarker/src/main/java/com/xkcoding/springbootdemotemplatefreemarker/SpringBootDemoTemplateFreemarkerApplication.java
  8. +53
    -0
      spring-boot-demo-template-freemarker/src/main/java/com/xkcoding/springbootdemotemplatefreemarker/controller/UserController.java
  9. +22
    -0
      spring-boot-demo-template-freemarker/src/main/java/com/xkcoding/springbootdemotemplatefreemarker/domain/User.java
  10. +3
    -0
      spring-boot-demo-template-freemarker/src/main/resources/application.yml
  11. +7
    -0
      spring-boot-demo-template-freemarker/src/main/resources/templates/common/header.ftl
  12. +9
    -0
      spring-boot-demo-template-freemarker/src/main/resources/templates/home.ftl
  13. +14
    -0
      spring-boot-demo-template-freemarker/src/main/resources/templates/index.ftl
  14. +16
    -0
      spring-boot-demo-template-freemarker/src/test/java/com/xkcoding/springbootdemotemplatefreemarker/SpringBootDemoTemplateFreemarkerApplicationTests.java

+ 3
- 1
README.md View File

@@ -1,6 +1,6 @@
# Spring Boot Demo # Spring Boot Demo


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


@@ -52,6 +52,7 @@ spring boot demo 是一个用来学习 spring boot 的项目,已经集成 actu
<module>../spring-boot-demo-logback</module> <module>../spring-boot-demo-logback</module>
<module>../spring-boot-demo-aoplog</module> <module>../spring-boot-demo-aoplog</module>
<module>../spring-boot-demo-exceptionhandler</module> <module>../spring-boot-demo-exceptionhandler</module>
<module>../spring-boot-demo-template-freemarker</module>
<module>../spring-boot-demo-orm-jpa</module> <module>../spring-boot-demo-orm-jpa</module>
<module>../spring-boot-demo-orm-mybatis</module> <module>../spring-boot-demo-orm-mybatis</module>
<module>../spring-boot-demo-cache-redis</module> <module>../spring-boot-demo-cache-redis</module>
@@ -150,6 +151,7 @@ spring boot demo 是一个用来学习 spring boot 的项目,已经集成 actu
| [spring-boot-demo-logback](./spring-boot-demo-logback) | spring-boot 集成 logback 日志 | | [spring-boot-demo-logback](./spring-boot-demo-logback) | spring-boot 集成 logback 日志 |
| [spring-boot-demo-aoplog](./spring-boot-demo-aoplog) | spring-boot 使用 AOP 切面的方式记录 web 请求日志 | | [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-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-orm-jpa](./spring-boot-demo-orm-jpa) | spring-boot 集成 spring-boot-starter-data-jpa 操作数据库 | | [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-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 做缓存 | | [spring-boot-demo-cache-redis](./spring-boot-demo-cache-redis) | spring-boot 使用 Redis 做缓存 |


+ 7
- 7
TODO.md View File

@@ -1,6 +1,6 @@
# spring-boot-demo 项目 # spring-boot-demo 项目


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


- [x] ~~spring-boot-demo-helloworld(Helloworld 示例)~~ - [x] ~~spring-boot-demo-helloworld(Helloworld 示例)~~
- [x] ~~spring-boot-demo-properties(读取配置文件信息)~~ - [x] ~~spring-boot-demo-properties(读取配置文件信息)~~
@@ -9,16 +9,16 @@
- [x] ~~spring-boot-demo-logback(集成 logback 日志)~~ - [x] ~~spring-boot-demo-logback(集成 logback 日志)~~
- [x] ~~spring-boot-demo-aoplog(使用 AOP 拦截请求日志信息)~~ - [x] ~~spring-boot-demo-aoplog(使用 AOP 拦截请求日志信息)~~
- [x] ~~spring-boot-demo-exceptionhandler(统一异常处理)~~ - [x] ~~spring-boot-demo-exceptionhandler(统一异常处理)~~
- [ ] spring-boot-demo-orm-jdbcTemplate(操作 SQL 关系型数据库 - JdbcTemplate)
- [x] ~~spring-boot-demo-template-freemarker(使用模板引擎 - Freemarker)~~
- [ ] 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)
- [x] ~~spring-boot-demo-orm-jpa(操作 SQL 关系型数据库 - JPA)~~ - [x] ~~spring-boot-demo-orm-jpa(操作 SQL 关系型数据库 - JPA)~~
- [x] ~~spring-boot-demo-orm-mybatis(操作 SQL 关系型数据库 - mybatis)~~ - [x] ~~spring-boot-demo-orm-mybatis(操作 SQL 关系型数据库 - mybatis)~~
- [ ] spring-boot-demo-orm-beetlSQL(操作 SQL 关系型数据库 - beetlSQL)
- [ ] spring-boot-demo-orm-beetlsql(操作 SQL 关系型数据库 - beetlSQL)
- [x] ~~spring-boot-demo-cache-redis(使用 redis 进行缓存)~~ - [x] ~~spring-boot-demo-cache-redis(使用 redis 进行缓存)~~
- [ ] spring-boot-demo-cache-ehcache(使用 Ehcache 进行缓存) - [ ] spring-boot-demo-cache-ehcache(使用 Ehcache 进行缓存)
- [ ] spring-boot-demo-template-freemarker(使用模板引擎 - Freemarker)
- [ ] spring-boot-demo-template-thymeleaf(使用模板引擎 - thymeleaf)
- [ ] spring-boot-demo-template-beetl(使用模板引擎 - beetl)
- [ ] spring-boot-demo-template-enjoy(使用模板引擎 - JFinal-Enjoy)
- [ ] spring-boot-demo-email(集成邮件服务) - [ ] spring-boot-demo-email(集成邮件服务)
- [ ] spring-boot-demo-task-schedule(定时任务 - Task 实现) - [ ] spring-boot-demo-task-schedule(定时任务 - Task 实现)
- [ ] spring-boot-demo-task-quartz(定时任务 - Quartz 实现) - [ ] spring-boot-demo-task-quartz(定时任务 - Quartz 实现)


+ 1
- 1
spring-boot-demo-orm-mybatis/src/main/resources/application.yml View File

@@ -61,7 +61,7 @@ mybatis:
mapper: mapper:
not-empty: false not-empty: false
identity: MYSQL identity: MYSQL
mappers: com.xkcoding.springbootdemoormmybatistis.util.MyMapper
mappers: com.xkcoding.springbootdemoormmybatis.util.MyMapper
# PageHelper 配置 # PageHelper 配置
pagehelper: pagehelper:
helper-dialect: mysql helper-dialect: mysql


+ 1
- 0
spring-boot-demo-parent/pom.xml View File

@@ -19,6 +19,7 @@
<module>../spring-boot-demo-logback</module> <module>../spring-boot-demo-logback</module>
<module>../spring-boot-demo-aoplog</module> <module>../spring-boot-demo-aoplog</module>
<module>../spring-boot-demo-exceptionhandler</module> <module>../spring-boot-demo-exceptionhandler</module>
<module>../spring-boot-demo-template-freemarker</module>
<module>../spring-boot-demo-orm-jpa</module> <module>../spring-boot-demo-orm-jpa</module>
<module>../spring-boot-demo-orm-mybatis</module> <module>../spring-boot-demo-orm-mybatis</module>
<module>../spring-boot-demo-cache-redis</module> <module>../spring-boot-demo-cache-redis</module>


+ 137
- 0
spring-boot-demo-template-freemarker/README.md View File

@@ -0,0 +1,137 @@
# spring-boot-demo-template-freemarker

依赖 [spring-boot-demo-parent](../spring-boot-demo-parent) 、`spring-boot-starter-freemarker`

### 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-freemarker</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>

<name>spring-boot-demo-template-freemarker</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-freemarker</artifactId>
</dependency>
</dependencies>

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

</project>
```

### SpringBootDemoTemplateFreemarkerApplication.java

```java
@Controller
@SpringBootApplication
public class SpringBootDemoTemplateFreemarkerApplication {

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

@GetMapping({"","/"})
public ModelAndView home(){
return new ModelAndView("index");
}
}
```

### UserController.java

```java
/**
* 用户 Controller
*
* @package: com.xkcoding.springbootdemotemplatefreemarker.controller
* @description: 用户 Controller
* @author: yangkai.shen
* @date: Created in 2017/11/26 下午6:47
* @copyright: Copyright (c) 2017
* @version: 0.0.1
* @modified: yangkai.shen
*/
@Controller
@RequestMapping("/user")
@Slf4j
public class UserController {

@PostMapping("/login")
public ModelAndView login(User user, HttpServletRequest request) {
ModelAndView mv = new ModelAndView();

mv.addObject(user);
mv.setViewName("home");

request.getSession().setAttribute("user", user);
return mv;
}

@GetMapping("/login")
public ModelAndView login(HttpServletRequest request) {
ModelAndView mv = new ModelAndView();

User user = (User) request.getSession().getAttribute("user");
if (user == null) {
mv.setViewName("redirect:/");
} else {
mv.setViewName("home");
}

return mv;
}
}
```

### home.ftl

```html
<!doctype html>
<html lang="en">
<#include "./common/header.ftl">
<body>
<div id="app" style="margin: 20px 20%">
欢迎登录,${user.name}!
</div>
</body>
</html>
```

### index.ftl

```html
<!doctype html>
<html lang="en">
<#include "./common/header.ftl">
<body>
<div id="app" style="margin: 20px 20%">
<form action="/demo/user/login" method="post">
用户名<input type="text" name="name" placeholder="用户名"/>
密码<input type="password" name="password" placeholder="密码"/>
<input type="submit" value="登录">
</form>
</div>

</body>
</html>
```


+ 31
- 0
spring-boot-demo-template-freemarker/pom.xml View File

@@ -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-freemarker</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>

<name>spring-boot-demo-template-freemarker</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-freemarker</artifactId>
</dependency>
</dependencies>

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

</project>

+ 21
- 0
spring-boot-demo-template-freemarker/src/main/java/com/xkcoding/springbootdemotemplatefreemarker/SpringBootDemoTemplateFreemarkerApplication.java View File

@@ -0,0 +1,21 @@
package com.xkcoding.springbootdemotemplatefreemarker;

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.servlet.ModelAndView;

@Controller
@SpringBootApplication
public class SpringBootDemoTemplateFreemarkerApplication {

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

@GetMapping({"","/"})
public ModelAndView home(){
return new ModelAndView("index");
}
}

+ 53
- 0
spring-boot-demo-template-freemarker/src/main/java/com/xkcoding/springbootdemotemplatefreemarker/controller/UserController.java View File

@@ -0,0 +1,53 @@
package com.xkcoding.springbootdemotemplatefreemarker.controller;

import com.xkcoding.springbootdemotemplatefreemarker.domain.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

/**
* 用户 Controller
*
* @package: com.xkcoding.springbootdemotemplatefreemarker.controller
* @description: 用户 Controller
* @author: yangkai.shen
* @date: Created in 2017/11/26 下午6:47
* @copyright: Copyright (c) 2017
* @version: 0.0.1
* @modified: yangkai.shen
*/
@Controller
@RequestMapping("/user")
@Slf4j
public class UserController {

@PostMapping("/login")
public ModelAndView login(User user, HttpServletRequest request) {
ModelAndView mv = new ModelAndView();

mv.addObject(user);
mv.setViewName("home");

request.getSession().setAttribute("user", user);
return mv;
}

@GetMapping("/login")
public ModelAndView login(HttpServletRequest request) {
ModelAndView mv = new ModelAndView();

User user = (User) request.getSession().getAttribute("user");
if (user == null) {
mv.setViewName("redirect:/");
} else {
mv.setViewName("home");
}

return mv;
}
}

+ 22
- 0
spring-boot-demo-template-freemarker/src/main/java/com/xkcoding/springbootdemotemplatefreemarker/domain/User.java View File

@@ -0,0 +1,22 @@
package com.xkcoding.springbootdemotemplatefreemarker.domain;

import lombok.Data;

import java.io.Serializable;

/**
* 用户实体类
*
* @package: com.xkcoding.springbootdemotemplatefreemarker
* @description: 用户实体类
* @author: yangkai.shen
* @date: Created in 2017/11/26 下午6:50
* @copyright: Copyright (c) 2017
* @version: 0.0.1
* @modified: yangkai.shen
*/
@Data
public class User implements Serializable {
private String name;
private String password;
}

+ 3
- 0
spring-boot-demo-template-freemarker/src/main/resources/application.yml View File

@@ -0,0 +1,3 @@
server:
port: 8080
context-path: /demo

+ 7
- 0
spring-boot-demo-template-freemarker/src/main/resources/templates/common/header.ftl View File

@@ -0,0 +1,7 @@
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>spring-boot-demo-template-thymeleaf</title>
</head>

+ 9
- 0
spring-boot-demo-template-freemarker/src/main/resources/templates/home.ftl View File

@@ -0,0 +1,9 @@
<!doctype html>
<html lang="en">
<#include "./common/header.ftl">
<body>
<div id="app" style="margin: 20px 20%">
欢迎登录,${user.name}!
</div>
</body>
</html>

+ 14
- 0
spring-boot-demo-template-freemarker/src/main/resources/templates/index.ftl View File

@@ -0,0 +1,14 @@
<!doctype html>
<html lang="en">
<#include "./common/header.ftl">
<body>
<div id="app" style="margin: 20px 20%">
<form action="/demo/user/login" method="post">
用户名<input type="text" name="name" placeholder="用户名"/>
密码<input type="password" name="password" placeholder="密码"/>
<input type="submit" value="登录">
</form>
</div>

</body>
</html>

+ 16
- 0
spring-boot-demo-template-freemarker/src/test/java/com/xkcoding/springbootdemotemplatefreemarker/SpringBootDemoTemplateFreemarkerApplicationTests.java View File

@@ -0,0 +1,16 @@
package com.xkcoding.springbootdemotemplatefreemarker;

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 SpringBootDemoTemplateFreemarkerApplicationTests {

@Test
public void contextLoads() {
}

}

Loading…
Cancel
Save