1、遍历

  1. <div class="panel-body">
  2. <div class="adv-table">
  3. <table class="display table table-bordered table-striped" id="dynamic-table">
  4. <thead>
  5. <tr>
  6. <th>#</th>
  7. <th>name</th>
  8. <th>age</th>
  9. <th>email</th>
  10. <th>操作</th>
  11. </tr>
  12. </thead>
  13. <tbody>
  14. <tr class="gradeX" th:each="user: ${users.records}">
  15. <td th:text="${user.id}"></td>
  16. <td>[[${user.name}]]</td>
  17. <td th:text="${user.age}">Win 95+</td>
  18. <td th:text="${user.email}">4</td>
  19. <td><a th:href="@{/user/delete/{id}(id=${user.id},pn=${users.current})}" class="btn btn-danger btn-sm" type="button">删除</a>
  20. </td>
  21. </tr>
  22. </table>
  23. <div class="row-fluid">
  24. <div class="span6">
  25. <div class="dataTables_info" id="dynamic-table_info">
  26. 当前第[[${users.current}]]页 总计 [[${users.pages}]]页 共[[${users.total}]]条记录
  27. </div>
  28. </div>
  29. <div class="span6">
  30. <div class="dataTables_paginate paging_bootstrap pagination">
  31. <ul>
  32. <li class="prev disabled">
  33. <a href="#">← 前一页</a>
  34. </li>
  35. <li th:class="${num == users.current?'active':''}" th:each="num:${#numbers.sequence(1,users.pages)}" >
  36. <a th:href="@{/dynamic_table(pn=${num})}">[[${num}]]</a>
  37. </li>
  38. <li class="next disabled"><a href="#">下一页 → </a></li>
  39. </ul>
  40. </div>
  41. </div>
  42. </div>
  43. </div>
  44. </div>

2、拦截器

2.1 自定义的拦截器

  1. /**
  2. * 拦截器作用:登录检查
  3. * @author dongxinxin
  4. * @create 2022-01-27 10:47
  5. */
  6. public class LoginInterceptor implements HandlerInterceptor{
  7. /**
  8. * 登陆前的检查
  9. * 1、配置好拦截器拦截那些请求
  10. * 2、将这些配置放在容器当中
  11. * @param request
  12. * @param response
  13. * @param handler
  14. * @return
  15. * @throws Exception
  16. */
  17. @Override
  18. public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
  19. //登录前检查的逻辑
  20. HttpSession session = request.getSession();
  21. Object loginUser = session.getAttribute("LoginUser");
  22. if(loginUser != null){
  23. //放行
  24. return true;
  25. }
  26. //拦截,也就是未登录,直接跳转到登录页
  27. //此时也可将错误信息放在session中
  28. // response.sendRedirect("/");
  29. request.setAttribute("msg","请重新登陆");
  30. request.getRequestDispatcher("/").forward(request,response);
  31. return false;
  32. }
  33. /**
  34. * 目标方法执行后进行的拦截
  35. * Controller方法处理完之后,DispatcherServlet进行视图的渲染之前,也就是说在这个方法中你可以对ModelAndView进行操作
  36. * @param request
  37. * @param response
  38. * @param handler
  39. * @param modelAndView
  40. * @throws Exception
  41. */
  42. @Override
  43. public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
  44. }
  45. /**
  46. * DispatcherServlet进行视图的渲染之后
  47. * @param request
  48. * @param response
  49. * @param handler
  50. * @param ex
  51. * @throws Exception
  52. */
  53. @Override
  54. public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
  55. }
  56. }

2.2 配置文件

  1. /**
  2. * @author dongxinxin
  3. * @create 2022-01-27 10:52
  4. */
  5. @Configuration
  6. public class AdminConfig implements WebMvcConfigurer {
  7. /**
  8. * addInterceptor(new LoginInterceptor())
  9. * 该方法是将我们自己的拦截器先添加到配置当中
  10. * addPathPatterns("/**")
  11. * 该方法是拦截所有请求
  12. * excludePathPatterns("/","/login")
  13. * 该方法是对那些请求进行放行
  14. *
  15. * "/css/**"
  16. * 表示对css文件下的文件进行放行,以此类推
  17. * @param registry
  18. */
  19. @Override
  20. public void addInterceptors(InterceptorRegistry registry) {
  21. registry.addInterceptor(new LoginInterceptor())
  22. .addPathPatterns("/**")//所有静态资源都会进行拦截
  23. .excludePathPatterns("/","/login","/css/**","/js/**");//只进行放行的资源
  24. }
  25. }