1.解决表单重复提交:重定向、
@PostMapping("/login")//存在重复提交问题,可以使用重定向
public String main(String username,String password){
//登陆成功重定向到main.html
return "redirect:/main.html";
}
/**
* 去main页面
* @return
*/
@GetMapping("/main.html")
public String mainPage(){
return "main";
}
2.防止直接从浏览器进入后台,可以设置一些条件
@PostMapping("/login")//存在重复提交问题,可以使用重定向
public String main(User user, HttpSession session, Model model){
if(!StringUtils.isEmpty(user.getUserName())&&"123456".equals(user.getPassword())){
session.setAttribute("loginUser", user);
//登陆成功重定向到main.html
return "redirect:/main.html";
}else{
model.addAttribute("msg", "账号密码错误");
return "login";
}
}
@GetMapping("/main.html")
public String mainPage(HttpSession session,Model model){
//是否登录。 拦截器,过滤器
Object loginUser = session.getAttribute("loginUser");
if (loginUser != null){
return "main";
}else{
//回到登录页面
model.addAttribute("msg", "当前未登录,请重新登录");
return "login";
}
3、 将用户信息存到session中,登陆之后显示,这是thymeleaf的行内显示语法 [[${session.loginUser.userName}]]
th:href="@{/editable_table}"表示当前 路径下的editable_table
有时候重修修改了bug还是不能生效可以尝试刷新浏览器
表格内容的遍历
<tr>
<th>#</th>
<th>用户名</th>
<th>密码</th>
</tr>
</thead>
<tbody>
<tr class="gradeX" th:each="user,stats:${users}">
<td th:text="${stats.count}">Trident</td>
<td th:text="${user.userName}">Explorer</td>
<td>[[${user.password}]]</td>
</tr>
</tbody>