diff --git a/README.md b/README.md index 30e3a0904..18e918abf 100644 --- a/README.md +++ b/README.md @@ -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 版本: @@ -52,6 +52,7 @@ spring boot demo 是一个用来学习 spring boot 的项目,已经集成 actu ../spring-boot-demo-logback ../spring-boot-demo-aoplog ../spring-boot-demo-exceptionhandler + ../spring-boot-demo-template-freemarker ../spring-boot-demo-orm-jpa ../spring-boot-demo-orm-mybatis ../spring-boot-demo-cache-redis @@ -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 做缓存 | diff --git a/TODO.md b/TODO.md index 9d5aac416..c3a893303 100644 --- a/TODO.md +++ b/TODO.md @@ -1,6 +1,6 @@ # spring-boot-demo 项目 -## 模块计划(已完成:12 / 35) +## 模块计划(已完成:13 / 35) - [x] ~~spring-boot-demo-helloworld(Helloworld 示例)~~ - [x] ~~spring-boot-demo-properties(读取配置文件信息)~~ @@ -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 实现) diff --git a/spring-boot-demo-orm-mybatis/src/main/resources/application.yml b/spring-boot-demo-orm-mybatis/src/main/resources/application.yml index feca12a4f..2ace76ac2 100644 --- a/spring-boot-demo-orm-mybatis/src/main/resources/application.yml +++ b/spring-boot-demo-orm-mybatis/src/main/resources/application.yml @@ -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 diff --git a/spring-boot-demo-parent/pom.xml b/spring-boot-demo-parent/pom.xml index 4e11ae4b8..71becd183 100644 --- a/spring-boot-demo-parent/pom.xml +++ b/spring-boot-demo-parent/pom.xml @@ -19,6 +19,7 @@ ../spring-boot-demo-logback ../spring-boot-demo-aoplog ../spring-boot-demo-exceptionhandler + ../spring-boot-demo-template-freemarker ../spring-boot-demo-orm-jpa ../spring-boot-demo-orm-mybatis ../spring-boot-demo-cache-redis diff --git a/spring-boot-demo-template-freemarker/README.md b/spring-boot-demo-template-freemarker/README.md new file mode 100644 index 000000000..2df68fe2b --- /dev/null +++ b/spring-boot-demo-template-freemarker/README.md @@ -0,0 +1,137 @@ +# spring-boot-demo-template-freemarker + +依赖 [spring-boot-demo-parent](../spring-boot-demo-parent) 、`spring-boot-starter-freemarker` + +### pom.xml + +```xml + + + 4.0.0 + + spring-boot-demo-template-freemarker + 0.0.1-SNAPSHOT + war + + spring-boot-demo-template-freemarker + Demo project for Spring Boot + + + com.xkcoding + spring-boot-demo-parent + 0.0.1-SNAPSHOT + ../spring-boot-demo-parent/pom.xml + + + + + org.springframework.boot + spring-boot-starter-freemarker + + + + + spring-boot-demo-template-freemarker + + + +``` + +### 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 + + +<#include "./common/header.ftl"> + +
+ 欢迎登录,${user.name}! +
+ + +``` + +### index.ftl + +```html + + +<#include "./common/header.ftl"> + +
+
+ 用户名 + 密码 + +
+
+ + + +``` + diff --git a/spring-boot-demo-template-freemarker/pom.xml b/spring-boot-demo-template-freemarker/pom.xml new file mode 100644 index 000000000..8a6701381 --- /dev/null +++ b/spring-boot-demo-template-freemarker/pom.xml @@ -0,0 +1,31 @@ + + + 4.0.0 + + spring-boot-demo-template-freemarker + 0.0.1-SNAPSHOT + war + + spring-boot-demo-template-freemarker + Demo project for Spring Boot + + + com.xkcoding + spring-boot-demo-parent + 0.0.1-SNAPSHOT + ../spring-boot-demo-parent/pom.xml + + + + + org.springframework.boot + spring-boot-starter-freemarker + + + + + spring-boot-demo-template-freemarker + + + \ No newline at end of file diff --git a/spring-boot-demo-template-freemarker/src/main/java/com/xkcoding/springbootdemotemplatefreemarker/SpringBootDemoTemplateFreemarkerApplication.java b/spring-boot-demo-template-freemarker/src/main/java/com/xkcoding/springbootdemotemplatefreemarker/SpringBootDemoTemplateFreemarkerApplication.java new file mode 100644 index 000000000..4cab05bc9 --- /dev/null +++ b/spring-boot-demo-template-freemarker/src/main/java/com/xkcoding/springbootdemotemplatefreemarker/SpringBootDemoTemplateFreemarkerApplication.java @@ -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"); + } +} diff --git a/spring-boot-demo-template-freemarker/src/main/java/com/xkcoding/springbootdemotemplatefreemarker/controller/UserController.java b/spring-boot-demo-template-freemarker/src/main/java/com/xkcoding/springbootdemotemplatefreemarker/controller/UserController.java new file mode 100644 index 000000000..46cafa620 --- /dev/null +++ b/spring-boot-demo-template-freemarker/src/main/java/com/xkcoding/springbootdemotemplatefreemarker/controller/UserController.java @@ -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; + } +} diff --git a/spring-boot-demo-template-freemarker/src/main/java/com/xkcoding/springbootdemotemplatefreemarker/domain/User.java b/spring-boot-demo-template-freemarker/src/main/java/com/xkcoding/springbootdemotemplatefreemarker/domain/User.java new file mode 100644 index 000000000..8480d96ef --- /dev/null +++ b/spring-boot-demo-template-freemarker/src/main/java/com/xkcoding/springbootdemotemplatefreemarker/domain/User.java @@ -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; +} diff --git a/spring-boot-demo-template-freemarker/src/main/resources/application.yml b/spring-boot-demo-template-freemarker/src/main/resources/application.yml new file mode 100644 index 000000000..3cae10b05 --- /dev/null +++ b/spring-boot-demo-template-freemarker/src/main/resources/application.yml @@ -0,0 +1,3 @@ +server: + port: 8080 + context-path: /demo \ No newline at end of file diff --git a/spring-boot-demo-template-freemarker/src/main/resources/templates/common/header.ftl b/spring-boot-demo-template-freemarker/src/main/resources/templates/common/header.ftl new file mode 100644 index 000000000..9a9b873f5 --- /dev/null +++ b/spring-boot-demo-template-freemarker/src/main/resources/templates/common/header.ftl @@ -0,0 +1,7 @@ + + + + + spring-boot-demo-template-thymeleaf + \ No newline at end of file diff --git a/spring-boot-demo-template-freemarker/src/main/resources/templates/home.ftl b/spring-boot-demo-template-freemarker/src/main/resources/templates/home.ftl new file mode 100644 index 000000000..47f203383 --- /dev/null +++ b/spring-boot-demo-template-freemarker/src/main/resources/templates/home.ftl @@ -0,0 +1,9 @@ + + +<#include "./common/header.ftl"> + +
+ 欢迎登录,${user.name}! +
+ + \ No newline at end of file diff --git a/spring-boot-demo-template-freemarker/src/main/resources/templates/index.ftl b/spring-boot-demo-template-freemarker/src/main/resources/templates/index.ftl new file mode 100644 index 000000000..8dc90bd08 --- /dev/null +++ b/spring-boot-demo-template-freemarker/src/main/resources/templates/index.ftl @@ -0,0 +1,14 @@ + + +<#include "./common/header.ftl"> + +
+
+ 用户名 + 密码 + +
+
+ + + \ No newline at end of file diff --git a/spring-boot-demo-template-freemarker/src/test/java/com/xkcoding/springbootdemotemplatefreemarker/SpringBootDemoTemplateFreemarkerApplicationTests.java b/spring-boot-demo-template-freemarker/src/test/java/com/xkcoding/springbootdemotemplatefreemarker/SpringBootDemoTemplateFreemarkerApplicationTests.java new file mode 100644 index 000000000..3fbb5af8f --- /dev/null +++ b/spring-boot-demo-template-freemarker/src/test/java/com/xkcoding/springbootdemotemplatefreemarker/SpringBootDemoTemplateFreemarkerApplicationTests.java @@ -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() { + } + +}