一、freemarker
- pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
- application.properties
#源码在freemarkerAutoConfiguration中的FreeMarkerProperties中
#自定义Freemarker模板位置,模板的位置默认是在resources下的templates
spring.freemarker.template-loader-path=classpath:/lyl/
#是否开启freemarker缓存(一般开发环境不开启,生产环境看情况)
spring.freemarker.cache=false
#自定义模板的编码格式,默认就是UTF-8
spring.freemarker.charset=UTF-8
#定义模板的content-type
spring.freemarker.content-type=text/html
#设置模板的后缀。默认是ftlh
#spring.freemarker.suffix=.ftl
- index.html
<table border="1">
<tr>
<td>用户id</td>
<td>用户姓名</td>
<td>用户地址</td>
<td>用户性别</td>
</tr>
<#list users as user>
<#if user.id==4>
<#break>
</#if>
<tr>
<td>${user.id}</td>
<td>${user.name}</td>
<td>${user.address}</td>
<td>
<#-- if指令 -->
<#-- <#if user.gender==0>
男
<#elseif user.gender==1>
女
<#else>
未知
</#if>-->
<#--switch指令 -->
<#switch user.gender>
<#case 0>男<#break>
<#case 1>女<#break>
<#default>未知
</#switch>
</td>
</tr>
</#list>
</table>
- UserController
@Controller
public class UserController {
@GetMapping("/user")
public String user(Model model) {
List<User> users = new ArrayList<>();
Random random = new Random();
for(int i = 0;i < 10 ;i++) {
User user = new User();
user.setId((long) i);
user.setName("李燕玲"+i);
user.setAddress("重庆"+i);
//0:男,1:女 其他:未知
user.setGender(random.nextInt(3));
users.add(user);
}
model.addAttribute("users",users);
return "index";
}
}
二、thymeleaf
- pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
- BookController
@Controller
public class BookController {
@GetMapping("/book")
public String book(Model model) {
List<Book> books = new ArrayList<>();
for (int i = 0; i < 10;i++) {
Book book = new Book();
book.setId((long) i);
book.setName("三国演义"+i);
book.setAuthor("罗贯中"+i);
book.setPrice(30.0);
books.add(book);
}
model.addAttribute("books",books);
return "book";
}
}
- index.html
<!DOCTYPE html>
<!--引入thymeleaf的名称空间-->
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>thymeleaf</title>
</head>
<body>
<table border="1">
<tr>
<td>书本ID</td>
<td>书本名称</td>
<td>书本作者</td>
<td>书本价格</td>
</tr>
<tr th:each="book : ${books}">
<td th:text="${book.id}"></td>
<td th:text="${book.name}"></td>
<td th:text="${book.author}"></td>
<td th:text="${book.price}"></td>
</tr>
</table>
</body>
</html>
三、jsp
- pom.xml
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
- WebMvcConfig
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
//配置视图解析器
@Override
public void configureViewResolvers(ViewResolverRegistry registry) {
registry.jsp("/jsp/",".jsp");
}
}
- JspController
@Controller
public class JspController {
@GetMapping("/jsp")
public String jsp(Model model, String name) {
model.addAttribute("name",name);
//hello底下的报错,实际上是没有问题的
return "hello";
}
}
- webapp/jsp/hello.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>jsp</title>
</head>
<body>
<h1>hello ${name}</h1>
</body>
</html>
四、添加文件模板到下图页面
步骤: