Perfil thymeleaf
Thymeleaf es medio independiente para la Web y moderno del lado del servidor Java motor de plantillas.
Además, debido a thymeleaf plantilla sufijo .html, puede abrir directamente el navegador, por lo tanto, vista previa muy conveniente.
integrar
Crear un proyecto, agregar una dependencia
Una vez creado se ha agregado pom.xml encontrado una buena dependencia
y luego crear una clase de entidad
public class User {
private int id;
private String name;
private String address;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
Crear el controlador
@Controller
public class UserController {
@RequestMapping(value = "index",method = RequestMethod.GET)
public String index(Model model){
List<User> users=new ArrayList<User>();
for (int i=1;i<4;i++){
User user=new User();
user.setId(i);
user.setName("小明"+i);
user.setAddress("东莞"+i);
users.add(user);
}
model.addAttribute("users",users);
return "index";
}
}
El formato predeterminado de codificación: UTF-8
prefijo ViewResolver: ruta de clase: / templates / (recursos / plantilla)
sufijo: .HTML
Index.html pasar por debajo y luego crear plantillas
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
<meta charset="UTF-8">
<title>index</title>
</head>
<body>
<table>
<tr>
<th>编号</th>
<th>姓名</th>
<th>住址</th>
</tr>
<tr th:each="user : ${users}">
<td th:text="${user.id}"></td>
<td th:text="${user.name}"></td>
<td th:text="${user.address}"></td>
</tr>
</table>
</body>
</html>
En thymeleaf por th: atravesar cada instrucción de un conjunto de datos mediante la presentación th: instrucciones de texto para poner en práctica,
atención a introducirse en la parte superior index.html thymeleaf espacio de nombres.
Documentos oficiales
Iniciar proyectos para poner a prueba
una springboot tales integrado thymeleaf pequeña caja tiene éxito.
Si necesita no quieren utilizar la configuración por defecto, se puede configurar por sí mismos spring.thymeleaf
application.properties a continuación sigue a continuación añadidos
#前缀
spring.thymeleaf.prefix=classpath:/html/
#后缀
spring.thymeleaf.suffix=.html
#编码格式
spring.thymeleaf.encoding=utf-8
#使用HTML5标准
spring.thymeleaf.mode=HTML5
UserController añadido
@RequestMapping(value = "hello",method=RequestMethod.GET)
public String hello(Model model){
model.addAttribute("name","hello,thymeleaf");
return "hello";
}
A continuación, la ruta de clase: al lado para crear una carpeta html, a continuación, crear un archivo de hello.html
Además, el apoyo thymeleaf en el js acceso directo a las variables del modelo.
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
<meta charset="UTF-8">
<title>hello</title>
</head>
<body>
<h1 th:text="${name}"></h1>
//在页面模板中,可以直接在 js 中获取到这个变量
<script th:inline="javascript">
var name = [[${name}]];
console.log(name)
</script>
</body>
</html>
Ejecutar prueba,
y finalmente, la estructura de archivos completa