1.在java下新建package,edu.td.xd
2.在edu.td.xd下new package.controller,在edu.td.xd.controller下new class
MasterController
package edu.td.xd.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.servlet.ModelAndView; @Controller @RequestMapping(value ="master",method = RequestMethod.GET) public class MasterController { @RequestMapping("login") public ModelAndView login(){ ModelAndView modelAndView = new ModelAndView(); //指定需要加载的视图 modelAndView.setViewName("login"); return modelAndView; } @RequestMapping("index") public ModelAndView index(ModelAndView modelAndView){ //指定需要加载的视图 modelAndView.setViewName("index"); return modelAndView; } }
3.在webapp下new JSP file, index
4.配置index
<%@ page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8" language="java" %> <html> <head> <title>首页</title> </head> <body> <h1>index 首页</h1> </body> </html>
5.单击add configuration,在+里找到Tomcat Server-Local
6.Depolyment - + - Artifact
7.启动
8.配置error页面
400页面
<%@ page contentType="text/html;charset=UTF-8" language="java" isErrorPage="true" pageEncoding="UTF-8" %> <%@ page import="java.io.PrintWriter" %> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>400</title> </head> <body> <h5>网站开小差了,赶快联系NPC</h5> <ul> <li>请求路径:<%=request.getAttribute("javax.servlet.forward.request_uri") %></li> <li>错误码:<%=request.getAttribute("javax.servlet.error.status_code") %></li> <li>错误信息:<%=exception.getMessage()%></li> </ul> <p> <% exception.printStackTrace(new PrintWriter(out));%> </p> </body> </html>
404页面
<%@ page contentType="text/html;charset=UTF-8" language="java" isErrorPage="true" pageEncoding="UTF-8" %> <%@ page import="java.io.PrintWriter" %> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>404</title> </head> <body> <h5>网站开小差了,您所查找的页面已经無了</h5> <ul> <li>请求路径:<%=request.getAttribute("javax.servlet.forward.request_uri") %></li> <li>错误码:<%=request.getAttribute("javax.servlet.error.status_code") %></li> <li>错误信息:<%=exception.getMessage()%></li> </ul> <p> <% exception.printStackTrace(new PrintWriter(out));%> </p> </body> </html>
500页面
<%@ page contentType="text/html;charset=UTF-8" language="java" isErrorPage="true" pageEncoding="UTF-8" %> <%@ page import="java.io.PrintWriter" %> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>500</title> </head> <body> <h5>网站倒闭了,赶快联系NPC</h5> <ul> <li>请求路径:<%=request.getAttribute("javax.servlet.forward.request_uri") %></li> <li>错误码:<%=request.getAttribute("javax.servlet.error.status_code") %></li> <li>错误信息:<%=exception.getMessage()%></li> </ul> <p> <% exception.printStackTrace(new PrintWriter(out));%> </p> </body> </html>
9.把error文件夹移动到WEB-INF中,在WEB-INF下new directory,views,在views下新建jsp :index,login
index
login
10.在spring-mvc中定义前缀后缀,简化
11.在MasterController中添加lockScreen,views下新建lockScreen.jsp
12.在edu.td.xd.controller下新建Java Class :MasterApiController
package edu.td.xd.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; @Controller @RequestMapping(value = "master",method = RequestMethod.POST) public class MasterApiController { @RequestMapping("loginDeal") @ResponseBody//表示直接返回数据给请求对象(浏览器),无需加载指定界面 public Object loginDeal(){ return null; } @RequestMapping("unlock") @ResponseBody//表示直接返回数据给请求对象(浏览器),无需加载指定界面 public Object unlock(){ return null; } }
13.配置MasterLoginInterceptor
package edu.td.xd.interceptor; import org.springframework.web.servlet.handler.HandlerInterceptorAdapter; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; /** * 拦截器必须实现接口HandlerInterceptor * preHandle 进入拦截器后优先执行判断的方法,如果返回值为True,则继续向后执行下一个拦截器或者执行Contrller,如果返回值是False则不再向后执行 * postHandle postHandle返回值是True且controller没有发生异常时执行的方法 * afterCompletion postHandle返回值是True且controller执行完成后(无论有无异常情况)执行的方法 * * 登录拦截只需要实现preHandle方法 */ public class MasterLoginInterceptor extends HandlerInterceptorAdapter { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { //判断登录 使用session HttpSession session = request.getSession(); if (session.getAttribute("loginUser") == null){ //用户未登录-重定向用户到登录页面 response.sendRedirect("/master/login.do"); return false; } return true; } }
14.在spring-mvc中添加登录验证拦截器
<!--登录验证拦截器 --> <mvc:interceptors> <mvc:interceptor> <mvc:mapping path="/**"/> <mvc:exclude-mapping path="/master/login.do"/> <mvc:exclude-mapping path="/master/loginDeal.do"/> <bean class="edu.td.xd.interceptor.MasterLoginInterceptor"/> </mvc:interceptor> </mvc:interceptors>
15.webapp下new Directory static
16.将AdminLTE-3.1.0复制进test-resources
17.将dist、plugins文件夹复制到static中
18.把bootstrap中的css文件夹复制到plugins下
19.配置login.jsp,完成后将target目录下内容全部删除
login.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <!DOCTYPE html> <html dir="ltr" lang="zh"> <head> <base href="http://localhost/"> <meta charset="UTF-8"> <!--浏览器的适配 --> <meta name="renderer" content="webkit|ie-comp|ie-stand"> <meta http-equiv="X-UA=Compatible" content="IE=edge,chrome=1"> <meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1.0,minimum-scale=1.0,user-scalable=no"> <!--禁止百度转码--> <meta http-equiv="Cache-Control" content="no-transform"> <meta http-equiv="Cache-Control" content="no-siteapp"> <!--站点基础信息--> <title>TD-CMS-0103</title> <meta name="keywords" content="td,xd,cms,通达学院,教学用"> <meta name="description" content="通达学院教学用CMS"> <meta name="author" content="Peanut"> <meta name="sitename" content="TD-CMS-0103"> <link rel="icon" type="image/png" href="/static/dist/img/AdminLTELogo.png"> <!--不再使用默认的.icon作为站点图片,而是用png代替--> <!--导入css样式表 --> <link rel="stylesheet" href="/static/plugins/bootstrap/css/bootstrap.min.css"> <link rel="stylesheet" href="/static/plugins/fontawesome-free/css/all.css"> <link rel="stylesheet" href="/static/plugins/toastr/toastr.min.css"> <link rel="stylesheet" href="/static/plugins/sweetalert2/sweetalert2.min.css"> <!--导入自定义css样式表 --> <link rel="stylesheet" href="/static/css.login.css"> </head> <body class="bg-gray"> <!--引入js脚本库 --> <script src="/static/plugins/jquery/jquery.min.js"></script> <script src="/static/plugins/bootstrap/js/bootstrap.bundle.min.js"></script> <script src="/static/plugins/sweetalert2/sweetalert2.min.js"></script> <script src="/static/plugins/toastr/toastr.min.js"></script> </body> </html>
login.css
html, body{ width: 100%; height: 100vh; overflow: hidden; } .bg-gray{ background-color: #5cbfe3; }