前言
在现代 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>