参考学习:https://www.zhihu.com/question/50392663/answer/121033320
总的来说有两大类,但是后面感觉还是用得比较多的,所以就单独分点了。
(1)直接传参
所谓的模板渲染其实就是动态的往页面里面传递数据,即如何将数据从控制端传到页面,这就是所谓的模板的动机,将不同页面的公共抽出来作为模板,而其中不同的数据再通过控制类来传递。这个就靠Thymeleaf来完成,使用Thymeleaf也是需要在pom.xml文件中添加依赖的。同时还需要将HTML的一些依赖在pom.xml文件添加:
<dependency>
<groupId>net.sourceforge.nekohtml</groupId>
<artifactId>nekohtml</artifactId>
<version>1.9.22</version>
</dependency>
在HTML中可以通过th:text="${}" 来获取控制类中的参数。这当然是不够的,仅仅靠这个HTML也不知道这个参数到底是哪个方法的哪个变量的。因此还需要对控制类进行一波操作。控制类直接上代码吧。
@GetMapping("/blogs/{id}")
public String index(@PathVariable int id,Model model)
{
model.addAttribute("title","This is a test!");
model.addAttribute("createdTime", "2015-08-11");
model.addAttribute("content", "This is content");
return "index";
}
HTML文件:
<div >
<div >
<h2 th:text="${title}"></h2>
<span th:text="${createdTime}">
</div>
<div th:text="${content}">
</div>
</div>
这样既可以通过控制类来向控制层向表现层传递参数。
上面的控制类的方法里有一个参数是Model,这个是spring MVC框架提供的model,可以调用addAttribute方法,这样就可以通过访问model来进行模板的渲染了。(2)通过对象传参
另外一种方法是通过向model中传递一个对象,然后HTML中访问参数时可以通过访问对象的属性来取得该值。这里也是直接上代码吧。
实体类:
import java.util.Date;
import ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy;
public class blog {
private Long id;
private String title;
private String content;
private Date createdTime;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public Date getCreatedTime() {
return createdTime;
}
public void setCreatedTime(Date createdTime) {
this.createdTime = createdTime;
}
}
控制类:
@GetMapping("/blogs/object")
public String indexObject(Model model)
{
Blog blog=new Blog();
blog.setContent("这是一个对象的内容");
blog.setCreatedTime(new java.util.Date());
blog.setId(2l);
blog.setTitle("这是对象的标题");
model.addAttribute("blogs",blog);
return "index2";
}
页面:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title >index2</title>
</head>
<body>
<div >
<div >
<h2 th:text="${blogs.title}"></h2>
span th:text="${blogs.createdTime}"></span>
</div>
<div th:text="${blogs.content}">
</div>
</div>
</body>
</html>
这样就完成了数据的传递。
当然这里的时间格式并不是我们一般想要的时间格式,因此可以在HTML中给date规定格式。
th:text="${#dates.format(blogs.createdTime,'yyyy-MM-dd')}"
这里的#dates是Thymeleaf的一个工具类。
(3)HTML中访问多个对象
如果需要访问多个类似的对象,则可以把几个对象封装成一个list数组,然后将数组放入model中,在视图中,可以通过th:each="blog :${blogs}"来一次访问数组中的对象,其余的跟一个访问的一样。
(4)HTML中的参数设置为超链接
有的时候可以在传递到HTML页面的数据中设置链接,比如点击传递过去的title标题会进入另外一个页面,可以这样设置:
<a href="#" th:href="@{'/blogs',${blogs.id}}" th:text="${blogs.title}">
后面传递的是以参数,对应到控制类不同的处理方法。
(5)jsp支持
spring boot也是支持jsp的,原理和HTML类似,也是需要在pom.xml文件中添加依赖的。依赖项:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<scope>provided</scope>
</dependency>
通常是将jsp文件放在webapp/WEB-INF/,并且在配置文件中添加:
spring.mvc.view.prefix: /WEB-INF/jsp/ ## 对应实际JSP模板文件的路径
spring.mvc.view.suffix: .jsp