SSM:拦截器&model&文件的上传和下载

简介: 本文档介绍了Spring MVC中拦截器的实现、Model对象的使用、文件上传下载的配置及实现,以及项目依赖管理文件pom.xml的配置。拦截器通过继承HandlerInterceptor接口实现请求的预处理、后处理和清理工作。Model对象用于数据传递,支持视图解析器的直接返回。文件上传下载涉及配置multipartResolver及编写控制器方法处理文件操作。pom.xml列出了项目所需的各种依赖库。

前言

在现代 web 开发中,Spring MVC 提供了一系列强大的功能来提升应用的灵活性和可维护性。本文档将深入探讨几个关键领域,包括拦截器的实现、Model 对象的使用、文件上传与下载的配置,以及项目依赖管理文件 pom.xml 的配置。

拦截器

创建 一个 类继承HandlerInterceptor

public class MyInterceptor implements HandlerInterceptor {
    public boolean  preHandle(HttpServletRequest request, HttpServletResponse response, Object   handler) throws Exception{
        System.out.println("===========处理前==========");
        return false; //   当return false 不会执行之后的
    }
    //日志
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object   handler, ModelAndView modelAndView) throws Exception{
        System.out.println("===========处理后==========");
    }
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object   handler, Exception  ex) throws Exception{
        System.out.println("===========清理 ==========");
    }
}
<!--拦截器 的配置 -->
    <mvc:interceptors>
        <mvc:interceptor>
<!--            包括这个 请求 下面 的 所有请求 -->
<!--            /**    文件下所有-->
<!--            /*    文件下一个-->
            <mvc:mapping path="/**"/>
            <bean class="com.youren.interceptor.MyInterceptor"/>
        </mvc:interceptor>
    </mvc:interceptors>

model

当 没有视图 解析器 时

package com.youren.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class ModelTest {
    @RequestMapping("/hello")
    public String test(ModelAndView model) {
        // 业务代码
        String result = "HelloSpringMVC";
        model.addObject("msg", result);
        return "forward:/WEB-INF/jsp/test.jsp";
   //       return  "redirect:index.jsp";
    }
}

有视图解析器则可以直接返回

文件的上传和下载

<dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>4.0.1</version>
    </dependency>
    <dependency>
        <groupId>commons-fileupload</groupId>
        <artifactId>commons-fileupload</artifactId>
        <version>1.3.3</version>
    </dependency>
<!--    the file upload configuration-->
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!--   request encoding   default is ISO-8859-1     -->
        <property name="defaultEncoding" value="utf-8" />
<!--      max    10485760=10m   -->
        <property name="maxUploadSize" value="10485760"/>
        <property name="maxInMemorySize" value="40960"/>
    </bean>
@RestController
public class fileController {
    @RequestMapping("/upload")
    public String upload(@RequestParam("file")CommonsMultipartFile file, HttpServletRequest request)  throws IOException {
        // get the filename
        String uploadFileName = file.getOriginalFilename();
//        judge
        if("".equals(uploadFileName)){
            return "redirect:/index.jsp";
        }
        System.out.println("the upload filename  is:"+uploadFileName);
//        save the file path
        String path= request.getServletContext().getRealPath("/upload");
//        if  the path is not existing create it
        File realPath= new File(path);
        if(!realPath.exists()){
            realPath.mkdirs();
        }
        System.out.println("the upload file path is :"+realPath);
        InputStream inputStream = file.getInputStream();    // input stream
        OutputStream outputStream =new FileOutputStream(new File(realPath,uploadFileName));
//        读取写出
        int len = 0;
        byte[] buffer= new byte[1024];
        while ((len=inputStream.read(buffer))!=-1){
            outputStream.write(buffer,0,len);
            outputStream.flush();
        }
        outputStream.close();
        inputStream.close();
        return "redirect:/index.jsp";
    }
}


pom.xml


  <dependency>
            <groupId>org.eclipse.ecf</groupId>
            <artifactId>javax.servlet</artifactId>
            <version>3.1.0.v201410161800</version>
        </dependency>
        <!--jstl依赖-->
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.1</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>6.0.6</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>6.0.6</version>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13.2</version>
        </dependency>

        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.13</version>
        </dependency>

        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>3.0.1</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.26</version>
        </dependency>

        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.14.2</version>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>4.0.1</version>
        </dependency>
    </dependencies>
相关文章
|
1天前
|
编解码 Java 程序员
写代码还有专业的编程显示器?
写代码已经十个年头了, 一直都是习惯直接用一台Mac电脑写代码 偶尔接一个显示器, 但是可能因为公司配的显示器不怎么样, 还要接转接头 搞得桌面杂乱无章,分辨率也低,感觉屏幕还是Mac自带的看着舒服
|
3天前
|
存储 缓存 关系型数据库
MySQL事务日志-Redo Log工作原理分析
事务的隔离性和原子性分别通过锁和事务日志实现,而持久性则依赖于事务日志中的`Redo Log`。在MySQL中,`Redo Log`确保已提交事务的数据能持久保存,即使系统崩溃也能通过重做日志恢复数据。其工作原理是记录数据在内存中的更改,待事务提交时写入磁盘。此外,`Redo Log`采用简单的物理日志格式和高效的顺序IO,确保快速提交。通过不同的落盘策略,可在性能和安全性之间做出权衡。
1540 5
|
1月前
|
弹性计算 人工智能 架构师
阿里云携手Altair共拓云上工业仿真新机遇
2024年9月12日,「2024 Altair 技术大会杭州站」成功召开,阿里云弹性计算产品运营与生态负责人何川,与Altair中国技术总监赵阳在会上联合发布了最新的“云上CAE一体机”。
阿里云携手Altair共拓云上工业仿真新机遇
|
7天前
|
人工智能 Rust Java
10月更文挑战赛火热启动,坚持热爱坚持创作!
开发者社区10月更文挑战,寻找热爱技术内容创作的你,欢迎来创作!
577 22
|
3天前
|
存储 SQL 关系型数据库
彻底搞懂InnoDB的MVCC多版本并发控制
本文详细介绍了InnoDB存储引擎中的两种并发控制方法:MVCC(多版本并发控制)和LBCC(基于锁的并发控制)。MVCC通过记录版本信息和使用快照读取机制,实现了高并发下的读写操作,而LBCC则通过加锁机制控制并发访问。文章深入探讨了MVCC的工作原理,包括插入、删除、修改流程及查询过程中的快照读取机制。通过多个案例演示了不同隔离级别下MVCC的具体表现,并解释了事务ID的分配和管理方式。最后,对比了四种隔离级别的性能特点,帮助读者理解如何根据具体需求选择合适的隔离级别以优化数据库性能。
201 3
|
10天前
|
JSON 自然语言处理 数据管理
阿里云百炼产品月刊【2024年9月】
阿里云百炼产品月刊【2024年9月】,涵盖本月产品和功能发布、活动,应用实践等内容,帮助您快速了解阿里云百炼产品的最新动态。
阿里云百炼产品月刊【2024年9月】
|
10天前
|
Linux 虚拟化 开发者
一键将CentOs的yum源更换为国内阿里yum源
一键将CentOs的yum源更换为国内阿里yum源
571 5
|
23天前
|
存储 关系型数据库 分布式数据库
GraphRAG:基于PolarDB+通义千问+LangChain的知识图谱+大模型最佳实践
本文介绍了如何使用PolarDB、通义千问和LangChain搭建GraphRAG系统,结合知识图谱和向量检索提升问答质量。通过实例展示了单独使用向量检索和图检索的局限性,并通过图+向量联合搜索增强了问答准确性。PolarDB支持AGE图引擎和pgvector插件,实现图数据和向量数据的统一存储与检索,提升了RAG系统的性能和效果。
|
6天前
|
XML 安全 Java
【Maven】依赖管理,Maven仓库,Maven核心功能
【Maven】依赖管理,Maven仓库,Maven核心功能
233 3
|
9天前
|
存储 人工智能 搜索推荐
数据治理,是时候打破刻板印象了
瓴羊智能数据建设与治理产品Datapin全面升级,可演进扩展的数据架构体系为企业数据治理预留发展空间,推出敏捷版用以解决企业数据量不大但需构建数据的场景问题,基于大模型打造的DataAgent更是为企业用好数据资产提供了便利。
327 2