springclould整合thymeleaf的坑


thymeleaf是spring官方推崇的模板引擎,下面说一说如何配置

1 添加依赖

如果是多模块项目最好放到顶层模块中。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf</artifactId>
<version>3.0.11.RELEASE</version>
</dependency>
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring4</artifactId>
<version>3.0.11.RELEASE</version>
</dependency>
<dependency>
<groupId>net.sourceforge.nekohtml</groupId>
<artifactId>nekohtml</artifactId>
<version>1.9.22</version>
</dependency>

2 配置文件中添加配置

配置

3 目录结构

目录结构

4 测试调用

代码:

1
2
3
4
5
6
7
@Controller
public class UserLoginController {
@RequestMapping(value = "/",method = RequestMethod.GET)
public String login(){
return "home";
}
}

页面:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org" >
<head>
<meta charset="UTF-8" />
<title>主页</title>
</head>

<body>
<form class="form-signin" role="form" th:action="@{/user/login}" th:method="post">
<input type="text" class="form-control" placeholder="用户名" required="required" name="userName" />
<input type="password" class="form-control" placeholder="密码" required="required" name="password" />
<button class="btn btn-lg btn-warning btn-block" type="submit">登录</button>
</form>
</body>
</html>

一切正常的话就可以用了,刚开始怎么都不能跳转页面,最后终于发现是没有引用nekohtml依赖,弄了俩小时。。

成功

-------------本文结束,感谢您的阅读-------------

本文标题:springclould整合thymeleaf的坑

文章作者:饭饭君~

发布时间:2019年04月23日 - 15:37

最后更新:2019年04月23日 - 16:01

原始链接:https://yangcf.github.io/2019/04/23/springclould整合thymeleaf的坑/

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。

如果我的文章有帮助到你,欢迎打赏~~
0%