pom文件中引入Thymeleaf模板
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
在h5页面头部
引入后台代码保存实现比喻
@RequestMapping("/hello")
public String hello(ModelMap model){
User user=new User();
user.setId(1);
user.setName("zhangsan");
model.addAttribute("user",user );
return "index";
}
前端值的获取(方式一):将model中的值赋给input,然后Js获取隐藏域的值。
<input type="text" th:value="${user.name}" id="user_name"/>
在javascript中访问model(方式二):直接在js中取值,需要在script上添加th:inline=”javascript”的属性
<script th:inline="javascript">
var single = [[${user}]];
console.log(user.name+"/"+user.id)
</script>
Thymeleaf如何拼接href \src
//src示例:
<img th:src="@{/img/research/{filename}(filename=${research.filename})}">
//href示例:
<a th:href="@{'https://'+${url.url}}" th:text="${url.urlName}"></a>