ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

java SpringMVC拦截器 maven分模块开发

2021-03-11 19:34:47  阅读:151  来源: 互联网

标签:拦截器 java SpringMVC spring request System println response out


SpringMVC拦截器

 

 

 

SpringMVC的处理器拦截器功能类似于Servlet规范中的过滤器Filter,用于对处理器Headle进行预处理和后处理

区别就是和Filter加载时机不一样

开发步骤:

  • 创建一个自定义的拦截器实现HandleInterceptor接口,该接口定义了三个方法
    • § preHadle:预处理回调方法,实现处理器的预处理,在客户端请求到达控制器之前做一次拦截(登录校验或者权限验证)该方法中有几个参数
      • HttpServletRequest:请求对象
      • HttpServletResponse:响应对象
      • Object处理器对象,(相应的处理器)

还有返回值:boolean布尔类型  如果返回结果为false,则代表拦截该请求不到达控制器,相当于此次的请求就结束了,此时我们可以response来进行响应,返回值true则代表放行客户端的请求

  • postHandle:后处理的回调方法.控制器中的代码已经加载完毕(在组装ModelAndView对象之前),此时你可以在该方法中定义ModelAndView中的数据.指定model中的传输的数据和指定跳转的视图地址.你也可以把它置为null.
  • afterCompletion:在整个方法请求结束后,组装完ModelAndView对象,即将传输给前端控制器之前拦截
  • 在Springmvc.xml文件中配置自定义拦截器

//拦截器类

public class MyInterceptor01 implements HandlerInterceptor {

    @Override

    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        //chain.doFilter(req,resp)

        //false  拦截客户端发送的请求,前提是必须经过前端控制器  弱于Filter过滤器

        System.out.println(request+"-------");

        System.out.println(response+"-------------");

        System.out.println(handler+"-------------");

        //逻辑判断

        //如果用户已经登陆了,那么应该进行资源放行

        if (request.getSession().getAttribute("login_user")!=null){

            //登陆过  用户经过检验了

            return true;

        }

        //跳转到登陆界面

        response.sendRedirect(request.getContextPath()+"/index.jsp");

        return false;

    }

 

    @Override

    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

        System.out.println(request+"================");

        System.out.println(response+"================");

        System.out.println(handler+"================");

        System.out.println(modelAndView+"================");

        /**

         * 主要就是对 ModelAndView modelAndView  数据视图做处理

         */

    }

 

    @Override

    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

        System.out.println(request+"+++++++++++++++++");

        System.out.println(response+"+++++++++++++++++");

        System.out.println(handler+"+++++++++++++++++");

        System.out.println(ex+"+++++++++++++++++");

        /**

         * 资源的释放  查看一些请求服务器的日志

         */

    }

}

 

 

Maven高级内容

  • 依赖管理

通过maven的依赖管理对项目中使用的jar包进行统一管理

  • dependencies
  • dependency
  • groupId
  • artifactId
  • version
  • scope

依赖范围

对于编译时classpath有效

对于测试时classpath有效

对运行时classpath失效

例子

compile

有效

有效

有效

spring-context

test

无效

有效

无效

junit

provided

有效

有效

无效

jsp/sevice

runtime

无效

有效

有效

Driver mysql驱动

system

有效

有效

无效

maven仓库之外的

备注:如果不设置scope值,默认值为complie

  • 依赖传递

 

  • 依赖冲突

 

依赖冲突的三种解决方案

  • 第一种方式
    • 第一声明原则 类似于html  默认流加载  谁放在前面的jar包 mavean就优先使用(不采用)
    • 路劲近者优先原则,谁的路径最短maven就优先使用谁
  • 第二种方式:
    • 排除依赖

<dependency>

    <groupId>org.springframework</groupId>

    <artifactId>spring-context</artifactId>

    <version>5.0.5.RELEASE</version>

   

    <!--排除依赖  不需要指定版本-->

    <exclusions>

        <exclusion>

            <groupId>org.springframework</groupId>

            <artifactId>spring-beans</artifactId>

        </exclusion>

    </exclusions>

</dependency>

  • 第三种解决方案:版本锁定(推荐使用)

企业里面也是该方式

使用dependencyManagement标签只是对版本进行锁定/传递的依赖的jar也可以更改

真正引入的还是在dependencies标签中

不需要写版本因为dependencyManagement标签版本锁定了

<!--

第三种解决方案:版本锁定

使用dependencyManagement标签进行版本锁定/传递的依赖的jar也可以更改

真正引入的还是在dependencies标签中

不需要写版本因为dependencyManagement标签版本锁定了

-->

<dependencyManagement>

<dependencies>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-context</artifactId>

<version>5.0.5.RELEASE</version>

</dependency>

</dependencies>

</dependencyManagement>

  • 抽取项目版本

方便管理

<!--

    抽取项目中的共同版本

    借助于properties 格式自定义  名称随意

-->

<properties>

    <!--指定Spring项目的版本-->

    <spring.version>5.0.5.RELEASE</spring.version>

</properties>

<dependency>

    <groupId>org.springframework</groupId>

    <artifactId>spring-context</artifactId>

    <version>${spring.version}</version>

</dependency>

  • maven分模块开发

我的分模块

各司其职  每个模块只有本模块相关的内容

 

备注:在聚合工程中,需要web工程中的web.xml文件中,在classpath下,添加*

<!--配置上下文监听器 ContextLoaderListener 读取spring的配置文件-->

<context-param>

    <param-name>contextConfigLocation</param-name>

    <param-value>classpath*:spring/applicationContext-*.xml</param-value>

</context-param>

 

<listener>

    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

</listener>

  • maven工程的继承

新建mavean工程

删除里面的src

在里面新建子mavean  会继承父mavean的jar包

 

 

 

引入其他模块的依赖  引入依赖可以传递依赖

<artifactId>day72_SSM_Web</artifactId>

<!--web工程含有web资源-->

<packaging>war</packaging>

<!--web项目需要建立和Service项目的依赖关系-->

<dependencies>

    <dependency>

        <groupId>com.zhiyou100</groupId>

        <artifactId>day72_SSM_Service</artifactId>

        <version>1.0-SNAPSHOT</version>

    </dependency>

</dependencies>

  • maven工程的聚合

 

 

<!--modules  意思是想把其他模块工程聚合到该工程中

    本质就是想要把其他模块工程交给自己自己统一进行管理

-->

<modules>

    <module>day72_SSM_pojo</module>

    <module>day72_SSM_Dao</module>

    <module>day72_SSM_Service</module>

    <module>day72_SSM_Web</module>

</modules>

标签:拦截器,java,SpringMVC,spring,request,System,println,response,out
来源: https://www.cnblogs.com/shangjinshuai/p/14519985.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有