bkt項目 (四)搭建環境並測試之3添加日誌和 Thymeleaf 模板
阿新 • • 發佈:2018-08-24
model nal -- contex port tro XML 測試 fan
簡介:這個項目是為了學習SpringBoot以及學習SpringCloud用的,如果對你有什麽幫助,還是非常高興的。
GitHub : https://github.com/fankf/bkt.git
技術使用 :SpringBoot + SSM + MySql + Thymeleaf
IDE : STS
日誌相關內容:
添加 logging 相關:
<!-- 日誌 默認logback 在start 包中已經添加 可以不添加-->
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-logging</artifactId> </dependency>
日誌配置:
logging:
# path: d:/logs/test
file: d:/logs/test/test.log #日誌導出
level:
root: info #日誌級別info 此外經常用的還有debug
Thymeleaf 相關內容:
添加依賴:
<!-- 模板thymeleaf --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
URI訪問配置:
server:
port: 8080
tomcat:
uri-encoding: UTF-8
TestController測試修改:
@Controller //@RestController不能導向頁面 @RequestMapping("/test")public class TestController { @Autowired private TestService testService; @RequestMapping(value= {"/list",""},method=RequestMethod.GET) public String getTestList(Model model){ List<Test> testList = testService.getTestList(); model.addAttribute("testList", testList); return "test/userList"; } }
模板存放位置是Resources包下templates/test/userList.html,此處是TestController導出的位置,如下圖:
為什麽會默認找到這個位置是因為在 org.springframework.boot.autoconfigure 包下關於Thymeleaf已經有默認配置:
package org.springframework.boot.autoconfigure.thymeleaf; import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; import java.util.List; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.http.MediaType; import org.springframework.util.MimeType; @ConfigurationProperties(prefix = "spring.thymeleaf") public class ThymeleafProperties { private static final Charset DEFAULT_ENCODING = StandardCharsets.UTF_8; public static final String DEFAULT_PREFIX = "classpath:/templates/"; public static final String DEFAULT_SUFFIX = ".html"; ...以下省略 }
html頁面:(此處引入Thymeleaf)
<!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <table> <thead> <tr> <th>編號</th> <th>姓名</th> </tr> </thead> <tbody> <tr th:each="user:${testList}"> <td th:text="${user.id}">編號</td> <td th:text="${user.testName}">姓名</td> </tr> </tbody> </table> </body> </html>
結果:
正常,ok!
bkt項目 (四)搭建環境並測試之3添加日誌和 Thymeleaf 模板