springboot themeleaf的使用

themeleaf是springboot推荐的页面渲染方式

用法总结暂时参见此博客

配置好之后访问测试接口报错:

controller:

@Controller
@RequestMapping("/demo")
public class SampleController {
    @RequestMapping("/thymeleaf")
    public String thymeleaf(Model model){
        System.out.println("hello");
        model.addAttribute("name","yuanfei");
        return "hello";
    }
}

application.properties

spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html

目录截图:

启动MainApplication,

访问  /demo/thymeleaf

控制台能打印输出 hello,但是页面上却是404

Whitelabel Error Page

This application has no explicit mapping for /error, so you are seeing this as a fallback.

解决:添加thymeleaf依赖

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>

启动,查看效果:

hello.html 页面代码:

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head>
    <title>hello</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<p th:text="'Hello, ' + ${name} + '!'" ></p>
</body>
</html>

猜你喜欢

转载自blog.csdn.net/qq_36922927/article/details/81783930