From e454d1828ab968057d85f24f0e9a47348a7a40c6 Mon Sep 17 00:00:00 2001 From: "Yangkai.Shen" <237497819@qq.com> Date: Thu, 11 Oct 2018 15:02:18 +0800 Subject: [PATCH] =?UTF-8?q?:sparkles:=20spring-boot-demo-template-enjoy=20?= =?UTF-8?q?=E5=AE=8C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- spring-boot-demo-template-enjoy/.gitignore | 25 ++ spring-boot-demo-template-enjoy/README.md | 235 ++++++++++++++++++ spring-boot-demo-template-enjoy/pom.xml | 66 +++++ ...pringBootDemoTemplateEnjoyApplication.java | 25 ++ .../template/enjoy/config/EnjoyConfig.java | 40 +++ .../enjoy/controller/IndexController.java | 43 ++++ .../enjoy/controller/UserController.java | 45 ++++ .../xkcoding/template/enjoy/model/User.java | 22 ++ .../src/main/resources/application.yml | 4 + .../main/resources/templates/common/head.html | 8 + .../main/resources/templates/page/index.html | 9 + .../main/resources/templates/page/login.html | 13 + ...BootDemoTemplateEnjoyApplicationTests.java | 16 ++ 13 files changed, 551 insertions(+) create mode 100644 spring-boot-demo-template-enjoy/.gitignore create mode 100644 spring-boot-demo-template-enjoy/README.md create mode 100644 spring-boot-demo-template-enjoy/pom.xml create mode 100644 spring-boot-demo-template-enjoy/src/main/java/com/xkcoding/template/enjoy/SpringBootDemoTemplateEnjoyApplication.java create mode 100644 spring-boot-demo-template-enjoy/src/main/java/com/xkcoding/template/enjoy/config/EnjoyConfig.java create mode 100644 spring-boot-demo-template-enjoy/src/main/java/com/xkcoding/template/enjoy/controller/IndexController.java create mode 100644 spring-boot-demo-template-enjoy/src/main/java/com/xkcoding/template/enjoy/controller/UserController.java create mode 100644 spring-boot-demo-template-enjoy/src/main/java/com/xkcoding/template/enjoy/model/User.java create mode 100644 spring-boot-demo-template-enjoy/src/main/resources/application.yml create mode 100644 spring-boot-demo-template-enjoy/src/main/resources/templates/common/head.html create mode 100644 spring-boot-demo-template-enjoy/src/main/resources/templates/page/index.html create mode 100644 spring-boot-demo-template-enjoy/src/main/resources/templates/page/login.html create mode 100644 spring-boot-demo-template-enjoy/src/test/java/com/xkcoding/template/enjoy/SpringBootDemoTemplateEnjoyApplicationTests.java diff --git a/spring-boot-demo-template-enjoy/.gitignore b/spring-boot-demo-template-enjoy/.gitignore new file mode 100644 index 0000000..82eca33 --- /dev/null +++ b/spring-boot-demo-template-enjoy/.gitignore @@ -0,0 +1,25 @@ +/target/ +!.mvn/wrapper/maven-wrapper.jar + +### STS ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr + +### NetBeans ### +/nbproject/private/ +/build/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ \ No newline at end of file diff --git a/spring-boot-demo-template-enjoy/README.md b/spring-boot-demo-template-enjoy/README.md new file mode 100644 index 0000000..e9bae91 --- /dev/null +++ b/spring-boot-demo-template-enjoy/README.md @@ -0,0 +1,235 @@ +# spring-boot-demo-template-enjoy + +> 本 demo 主要演示了 Spring Boot 项目如何集成 enjoy 模板引擎。 + +## pom.xml + +```xml + + + 4.0.0 + + spring-boot-demo-template-beetl + 0.0.1-SNAPSHOT + jar + + spring-boot-demo-template-beetl + Demo project for Spring Boot + + + com.xkcoding + spring-boot-demo + 0.0.1-SNAPSHOT + + + + UTF-8 + UTF-8 + 1.8 + 1.1.63.RELEASE + + + + + com.ibeetl + beetl-framework-starter + ${ibeetl.version} + + + + org.springframework.boot + spring-boot-starter-web + + + + org.springframework.boot + spring-boot-starter-test + test + + + + org.projectlombok + lombok + true + + + + cn.hutool + hutool-all + + + + + spring-boot-demo-template-beetl + + + org.springframework.boot + spring-boot-maven-plugin + + + + + +``` + +## EnjoyConfig.java + +```java +/** + *

+ * Enjoy 模板配置类 + *

+ * + * @package: com.xkcoding.template.enjoy.config + * @description: Enjoy 模板配置类 + * @author: yangkai.shen + * @date: Created in 2018/10/11 2:06 PM + * @copyright: Copyright (c) 2018 + * @version: V1.0 + * @modified: yangkai.shen + */ +@Configuration +public class EnjoyConfig { + @Bean(name = "jfinalViewResolver") + public JFinalViewResolver getJFinalViewResolver() { + JFinalViewResolver jfr = new JFinalViewResolver(); + // setDevMode 配置放在最前面 + jfr.setDevMode(true); + // 使用 ClassPathSourceFactory 从 class path 与 jar 包中加载模板文件 + jfr.setSourceFactory(new ClassPathSourceFactory()); + // 在使用 ClassPathSourceFactory 时要使用 setBaseTemplatePath + // 代替 jfr.setPrefix("/view/") + JFinalViewResolver.engine.setBaseTemplatePath("/templates/"); + + jfr.setSessionInView(true); + jfr.setSuffix(".html"); + jfr.setContentType("text/html;charset=UTF-8"); + jfr.setOrder(0); + return jfr; + } +} +``` + +## IndexController.java + +```java +/** + *

+ * 主页 + *

+ * + * @package: com.xkcoding.template.enjoy.controller + * @description: 主页 + * @author: yangkai.shen + * @date: Created in 2018/10/11 2:22 PM + * @copyright: Copyright (c) 2018 + * @version: V1.0 + * @modified: yangkai.shen + */ +@Controller +@Slf4j +public class IndexController { + + @GetMapping(value = {"", "/"}) + public ModelAndView index(HttpServletRequest request) { + ModelAndView mv = new ModelAndView(); + + User user = (User) request.getSession().getAttribute("user"); + if (ObjectUtil.isNull(user)) { + mv.setViewName("redirect:/user/login"); + } else { + mv.setViewName("page/index"); + mv.addObject(user); + } + + return mv; + } +} +``` + +## UserController.java + +```java +/** + *

+ * 用户页面 + *

+ * + * @package: com.xkcoding.template.enjoy.controller + * @description: 用户页面 + * @author: yangkai.shen + * @date: Created in 2018/10/11 2:24 PM + * @copyright: Copyright (c) 2018 + * @version: V1.0 + * @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("redirect:/"); + + request.getSession().setAttribute("user", user); + return mv; + } + + @GetMapping("/login") + public ModelAndView login() { + return new ModelAndView("page/login"); + } +} +``` + +## index.html + +```jsp + + +#include("/common/head.html") + +
+ 欢迎登录,#(user.name)! +
+ + +``` + +## login.html + +```jsp + + +#include("/common/head.html") + +
+
+ 用户名 + 密码 + +
+
+ + +``` + +## application.yml + +```yaml +server: + port: 8080 + servlet: + context-path: /demo +``` + +## Enjoy 语法糖学习文档 + +http://www.jfinal.com/doc/6-1 + + + diff --git a/spring-boot-demo-template-enjoy/pom.xml b/spring-boot-demo-template-enjoy/pom.xml new file mode 100644 index 0000000..789f595 --- /dev/null +++ b/spring-boot-demo-template-enjoy/pom.xml @@ -0,0 +1,66 @@ + + + 4.0.0 + + spring-boot-demo-template-enjoy + 0.0.1-SNAPSHOT + jar + + spring-boot-demo-template-enjoy + Demo project for Spring Boot + + + com.xkcoding + spring-boot-demo + 0.0.1-SNAPSHOT + + + + UTF-8 + UTF-8 + 1.8 + 3.5 + + + + + org.springframework.boot + spring-boot-starter-web + + + + org.springframework.boot + spring-boot-starter-test + test + + + + com.jfinal + enjoy + ${enjoy.version} + + + + org.projectlombok + lombok + true + + + + cn.hutool + hutool-all + + + + + spring-boot-demo-template-enjoy + + + org.springframework.boot + spring-boot-maven-plugin + + + + + diff --git a/spring-boot-demo-template-enjoy/src/main/java/com/xkcoding/template/enjoy/SpringBootDemoTemplateEnjoyApplication.java b/spring-boot-demo-template-enjoy/src/main/java/com/xkcoding/template/enjoy/SpringBootDemoTemplateEnjoyApplication.java new file mode 100644 index 0000000..46314b9 --- /dev/null +++ b/spring-boot-demo-template-enjoy/src/main/java/com/xkcoding/template/enjoy/SpringBootDemoTemplateEnjoyApplication.java @@ -0,0 +1,25 @@ +package com.xkcoding.template.enjoy; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +/** + *

+ * 启动类 + *

+ * + * @package: com.xkcoding.template.enjoy + * @description: 启动类 + * @author: yangkai.shen + * @date: Created in 2018/10/11 2:06 PM + * @copyright: Copyright (c) 2018 + * @version: V1.0 + * @modified: yangkai.shen + */ +@SpringBootApplication +public class SpringBootDemoTemplateEnjoyApplication { + + public static void main(String[] args) { + SpringApplication.run(SpringBootDemoTemplateEnjoyApplication.class, args); + } +} diff --git a/spring-boot-demo-template-enjoy/src/main/java/com/xkcoding/template/enjoy/config/EnjoyConfig.java b/spring-boot-demo-template-enjoy/src/main/java/com/xkcoding/template/enjoy/config/EnjoyConfig.java new file mode 100644 index 0000000..246a557 --- /dev/null +++ b/spring-boot-demo-template-enjoy/src/main/java/com/xkcoding/template/enjoy/config/EnjoyConfig.java @@ -0,0 +1,40 @@ +package com.xkcoding.template.enjoy.config; + +import com.jfinal.template.ext.spring.JFinalViewResolver; +import com.jfinal.template.source.ClassPathSourceFactory; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +/** + *

+ * Enjoy 模板配置类 + *

+ * + * @package: com.xkcoding.template.enjoy.config + * @description: Enjoy 模板配置类 + * @author: yangkai.shen + * @date: Created in 2018/10/11 2:06 PM + * @copyright: Copyright (c) 2018 + * @version: V1.0 + * @modified: yangkai.shen + */ +@Configuration +public class EnjoyConfig { + @Bean(name = "jfinalViewResolver") + public JFinalViewResolver getJFinalViewResolver() { + JFinalViewResolver jfr = new JFinalViewResolver(); + // setDevMode 配置放在最前面 + jfr.setDevMode(true); + // 使用 ClassPathSourceFactory 从 class path 与 jar 包中加载模板文件 + jfr.setSourceFactory(new ClassPathSourceFactory()); + // 在使用 ClassPathSourceFactory 时要使用 setBaseTemplatePath + // 代替 jfr.setPrefix("/view/") + JFinalViewResolver.engine.setBaseTemplatePath("/templates/"); + + jfr.setSessionInView(true); + jfr.setSuffix(".html"); + jfr.setContentType("text/html;charset=UTF-8"); + jfr.setOrder(0); + return jfr; + } +} diff --git a/spring-boot-demo-template-enjoy/src/main/java/com/xkcoding/template/enjoy/controller/IndexController.java b/spring-boot-demo-template-enjoy/src/main/java/com/xkcoding/template/enjoy/controller/IndexController.java new file mode 100644 index 0000000..6cc978d --- /dev/null +++ b/spring-boot-demo-template-enjoy/src/main/java/com/xkcoding/template/enjoy/controller/IndexController.java @@ -0,0 +1,43 @@ +package com.xkcoding.template.enjoy.controller; + +import cn.hutool.core.util.ObjectUtil; +import com.xkcoding.template.enjoy.model.User; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.servlet.ModelAndView; + +import javax.servlet.http.HttpServletRequest; + +/** + *

+ * 主页 + *

+ * + * @package: com.xkcoding.template.enjoy.controller + * @description: 主页 + * @author: yangkai.shen + * @date: Created in 2018/10/11 2:22 PM + * @copyright: Copyright (c) 2018 + * @version: V1.0 + * @modified: yangkai.shen + */ +@Controller +@Slf4j +public class IndexController { + + @GetMapping(value = {"", "/"}) + public ModelAndView index(HttpServletRequest request) { + ModelAndView mv = new ModelAndView(); + + User user = (User) request.getSession().getAttribute("user"); + if (ObjectUtil.isNull(user)) { + mv.setViewName("redirect:/user/login"); + } else { + mv.setViewName("page/index"); + mv.addObject(user); + } + + return mv; + } +} diff --git a/spring-boot-demo-template-enjoy/src/main/java/com/xkcoding/template/enjoy/controller/UserController.java b/spring-boot-demo-template-enjoy/src/main/java/com/xkcoding/template/enjoy/controller/UserController.java new file mode 100644 index 0000000..41bddff --- /dev/null +++ b/spring-boot-demo-template-enjoy/src/main/java/com/xkcoding/template/enjoy/controller/UserController.java @@ -0,0 +1,45 @@ +package com.xkcoding.template.enjoy.controller; + +import com.xkcoding.template.enjoy.model.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; + +/** + *

+ * 用户页面 + *

+ * + * @package: com.xkcoding.template.enjoy.controller + * @description: 用户页面 + * @author: yangkai.shen + * @date: Created in 2018/10/11 2:24 PM + * @copyright: Copyright (c) 2018 + * @version: V1.0 + * @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("redirect:/"); + + request.getSession().setAttribute("user", user); + return mv; + } + + @GetMapping("/login") + public ModelAndView login() { + return new ModelAndView("page/login"); + } +} diff --git a/spring-boot-demo-template-enjoy/src/main/java/com/xkcoding/template/enjoy/model/User.java b/spring-boot-demo-template-enjoy/src/main/java/com/xkcoding/template/enjoy/model/User.java new file mode 100644 index 0000000..99a7bce --- /dev/null +++ b/spring-boot-demo-template-enjoy/src/main/java/com/xkcoding/template/enjoy/model/User.java @@ -0,0 +1,22 @@ +package com.xkcoding.template.enjoy.model; + +import lombok.Data; + +/** + *

+ * 用户 model + *

+ * + * @package: com.xkcoding.template.enjoy.model + * @description: 用户 model + * @author: yangkai.shen + * @date: Created in 2018/10/11 2:21 PM + * @copyright: Copyright (c) 2018 + * @version: V1.0 + * @modified: yangkai.shen + */ +@Data +public class User { + private String name; + private String password; +} diff --git a/spring-boot-demo-template-enjoy/src/main/resources/application.yml b/spring-boot-demo-template-enjoy/src/main/resources/application.yml new file mode 100644 index 0000000..a02fbde --- /dev/null +++ b/spring-boot-demo-template-enjoy/src/main/resources/application.yml @@ -0,0 +1,4 @@ +server: + port: 8080 + servlet: + context-path: /demo \ No newline at end of file diff --git a/spring-boot-demo-template-enjoy/src/main/resources/templates/common/head.html b/spring-boot-demo-template-enjoy/src/main/resources/templates/common/head.html new file mode 100644 index 0000000..6d93920 --- /dev/null +++ b/spring-boot-demo-template-enjoy/src/main/resources/templates/common/head.html @@ -0,0 +1,8 @@ + + + + + spring-boot-demo-template-enjoy + + diff --git a/spring-boot-demo-template-enjoy/src/main/resources/templates/page/index.html b/spring-boot-demo-template-enjoy/src/main/resources/templates/page/index.html new file mode 100644 index 0000000..d90d5e9 --- /dev/null +++ b/spring-boot-demo-template-enjoy/src/main/resources/templates/page/index.html @@ -0,0 +1,9 @@ + + +#include("/common/head.html") + +
+ 欢迎登录,#(user.name)! +
+ + \ No newline at end of file diff --git a/spring-boot-demo-template-enjoy/src/main/resources/templates/page/login.html b/spring-boot-demo-template-enjoy/src/main/resources/templates/page/login.html new file mode 100644 index 0000000..c8d8edc --- /dev/null +++ b/spring-boot-demo-template-enjoy/src/main/resources/templates/page/login.html @@ -0,0 +1,13 @@ + + +#include("/common/head.html") + +
+
+ 用户名 + 密码 + +
+
+ + \ No newline at end of file diff --git a/spring-boot-demo-template-enjoy/src/test/java/com/xkcoding/template/enjoy/SpringBootDemoTemplateEnjoyApplicationTests.java b/spring-boot-demo-template-enjoy/src/test/java/com/xkcoding/template/enjoy/SpringBootDemoTemplateEnjoyApplicationTests.java new file mode 100644 index 0000000..476c25d --- /dev/null +++ b/spring-boot-demo-template-enjoy/src/test/java/com/xkcoding/template/enjoy/SpringBootDemoTemplateEnjoyApplicationTests.java @@ -0,0 +1,16 @@ +package com.xkcoding.template.enjoy; + +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 SpringBootDemoTemplateEnjoyApplicationTests { + + @Test + public void contextLoads() { + } + +}