添加thymelleaf依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
1.例如登录验证 ,前端代码例子
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" >
<head>
<!-- 页面meta -->
<meta charset="utf-8">
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/>
<!-- 引入样式 -->
<link rel="stylesheet" href="/css/theme-chalk.css">
<link rel="stylesheet" href="/css/login_style.css">
</head>
<form class="loginForm clear" th:action="@{/loginverify}" method="post">
<div class="loginMain">
<div class="loginlogo"></div>
<p style="color:red" th:text="${message}" th:if="${not #strings.isEmpty(message)}"></p>
<div class="loginCon">
<span class="el-form-item">
<span class="userIcon"></span>
<span class="el-input">
<input class="el-input__inner" name="username" required="" autofocus="" placeholder="账号"/>
</span>
</span>
2.后端代码建议使用modelandview
public ModelAndView postViewLogin(ModelAndView mv, HttpServletRequest request, HttpSession session) throws NoSuchAlgorithmException {
String username = request.getParameter("username")
User user = userDao.findAllByUsername(username);
if(user==null){
mv.addObject("message","用户名或密码错误");
mv.setViewName("login");
return mv;
}
}