1、遍历
<div class="panel-body">
<div class="adv-table">
<table class="display table table-bordered table-striped" id="dynamic-table">
<thead>
<tr>
<th>#</th>
<th>name</th>
<th>age</th>
<th>email</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<tr class="gradeX" th:each="user: ${users.records}">
<td th:text="${user.id}"></td>
<td>[[${user.name}]]</td>
<td th:text="${user.age}">Win 95+</td>
<td th:text="${user.email}">4</td>
<td><a th:href="@{/user/delete/{id}(id=${user.id},pn=${users.current})}" class="btn btn-danger btn-sm" type="button">删除</a>
</td>
</tr>
</table>
<div class="row-fluid">
<div class="span6">
<div class="dataTables_info" id="dynamic-table_info">
当前第[[${users.current}]]页 总计 [[${users.pages}]]页 共[[${users.total}]]条记录
</div>
</div>
<div class="span6">
<div class="dataTables_paginate paging_bootstrap pagination">
<ul>
<li class="prev disabled">
<a href="#">← 前一页</a>
</li>
<li th:class="${num == users.current?'active':''}" th:each="num:${#numbers.sequence(1,users.pages)}" >
<a th:href="@{/dynamic_table(pn=${num})}">[[${num}]]</a>
</li>
<li class="next disabled"><a href="#">下一页 → </a></li>
</ul>
</div>
</div>
</div>
</div>
</div>
2、拦截器
2.1 自定义的拦截器
/**
* 拦截器作用:登录检查
* @author dongxinxin
* @create 2022-01-27 10:47
*/
public class LoginInterceptor implements HandlerInterceptor{
/**
* 登陆前的检查
* 1、配置好拦截器拦截那些请求
* 2、将这些配置放在容器当中
* @param request
* @param response
* @param handler
* @return
* @throws Exception
*/
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//登录前检查的逻辑
HttpSession session = request.getSession();
Object loginUser = session.getAttribute("LoginUser");
if(loginUser != null){
//放行
return true;
}
//拦截,也就是未登录,直接跳转到登录页
//此时也可将错误信息放在session中
// response.sendRedirect("/");
request.setAttribute("msg","请重新登陆");
request.getRequestDispatcher("/").forward(request,response);
return false;
}
/**
* 目标方法执行后进行的拦截
* Controller方法处理完之后,DispatcherServlet进行视图的渲染之前,也就是说在这个方法中你可以对ModelAndView进行操作
* @param request
* @param response
* @param handler
* @param modelAndView
* @throws Exception
*/
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
}
/**
* DispatcherServlet进行视图的渲染之后
* @param request
* @param response
* @param handler
* @param ex
* @throws Exception
*/
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
}
}
2.2 配置文件
/**
* @author dongxinxin
* @create 2022-01-27 10:52
*/
@Configuration
public class AdminConfig implements WebMvcConfigurer {
/**
* addInterceptor(new LoginInterceptor())
* 该方法是将我们自己的拦截器先添加到配置当中
* addPathPatterns("/**")
* 该方法是拦截所有请求
* excludePathPatterns("/","/login")
* 该方法是对那些请求进行放行
*
* "/css/**"
* 表示对css文件下的文件进行放行,以此类推
* @param registry
*/
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new LoginInterceptor())
.addPathPatterns("/**")//所有静态资源都会进行拦截
.excludePathPatterns("/","/login","/css/**","/js/**");//只进行放行的资源
}
}