SpringMVC拦截器

简介: SpringMVC拦截器

目录


使用说明

基础代码展示

UserController

MyInterceptor

spring-mvc.xml

web.xml

error.jsp

index.jsp


使用说明


1.png

1.png

基础代码展示


UserController


package com.ljx.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class UserController {
    @RequestMapping("/target")
    public ModelAndView show(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("name","li");
        modelAndView.setViewName("index");
        return modelAndView;
    }
}

MyInterceptor


package com.ljx.interceptor;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
 * @Author: Ljx
 * @Date: 2021/8/12 16:17
 */
public class MyInterceptor implements HandlerInterceptor {
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String name = request.getParameter("name");
        if (name.equals("li")){
            return true;
        }else {
            request.getRequestDispatcher("/error.jsp").forward(request,response);
            return false;
        }
    }
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
    }
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
    }
}

spring-mvc.xml


<?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:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
                           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!--    mvc注解驱动-->
    <mvc:annotation-driven/>
<!--    配置视图解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
<!--    静态资源环境开放-->
    <mvc:default-servlet-handler/>
<!--    组件扫描  扫描Controller包-->
    <context:component-scan base-package="com.ljx.controller"/>
    <mvc:interceptors>
        <mvc:interceptor>
<!--            对那个进行拦截-->
            <mvc:mapping path="/**"/>
<!--            对那个不进行拦截-->
<!--            <mvc:exclude-mapping path=""/>-->
            <bean class="com.ljx.interceptor.MyInterceptor"/>
        </mvc:interceptor>
    </mvc:interceptors>
</beans>

web.xml


<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
  <display-name>Archetype Created Web Application</display-name>
  <filter>
    <filter-name>CharacterEncodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
      <param-name>encoding</param-name>
      <param-value>UTF-8</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>CharacterEncodingFilter</filter-name>
    <url-pattern>/</url-pattern>
  </filter-mapping>
<!--  配置SpringMvc前端控制器-->
  <servlet>
    <servlet-name>DispatcherServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:spring-mvc.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>DispatcherServlet</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>

error.jsp


<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
error
</body>
</html>

index.jsp


<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page isELIgnored="false" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <h1>hello ${name}</h1>
</body>
</html>


相关文章
|
6月前
|
XML Java 数据格式
|
6月前
|
JSON 算法 Java
SpringMVC(五)【拦截器】
SpringMVC(五)【拦截器】
|
7月前
|
数据采集 监控 前端开发
SpringMVC 拦截器
SpringMVC 拦截器
59 0
|
7月前
|
数据安全/隐私保护
SpringMVC拦截器
SpringMVC拦截器
62 0
|
7月前
springMVC之拦截器
springMVC之拦截器
|
Java
SpringMVC-拦截器
SpringMVC-拦截器
60 0
SpringMVC-拦截器
|
前端开发 JavaScript Java
SpringMVC 中的拦截器
SpringMVC 中的拦截器
|
XML 数据格式 容器
springmvc之拦截器
springmvc之拦截器
114 2