1.springboot整合thymeleaf(精简版)
1.1引入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
1.2 在resources下创建一个templates文件夹(文件名称不能写错)
该文件夹用来放置静态资源
1.3创建controller类
注意:最好将数据对象封装成一个Map,便于前台数据获取。
import com.mayday.portals.pojo.Article;
import com.mayday.portals.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Controller
public class PortalsController {
@Autowired
private ArticleService articleService;
@GetMapping("/portals.html")
public String toPortalsPage(Model model) {
List<Article> articles = articleService.listAll();
Map<String, List<Article>> models = new HashMap<>();
models.put("articles", articles);
//准备数据模型
model.addAllAttributes(models);
return "portals";
}
}
1.4 创建静态页面
引入thymeleaf依赖
1.5启动
在浏览器中即个访问
在开发环境中最好将thymeleaf的缓存关闭,避免不必要的麻烦。