赞
踩
在pom.xml中添加相关依赖:
<!--整合thymeleaf-->
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring5</artifactId>
</dependency>
<!-- thymeleaf整合springsecurity5的标签库 -->
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity5</artifactId>
</dependency>
public class SpringMvcConfig implements WebMvcConfigurer {
//添加资源处理程序
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/images/**").addResourceLocations("/images/");
registry.addResourceHandler("/css/**").addResourceLocations("/css/");
registry.addResourceHandler("/js/**").addResourceLocations("/js/");
registry.addResourceHandler("/lib/**").addResourceLocations("/lib/");
registry.addResourceHandler("/pages/**").addResourceLocations("/pages/");
}
//开启视图解析器
@Bean
public SpringResourceTemplateResolver springResourceTemplateResolver() {
SpringResourceTemplateResolver srtr = new SpringResourceTemplateResolver();
//设置前后缀
srtr.setPrefix("/pages/");
srtr.setSuffix(".html");
//解决页面的中文乱码
srtr.setCharacterEncoding("UTF-8");
srtr.setOrder(1);
srtr.setTemplateMode("HTML5");
srtr.setCacheable(false);
return srtr;
}
@Bean
public SpringSecurityDialect springSecurityDialect() {
return new SpringSecurityDialect();
}
@Bean
public SpringTemplateEngine springTemplateEngine(SpringResourceTemplateResolver springResourceTemplateResolver, SpringSecurityDialect springSecurityDialect) {
SpringTemplateEngine ste = new SpringTemplateEngine();
ste.setTemplateResolver(springResourceTemplateResolver);
Set set = new HashSet();
set.add(springSecurityDialect);
ste.setAdditionalDialects(set);
return ste;
}
//配置thymeleaf视图解析器
@Bean
public ThymeleafViewResolver thymeleafViewResolver(SpringTemplateEngine springTemplateEngine) {
ThymeleafViewResolver tvr = new ThymeleafViewResolver();
tvr.setTemplateEngine(springTemplateEngine);
tvr.setCharacterEncoding("UTF-8");
return tvr;
}
}
后端使用addAttribute方法向前端传递数据。
model.addAttribute("bookList",bookList);
具体代码如下:
@GetMapping("/books")
public String getAll(@RequestParam(required = false) String name, Model model) {
System.out.println(name);
List<Book> bookList = bookService.getAll(name);
System.out.println(bookList);
model.addAttribute("bookList",bookList);
return "main";
}
在HTML头部标签中添加xlms属性,如下代码所示:
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<tr th:each="book : ${bookList}">
<td bgcolor="#FFFFFF" style='border-bottom: 1px dotted #eeeeee;' th:text="${book.id}"></td>
<td bgcolor="#FFFFFF" style='border-bottom: 1px dotted #eeeeee;' th:text="${book.type}"></td>
<td bgcolor="#FFFFFF" style='border-bottom: 1px dotted #eeeeee;' th:text="${book.name}">></td>
<td bgcolor="#FFFFFF" style='border-bottom: 1px dotted #eeeeee;' th:text="${book.description}"></td>
<td bgcolor="#FFFFFF" style='border-bottom: 1px dotted #eeeeee;'>
<a th:href="@{/books/id(id=${book.id})}" >
修改</a>
|
<a th:attr="href=@{/delete/id(id=${book.id})}">
删除</a>
</td>
</tr>
前端使用${bookList}即可访问到后端传递的数据
th:each 是遍历的意思,会将bookList中的数据遍历出来,类似vue中的v-for
以上是自己学习Theamleaf的记录,希望自己在以后的学习生活中每天开开心心!
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。