登录注册拦截器+登录页面+controller业务+通用查询列表页面+分页
@Configuration
public class WebConfiguration implements WebMvcConfigurer {
/**
* 注册自定义拦截器
* @param registry
*/
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new LoginInterceptor())
.addPathPatterns("/**")
.excludePathPatterns("/", "/login");
}
}
自定义登录拦截器
public class LoginInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
if(request.getSession().getAttribute("user")!=null){
return true;
}
response.sendRedirect("/");
return false;
}
}
通用登录页面 <!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <base th:href="${#request.getContextPath()}+'/'"> <link href="bootstrap/css/bootstrap.css" rel="stylesheet"> <script type="text/javascript" src="js/jquery-3.5.1.js"></script> <script type="text/javascript" src="bootstrap/js/bootstrap.js"></script> <head> <meta charset="UTF-8"> <title>登录</title> </head> <body> <div style="width:20%;margin:0 auto; text-align: center; padding-top:5% "> <p id="msg" style="color:red" th:text="${msg}"></p> <form action="login" id="myform" method="post"> <div class="form-group form-inline"> <label for="account">用户名:</label> <input type="text" class="form-control" id="account" name="account" placeholder="请输入用户名"> </div> <div class="form-group form-inline"> <label for="account">密 码:</label> <input type="password" class="form-control" id="password" name="password" placeholder="请输入密码"/> </div> <div class="form-group"> <button class="btn btn-success btn-md" id="btnLogin" type="submit">登录</button> </div> </form> </div> </body> </html>
/** *登录业务层代码 * */ @RequestMapping("login") public String getLogin(String account, String password, HttpServletRequest request){ List<Users> user = userService.login(account, password); if(user.size()>0){ request.getSession().setAttribute("user",user.get(0)); return "redirect:index"; } request.setAttribute("msg","账号或密码错误"); return "login"; }
/** *跳转到登录页面 * */
@RequestMapping("/")
public String getIndex(){
return "login";
}
通用列表
<!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <base th:href="${#request.getContextPath()}+'/'"> <link href="bootstrap/css/bootstrap.css" rel="stylesheet"> <script type="text/javascript" src="js/jquery-3.5.1.js"></script> <script type="text/javascript" src="bootstrap/js/bootstrap.js"></script> <head> <meta charset="UTF-8"> <title>首页</title> <style type="text/css"> .table tbody tr td { vertical-align: middle; } table { margin-top: 10px; } div { padding-top: 10px; } </style> </head> <body> <div> <form class="form-inline" id="myform" action="/index"> <div class="form-group"> <label for="bookName">论文主题:</label> <input type="text" class="form-control" name="title" id="bookName" th:value="${paper.title}" > </div> <div class="form-group"> <label >论文类型:</label> <select name="typeid"> <option value='0' >--请选择--</option> <span th:each="papertype:${paperType}"> <option th:value="${papertype.id}" th:text="${papertype.pername}" th:selected="${paper?.typeid eq papertype?.id}"> </option> </span> </select> <button type="submit" id="btnQuery" class="btn btn-primary">查询</button> <a href="gotoadd">添加论文</a> </div> </form> </div> <div class="container"> <p> 欢迎:<span th:text="${session.user.username}"></span> <button type="button" class="btn btn-danger btn-xs pull-right" id="logout">退出登录</button> </p> <table class="table table-bordered"> <tr> <th class="text-center">论文主题</th> <th class="text-center">作者</th> <th class="text-center">论文类型</th> <th class="text-center">发表时间</th> <th class="text-center">修改时间</th> <th class="text-center">操作</th> </tr> <tr class="text-center" th:each="paper,v:${pageInfo.list}"> <td th:text="${paper.title}"></td> <td th:text="${paper.createdby}"></td> <td th:text="${paper.paperType.pername}"></td> <td th:text="${paper.creationdate}"></td> <td><a th:href="@{'/gotoupdate?id='+${paper.id}}">修改</a></td> <td><a th:href="@{'javascript:deletee('+${paper.id}+')'}">删除</a></td> </tr> </table> <!--分页--> <nav aria-label="Page navigation"> <ul class="pagination"> <li> <a th:href="@{'javascript:sup('+${pageInfo.pageNum}+')'}" aria-label="Previous"> <span aria-hidden="true">上一页</span> </a> </li> <li th:each="p,v:${pageInfo.navigatepageNums}"> <a th:href="@{'javascript:goPage('+${p}+')'}" th:text="${p}">1</a> </li> <li> <a th:href="@{'javascript:sub('+${pageInfo.pageNum}+','+${pageInfo.pages}+')'}" aria-label="Next"> <span aria-hidden="true">下一页</span> </a> </li> </ul> </nav> </div> <script type="text/javascript"> //上一页 function sup(pageNo){ if(pageNo>1){ pageNo--; window.location.href="index?pageNo="+pageNo+"&"+$("#myform").serialize(); } } //下一页 function sub(pageNo,pages){ if(pageNo<pages){ pageNo++; window.location.href="index?pageNo="+pageNo+"&"+$("#myform").serialize(); } } //页码跳转 function goPage(pageNo){ window.location.href="index?pageNo="+pageNo+"&"+$("#myform").serialize(); } function deletee(pp){ if(confirm("确定要删除吗")){ window.location.href="delete?id="+pp; } } </script> </body> </html>

浙公网安备 33010602011771号