《一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码》,点击传送门,即可获取!
-
@Package: com.ty.controller
-
@ClassName: MyInterceptor
-
@Author: tianyn
-
@Description:
-
@Date: 2021/7/19 18:41
-
@Version: 1.0
*/
public class MyInterceptor implements HandlerInterceptor {
//在请求处理的方法之前执行
//如果返回true执行下一个拦截器
//如果返回false就不执行下一个拦截器
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println(“--------处理前---------”);
return true;
}
//在请求处理方法执行之后执行
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
System.out.println(“--------处理后----------”);
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
System.out.println(“---------清理------------”);
}
}
③在springmvc的配置文件中配置拦截器
<?xml version="1.0" encoding="utf-8"?><beans xmlns=“http://www.springframework.org/schema/beans”
xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”
xmlns:context=“http://www.springframework.org/schema/context”
xmlns:aop=“http://www.springframework.org/schema/aop”
xmlns:mvc=“http://www.springframework.org/schema/mvc”
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
https://www.springframework.org/schema/mvc/spring-mvc.xsd">
mvc:annotation-driven/
mvc:default-servlet-handler/
mvc:interceptors
mvc:interceptor
<mvc:mapping path=“/**”/>
</mvc:interceptor>
</mvc:interceptors>
<context:component-scan base-package=“com.ty.controller”></context:component-scan>
④编写一个Controller,接收请求
package com.ty.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class InterceptorController {
@GetMapping(“/ic”)
public String test1(){
System.out.println(“控制器中的方法执行了”);
return “hello”;
}
}
验证用户是否登录
实现思路:
①编写一个页面 login.jsp success.jsp
login.jsp
<%@ page contentType=“text/html;charset=UTF-8” language=“java” %>
用户名:
密码:
success.jsp
<%@ page contentType=“text/html;charset=UTF-8” language=“java” %>
登录成功
②编写一个Controller处理请求
package com.ty.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import javax.servlet.http.HttpSession;
/**
-
@ProjectName: springMVCStudy
-
@Package: com.ty.controller
-
@ClassName: LoginController
-
@Author: tianyn
-
@Description:
-
@Date: 2021/7/19 23:11
-
@Version: 1.0
*/
@RequestMapping(“/user”)
@Controller
public class LoginController {
//跳转到登录页面
@GetMapping(“/goLogin”)
public String goLogin() {
return “login”;
}
//跳转到登录成功页面
@GetMapping(“/goSuccess”)
public String goSuccess() {
return “success”;
}
//登录提交
@RequestMapping(“/loginSubmit”)
public String loginSubmit(HttpSession httpSession, @RequestParam(“username”) String username,@RequestParam(“password”) String password, Model model) {
if (username.equals(“ty”) && password.equals(“123”)) {
httpSession.setAttribute(“user”, username);
return “success”;
} else {
return “redirect:/user/goLogin”;
}
}
//退出登录
@GetMapping(“/loginOut”)
public String loginOut(HttpSession httpSession) {
httpSession.removeAttribute(“user”);
return “redirect:/user/goLogin”;
}
}
③编写用户登录拦截器
package com.ty.controller;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
最后
《一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码》,点击传送门,即可获取!
et.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
最后
[外链图片转存中…(img-vC5oBwLS-1714674579676)]
[外链图片转存中…(img-IMbIVKEt-1714674579677)]
[外链图片转存中…(img-hSFEord9-1714674579677)]
《一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码》,点击传送门,即可获取!