Skip to content

Commit

Permalink
spring boot 集成 freemarker 模板引擎
Browse files Browse the repository at this point in the history
  • Loading branch information
xkcoding committed Nov 26, 2017
1 parent 061d0a2 commit 92d000e
Show file tree
Hide file tree
Showing 14 changed files with 325 additions and 9 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 级别和页面级别)、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 版本:

Expand Down Expand Up @@ -52,6 +52,7 @@ spring boot demo 是一个用来学习 spring boot 的项目,已经集成 actu
<module>../spring-boot-demo-logback</module>
<module>../spring-boot-demo-aoplog</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-mybatis</module>
<module>../spring-boot-demo-cache-redis</module>
Expand Down Expand Up @@ -150,6 +151,7 @@ spring boot demo 是一个用来学习 spring boot 的项目,已经集成 actu
| [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-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-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
14 changes: 7 additions & 7 deletions TODO.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# spring-boot-demo 项目

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

- [x] ~~spring-boot-demo-helloworld(Helloworld 示例)~~
- [x] ~~spring-boot-demo-properties(读取配置文件信息)~~
Expand All @@ -9,16 +9,16 @@
- [x] ~~spring-boot-demo-logback(集成 logback 日志)~~
- [x] ~~spring-boot-demo-aoplog(使用 AOP 拦截请求日志信息)~~
- [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-mybatis(操作 SQL 关系型数据库 - mybatis)~~
- [ ] spring-boot-demo-orm-beetlSQL(操作 SQL 关系型数据库 - beetlSQL)
- [ ] spring-boot-demo-orm-beetlsql(操作 SQL 关系型数据库 - beetlSQL)
- [x] ~~spring-boot-demo-cache-redis(使用 redis 进行缓存)~~
- [ ] 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-task-schedule(定时任务 - Task 实现)
- [ ] spring-boot-demo-task-quartz(定时任务 - Quartz 实现)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ mybatis:
mapper:
not-empty: false
identity: MYSQL
mappers: com.xkcoding.springbootdemoormmybatistis.util.MyMapper
mappers: com.xkcoding.springbootdemoormmybatis.util.MyMapper
# PageHelper 配置
pagehelper:
helper-dialect: mysql
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 @@ -19,6 +19,7 @@
<module>../spring-boot-demo-logback</module>
<module>../spring-boot-demo-aoplog</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-mybatis</module>
<module>../spring-boot-demo-cache-redis</module>
Expand Down
137 changes: 137 additions & 0 deletions spring-boot-demo-template-freemarker/README.md
Original file line number Diff line number Diff line change
@@ -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 changes: 31 additions & 0 deletions spring-boot-demo-template-freemarker/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-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>
Original file line number Diff line number Diff line change
@@ -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");
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
server:
port: 8080
context-path: /demo
Original file line number Diff line number Diff line change
@@ -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>
Original file line number Diff line number Diff line change
@@ -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>
Original file line number Diff line number Diff line change
@@ -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>
Original file line number Diff line number Diff line change
@@ -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() {
}

}

0 comments on commit 92d000e

Please sign in to comment.