sping boot集成thymleaf

创建一个maven工程,指定父pom:

<parent>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-parent</artifactId>
   <version>2.1.5.RELEASE</version>
</parent>

并添加依赖:

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

在maven工程的resource目录中添加static和templates两个目录,分别用来存放静态文件和模板文件。

新建静态页面index.html,内容如下:

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>static</title>
</head>
<body>
this is static html!
</body>
</html>

新建模板文件test.html,内容如下:

@Controller
public class TestController {

    @RequestMapping("/test")
    public String test(@RequestParam(value = "name",required = false)String name, Model model)
    {   model.addAttribute("name",name);
        return "test";
    }
}

新建一个IndexController,跳转到静态页面文件index.html:

@Controller
public class IndexController {

    @RequestMapping("/")
    public String Index()
    {
        return  "redirect:index.html";
    }
}

新建一个TestController,跳转到模板test.html:

@Controller
public class TestController {

    @RequestMapping("/test")
    public String test(@RequestParam(value = "name",required = false)String name, Model model)
    {   model.addAttribute("name",name);
        return "test";
    }
}
© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享