You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

README.md 4.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. # spring-boot-demo-template-thymeleaf
  2. > 本 demo 主要演示了 Spring Boot 项目如何集成 thymeleaf 模板引擎
  3. ## pom.xml
  4. ```xml
  5. <?xml version="1.0" encoding="UTF-8"?>
  6. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  7. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  8. <modelVersion>4.0.0</modelVersion>
  9. <artifactId>spring-boot-demo-template-thymeleaf</artifactId>
  10. <version>1.0.0-SNAPSHOT</version>
  11. <packaging>jar</packaging>
  12. <name>spring-boot-demo-template-thymeleaf</name>
  13. <description>Demo project for Spring Boot</description>
  14. <parent>
  15. <groupId>com.xkcoding</groupId>
  16. <artifactId>spring-boot-demo</artifactId>
  17. <version>1.0.0-SNAPSHOT</version>
  18. </parent>
  19. <properties>
  20. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  21. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  22. <java.version>1.8</java.version>
  23. </properties>
  24. <dependencies>
  25. <dependency>
  26. <groupId>org.springframework.boot</groupId>
  27. <artifactId>spring-boot-starter-thymeleaf</artifactId>
  28. </dependency>
  29. <dependency>
  30. <groupId>org.springframework.boot</groupId>
  31. <artifactId>spring-boot-starter-web</artifactId>
  32. </dependency>
  33. <dependency>
  34. <groupId>org.springframework.boot</groupId>
  35. <artifactId>spring-boot-starter-test</artifactId>
  36. <scope>test</scope>
  37. </dependency>
  38. <dependency>
  39. <groupId>org.projectlombok</groupId>
  40. <artifactId>lombok</artifactId>
  41. <optional>true</optional>
  42. </dependency>
  43. <dependency>
  44. <groupId>cn.hutool</groupId>
  45. <artifactId>hutool-all</artifactId>
  46. </dependency>
  47. </dependencies>
  48. <build>
  49. <finalName>spring-boot-demo-template-thymeleaf</finalName>
  50. <plugins>
  51. <plugin>
  52. <groupId>org.springframework.boot</groupId>
  53. <artifactId>spring-boot-maven-plugin</artifactId>
  54. </plugin>
  55. </plugins>
  56. </build>
  57. </project>
  58. ```
  59. ## IndexController.java
  60. ```java
  61. /**
  62. * <p>
  63. * 主页
  64. * </p>
  65. *
  66. * @package: com.xkcoding.template.thymeleaf.controller
  67. * @description: 主页
  68. * @author: yangkai.shen
  69. * @date: Created in 2018/10/10 10:12 AM
  70. * @copyright: Copyright (c) 2018
  71. * @version: V1.0
  72. * @modified: yangkai.shen
  73. */
  74. @Controller
  75. @Slf4j
  76. public class IndexController {
  77. @GetMapping(value = {"", "/"})
  78. public ModelAndView index(HttpServletRequest request) {
  79. ModelAndView mv = new ModelAndView();
  80. User user = (User) request.getSession().getAttribute("user");
  81. if (ObjectUtil.isNull(user)) {
  82. mv.setViewName("redirect:/user/login");
  83. } else {
  84. mv.setViewName("page/index");
  85. mv.addObject(user);
  86. }
  87. return mv;
  88. }
  89. }
  90. ```
  91. ## UserController.java
  92. ```java
  93. /**
  94. * <p>
  95. * 用户页面
  96. * </p>
  97. *
  98. * @package: com.xkcoding.template.thymeleaf.controller
  99. * @description: 用户页面
  100. * @author: yangkai.shen
  101. * @date: Created in 2018/10/10 10:11 AM
  102. * @copyright: Copyright (c) 2018
  103. * @version: V1.0
  104. * @modified: yangkai.shen
  105. */
  106. @Controller
  107. @RequestMapping("/user")
  108. @Slf4j
  109. public class UserController {
  110. @PostMapping("/login")
  111. public ModelAndView login(User user, HttpServletRequest request) {
  112. ModelAndView mv = new ModelAndView();
  113. mv.addObject(user);
  114. mv.setViewName("redirect:/");
  115. request.getSession().setAttribute("user", user);
  116. return mv;
  117. }
  118. @GetMapping("/login")
  119. public ModelAndView login() {
  120. return new ModelAndView("page/login");
  121. }
  122. }
  123. ```
  124. ## index.html
  125. ```jsp
  126. <!doctype html>
  127. <html lang="en" xmlns:th="http://www.thymeleaf.org">
  128. <header th:replace="~{common/head :: header}"></header>
  129. <body>
  130. <div id="app" style="margin: 20px 20%">
  131. 欢迎登录,<span th:text="${user.name}"></span>!
  132. </div>
  133. </body>
  134. </html>
  135. ```
  136. ## login.html
  137. ```jsp
  138. <!doctype html>
  139. <html lang="en" xmlns:th="http://www.thymeleaf.org">
  140. <header th:replace="~{common/head :: header}"></header>
  141. <body>
  142. <div id="app" style="margin: 20px 20%">
  143. <form action="/demo/user/login" method="post">
  144. 用户名<input type="text" name="name" placeholder="用户名"/>
  145. 密码<input type="password" name="password" placeholder="密码"/>
  146. <input type="submit" value="登录">
  147. </form>
  148. </div>
  149. </body>
  150. </html>
  151. ```
  152. ## application.yml
  153. ```yaml
  154. server:
  155. port: 8080
  156. servlet:
  157. context-path: /demo
  158. spring:
  159. thymeleaf:
  160. mode: HTML
  161. encoding: UTF-8
  162. servlet:
  163. content-type: text/html
  164. cache: false
  165. ```
  166. ## Thymeleaf语法糖学习文档
  167. https://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html

一个用来深度学习并实战 spring boot 的项目,目前总共包含 66 个集成demo,已经完成 55 个。

Contributors (1)