bkt项目 (四)搭建环境并测试之3添加日志和 Thymeleaf 模板

简介:这个项目是为了学习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!

 

posted @ 2018-08-24 16:27  FN飞鸟  阅读(194)  评论(0编辑  收藏  举报