Bläddra i källkod

springboot高级2

guyanqing 1 år sedan
förälder
incheckning
26c711bc62

+ 6 - 0
springBoot/pom.xml

@@ -62,6 +62,12 @@
             <artifactId>mysql-connector-java</artifactId>
             <scope>runtime</scope>
         </dependency>
+
+<!--        springboot - thymeleaf-->
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-thymeleaf</artifactId>
+        </dependency>
     </dependencies>
 
     <build>

+ 52 - 0
springBoot/src/main/java/com/sf/springboot/controller/HelloWordController.java

@@ -0,0 +1,52 @@
+package com.sf.springboot.controller;
+
+import com.sf.springboot.entity.User;
+import org.springframework.stereotype.Controller;
+import org.springframework.ui.Model;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.ResponseBody;
+import org.springframework.web.bind.annotation.RestController;
+
+import javax.servlet.http.HttpServletRequest;
+import java.util.*;
+
+
+@Controller
+public class HelloWordController {
+
+    @GetMapping("/helloworld")
+    public String helloworld(){
+        System.out.println("helloworld");
+        return "ok";
+    }
+
+    @RequestMapping("/HelloThymeleaf")
+    public String HelloThymeleaf(Model model, HttpServletRequest request){
+        //显示普通文本,从域对象中获取
+        model.addAttribute("username","Tom");
+        //显示带有样式的普通文本
+        model.addAttribute("desc","<span style='color:red'>你好,中国</span>");
+        //显示对象,数据处理(thymeleaf提供了内置对象API可以操作数据,比如对前端显示时间的格式化)
+        User user = new User(1001,"user01",new Date());
+        model.addAttribute("user",user);
+        //内置域对象
+        request.setAttribute("password","123456");
+        //数据遍历 list
+        User user1 = new User(1002,"user02",new Date());
+        User user2 = new User(1003,"user03",new Date());
+        User user3 = new User(1004,"user04",new Date());
+        List<User> users = new ArrayList<>();
+        users.add(user1);
+        users.add(user2);
+        users.add(user3);
+        model.addAttribute("users",users);
+        //数据遍历 map
+        Map<String,User> map = new HashMap<>();
+        map.put("user01",user1);
+        map.put("user02",user2);
+        map.put("user03",user3);
+        model.addAttribute("map",map);
+        return "result";
+    }
+}

+ 6 - 0
springBoot/src/main/resources/application.properties

@@ -8,6 +8,12 @@ spring.jackson.date-format=yyyy-MM-dd HH:mm:ss
 
 spring.jackson.time-zone=GMT+8
 
+#??thymeleaf
+#??html
+spring.thymeleaf.mode=HTML
+spring.thymeleaf.encoding=UTF-8
+spring.thymeleaf.cache=false
+spring.web.resources.static-locations=classpath:/templates/
 
 
 adminName=admin

+ 13 - 0
springBoot/src/main/resources/templates/footer.html

@@ -0,0 +1,13 @@
+<!DOCTYPE html>
+<html lang="en">
+<head>
+  <meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/>
+    <title>Title</title>
+</head>
+<body>
+<footer th:fragment="companyInfo">
+  <p>设为首页 ©2018 SpringBoot 使用<span th:text="${username}"/>前必读意见反馈京ICP证030173号 </p>
+</footer>
+
+</body>
+</html>

+ 19 - 0
springBoot/src/main/resources/templates/index.html

@@ -0,0 +1,19 @@
+<!DOCTYPE html>
+<html lang="en">
+<head>
+    <meta charset="UTF-8">
+    <title>首页</title>
+</head>
+<body>
+<h1>首页</h1>
+
+<a th:href="@{/helloworld}">Hello World</a>
+<br>
+<a href="/HelloThymeleaf">HelloThymeleaf</a>
+
+<div th:include="footer :: companyInfo" th:with="username='韩梅梅'"/>
+</body>
+
+
+
+</html>

+ 10 - 0
springBoot/src/main/resources/templates/ok.html

@@ -0,0 +1,10 @@
+<!DOCTYPE html>
+<html lang="en">
+<head>
+    <meta charset="UTF-8">
+    <title>Title</title>
+</head>
+<body>
+<h1>ok...</h1>
+</body>
+</html>

+ 71 - 0
springBoot/src/main/resources/templates/result.html

@@ -0,0 +1,71 @@
+<!DOCTYPE html>
+<html lang="en">
+<head>
+    <meta charset="UTF-8">
+    <title>result</title>
+</head>
+<body>
+<h1>结果页</h1>
+<p th:text="${username}"></p>
+<!--显示带有样式的普通文本-->
+<p th:utext="${desc}"></p>
+<hr>
+<!--显示对象,数据处理(thymeleaf提供了内置对象API可以操作数据,比如对前端显示时间的格式化)-->
+<div> <p th:text="${user}"></p></div>
+<br>
+<div>
+  <p th:text="${user.getUserId()}"></p>
+  <p th:text="${user.getUserName()}"></p>
+  <p th:text="${user.getHireDate()}"></p>
+  <p th:text="${#dates.format(user.getHireDate(),'yyyy-MM-dd HH:mm:ss')}"></p>
+</div>
+
+<hr>
+<!--内置域对象-->
+<p th:text="${#httpServletRequest.getAttribute('password')}"></p>
+<hr>
+
+<hr>
+<!--数据遍历  list集合-->
+<table>
+  <tr>
+    <td>No.</td>
+    <td>UID</td>
+    <td>姓名</td>
+    <td>创建时间</td>
+    <td>偶数</td>
+    <td>奇数</td>
+  </tr>
+  <tr th:each="x,y:${users}">
+    <td th:text="${y.index+1}"/>
+    <td th:text="${x.userId}"/>
+    <td th:text="${x.userName}"/>
+    <td th:text="${#dates.format(user.getHireDate(),'yyyy-MM-dd HH:mm:ss')}"/>
+    <td th:text="${y.even}"/><!--偶数-->
+    <td th:text="${y.odd}"/><!--奇数-->
+  </tr>
+</table>
+<hr>
+
+<!--数据遍历  map集合-->
+<table>
+  <tr>
+    <td>No.</td>
+    <td>UID</td>
+    <td>姓名</td>
+    <td>创建时间</td>
+    <td>偶数</td>
+    <td>奇数</td>
+  </tr>
+  <tr th:each="x,y:${map}">
+    <td th:text="${y.index+1}"/>
+    <td th:text="${x.value.userId}"/>
+    <td th:text="${x.value.userName}"/>
+    <td th:text="${x.value.hireDate}"/>
+    <td th:text="${y.even}"/><!--偶数-->
+    <td th:text="${y.odd}"/><!--奇数-->
+  </tr>
+</table>
+
+</body>
+</html>