番外一 Thymeleaf th:text
如题,这是一篇番外!
这里记录的是使用Spring boot 期间自己学到的其他东西,由于建立项目的同时集成了Thymeleaf于是就“借坡下驴”使用了这个东东。
首先,pom.xml dependency
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
如果建立项目时勾选了(Visual Studio Code),pom.xml文件中会自动添加。
之后就可以直接使用了!
还是举个例子 : /login 登录界面
先是Controller的代码段
@Controller
public class HomeController{
@RequestMapping("/tologin")
public String tologin(HashMap<String,Object> map){
String a = "不要回答!不要回答!不要回答!";
Person person = new Person();
person.setName("张三");
map.put("firstMessage",a);
map.put("person",person);
//这里不一定要用HashMap,Model等容器都是可以的
return "tologin";
}
}
Person 类
@Setter@Getter@ToString
//这里用的是 Lombok,这个可以自动写Getter、Setter、ToStirng方法
public class Person{
private String name;
}
Lombok的Maven依赖
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
然后html页面
<!DOCTYPE html>
<html>
<head><!--这一块省略掉,不关注--></head>
<body>
<p th:text="${firstMessage}"></p>
<div th:object="${person}">
<p th:text="*{name}"></p>
</div>
</body>
</html>
页面效果:
简单吧!
而且,th:系列(我自己命的名),还有许多,例如:
th:href
th:src
th:style
th:each
th:if
等等!
若是可以,我也会慢慢整理到其他番外内的!敬请期待!。