0.准备工作
maven环境的搭建及集成SSM框架:
https://blog.csdn.net/qq_37067955/article/details/83899758
1.创建拦截控制逻辑文件
#interceptor.java
package com.neu.interceptor; //当前文件所在目录文件目录
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
public class LoginInterceptor implements HandlerInterceptor{
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
// TODO Auto-generated method stub
HttpSession httpSession = request.getSession();
Object object = httpSession.getAttribute("userName");//获取session中userName
if(object != null){ //判断userName是否为非空,如果为非空,则表示已登录,可继续操作
return true;
}else{ //如果为空,则表示未登录,返回登录界面
request.getRequestDispatcher("/WEB-INF/jsp/login.jsp").forward(request,response);
}
return false;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
ModelAndView modelAndView) throws Exception {
// TODO Auto-generated method stub
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
throws Exception {
// TODO Auto-generated method stub
}
}
2.配置拦截器插件
在springMVC.xml中添加如下代码
<mvc:interceptors>
<mvc:interceptor >
<mvc:mapping path="/**"/>
<mvc:exclude-mapping path="/login.action"/> //表示返回到登录控制逻辑页面
<bean class="com.neu.interceptor.LoginInterceptor"></bean> //表示控制器的目录位置
</mvc:interceptor>
</mvc:interceptors>
3.编写登录前端html界面
3_0.编写一个测试登录界面
#login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="${pageContext.request.contextPath }/login.action">
<input type="text" name="userName">
<input type="password" name="userPass">
<input type="submit">
</form>
</body>
</html>
3_1.编写一个测试用的系统内部界面
#index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<span>你已成功进入本系统</span>
</body>
</html>
4.编写后台控制逻辑页面
4_0.用来表示系统登录逻辑
#LoginControoler.java
package com.neu.controller;
import javax.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class LoginController {
@RequestMapping("login")
public String login(String userName,String userPass,HttpSession session) {
System.out.println(userName);
System.out.println(userPass);
System.err.println(1);
session.setAttribute("userName",userName);
return "index";
}
}
4_0.用来表示进入两个系统页面的测试类
#TestControoler.java
package com.neu.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("Test")
public class TestController {
@RequestMapping("goLogin")
public String goLogin() {
return "login";
}
@RequestMapping("goIndex")
public String goIndex() {
return "index";
}
}
5.源码分享
链接:https://pan.baidu.com/s/1LMYq0zLNNFy2KVAgfT5igg
提取码:pyv4