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
+
+
+ * 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") + ++ * 启动类 + *
+ * + * @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 @@ + + + + +