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.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. * @author yangkai.shen
  67. * @date Created in 2018-10-10 10:12
  68. */
  69. @Controller
  70. @Slf4j
  71. public class IndexController {
  72. @GetMapping(value = {"", "/"})
  73. public ModelAndView index(HttpServletRequest request) {
  74. ModelAndView mv = new ModelAndView();
  75. User user = (User) request.getSession().getAttribute("user");
  76. if (ObjectUtil.isNull(user)) {
  77. mv.setViewName("redirect:/user/login");
  78. } else {
  79. mv.setViewName("page/index");
  80. mv.addObject(user);
  81. }
  82. return mv;
  83. }
  84. }
  85. ```
  86. ## UserController.java
  87. ```java
  88. /**
  89. * <p>
  90. * 用户页面
  91. * </p>
  92. *
  93. * @author yangkai.shen
  94. * @date Created in 2018-10-10 10:11
  95. */
  96. @Controller
  97. @RequestMapping("/user")
  98. @Slf4j
  99. public class UserController {
  100. @PostMapping("/login")
  101. public ModelAndView login(User user, HttpServletRequest request) {
  102. ModelAndView mv = new ModelAndView();
  103. mv.addObject(user);
  104. mv.setViewName("redirect:/");
  105. request.getSession().setAttribute("user", user);
  106. return mv;
  107. }
  108. @GetMapping("/login")
  109. public ModelAndView login() {
  110. return new ModelAndView("page/login");
  111. }
  112. }
  113. ```
  114. ## index.html
  115. ```jsp
  116. <!doctype html>
  117. <html lang="en" xmlns:th="http://www.thymeleaf.org">
  118. <header th:replace="~{common/head :: header}"></header>
  119. <body>
  120. <div id="app" style="margin: 20px 20%">
  121. 欢迎登录,<span th:text="${user.name}"></span>!
  122. </div>
  123. </body>
  124. </html>
  125. ```
  126. ## login.html
  127. ```jsp
  128. <!doctype html>
  129. <html lang="en" xmlns:th="http://www.thymeleaf.org">
  130. <header th:replace="~{common/head :: header}"></header>
  131. <body>
  132. <div id="app" style="margin: 20px 20%">
  133. <form action="/demo/user/login" method="post">
  134. 用户名<input type="text" name="name" placeholder="用户名"/>
  135. 密码<input type="password" name="password" placeholder="密码"/>
  136. <input type="submit" value="登录">
  137. </form>
  138. </div>
  139. </body>
  140. </html>
  141. ```
  142. ## application.yml
  143. ```yaml
  144. server:
  145. port: 8080
  146. servlet:
  147. context-path: /demo
  148. spring:
  149. thymeleaf:
  150. mode: HTML
  151. encoding: UTF-8
  152. servlet:
  153. content-type: text/html
  154. cache: false
  155. ```
  156. ## Thymeleaf语法糖学习文档
  157. https://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html