SpringMVC使用

SpringMVC:Java实现的MVC轻量级框架

MVC:模型视图控制器

最典型的mvc架构就是:jsp + service + servlet .

Model2时代演变成mvc架构,

以前是jsp(jsp本身就是servlet)+dao = 视图层 + 模型层


架构一定是演进过来的(ALL in One)

<<淘宝的十年架构>> 淘宝技术这十年架构发展_Apple_Web的博客-CSDN博客

王坚:去IOE化

方便团队开发,java是项目越大,越好开发


特点

约定优于配置

中心控制器DispatchServlet


回顾Servlet

尽量用**add framework support(添加框架支持)**来添加web目录,保证web.xml是最新的

M V VM:ViewModel(双向绑定)


HelloSpringMVC

运行原理

在这里插入图片描述

  1. 客户端发送请求

  2. DispatcherServlet(核心控制分发器)根据请求,找到处理器映射器和处理器适配器,让他们去执行一个servlet

  3. SpringMVC会调用DispatchServlet的doService(),debug可以看到

    protected void doService(HttpServletRequest request, HttpServletResponse response) throws Exception {
    
  4. 这个servlet实现了Controller,就是一个Controller(控制器)了.


  5. 实现其中的方法,必须返回一个ModelAndView,相当于告诉DispatcherServlet,去哪个视图,和怎么处理数据.

  6. DispatcherServlet找到对应的视图,渲染数据后响应回客户端


spring-webmvc包含了所有spring和springmvc会用到的jar包

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

web.xml配置DispatcherServlet及映射

java项目中的classpath到底是什么

<load-on-startup>1</load-on-startup>的作用

<servlet>
  <servlet-name>springmvc</servlet-name>
  <!--SpringMVC核心:请求分发器,也是前端控制器-->
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <init-param>
    <!--上下文配置位置-->
    <param-name>contextConfigLocation</param-name>
    <!--使用classpath*:这种前缀,则可以代表多个匹配的文件;
            **/mapper/mapping/*Mapper.xml,双星号**表示在任意目录下-->
    <param-value>classpath:springmvc-servlet.xml</param-value>
  </init-param>

  <!--启动级别:容器是否在启动的时候就加载这个servlet-->
  <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
  <servlet-name>springmvc</servlet-name>
  <!--
    /只匹配所有请求,不匹配jsp页面
    /*(会匹配jsp页面,形成死循环)
   -->
  <url-pattern>/*</url-pattern>
</servlet-mapping>

在spring-servlet.xml中,配置视图适配器视图解析器,设置前后缀,方便找到页面

<!--处理器映射器和处理器适配器-->
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>

<!--视图解析器
        模板引擎:Thymeleaf Freemarker是和vue一起简化前端开发的
        先不要去了解
    -->
<bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
  <!--前后缀-->
  <property name="prefix" value="/WEB-INF/jsp/"/>
  <property name="suffix" value=".jsp"/>
</bean>

<!--只要是/hello的请求,适配器映射器会自动去找这个servlet类-->
<bean id="/hello" class="com.changGe.li.servlets.HelloSpringMVC" />

servlet类实现Controller的方法,返回一个ModelAndView

package com.changGe.li.servlets;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

//只要实现了Controller的类,说明这就是一个控制器了
public class HelloSpringMVC implements Controller {

  @Override
  public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {

    //模型和视图
    ModelAndView modelAndView = new ModelAndView();

    //封装一个对象
    modelAndView.addObject("msg","HelloSpringMVC");

    //跳转页面
    modelAndView.setViewName("hello");

    //返回这个对象才会执行
    return modelAndView;
  }

}

记住一定要把依赖导入进来,不然tomcat报错ClassNotFount

IDEA maven项目部署到tomcat的jar包找不到问题

在这里插入图片描述

注意配置maven构建查找resource目录下的资源文件


<build>
  <!-- 资源目录 -->
  <resources>
    <resource>
      <!-- 设定主资源目录  -->
      <directory>src/main/java</directory>

      <!-- maven default生命周期,process-resources阶段执行maven-resources-plugin插件的resources目标处理主资源目下的资源文件时,只处理如下配置中包含的资源类型 -->
      <includes>
        <include>**/*.xml</include>
        <include>**/*.properties</include>
      </includes>

      <!-- maven default生命周期,process-resources阶段执行maven-resources-plugin插件的resources目标处理主资源目下的资源文件时,不处理如下配置中包含的资源类型(剔除下如下配置中包含的资源类型)-->
      <excludes>
        <exclude>**/*.yaml</exclude>
      </excludes>

      <!-- maven default生命周期,process-resources阶段执行maven-resources-plugin插件的resources目标处理主资源目下的资源文件时,指定处理后的资源文件输出目录,默认是${build.outputDirectory}指定的目录-->
      <!--<targetPath>${build.outputDirectory}</targetPath> -->

      <!-- maven default生命周期,process-resources阶段执行maven-resources-plugin插件的resources目标处理主资源目下的资源文件时,是否对主资源目录开启资源过滤 -->
      <filtering>true</filtering>
    </resource>
  </resources>
</build>

ModelMap家族

在这里插入图片描述


注解开发

web.xml不变,spring-servlet.xml配置注解相关

default和annotation是不处理静态资源,如css文件,和自动注入(省略注解适配器和解析器)

<?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:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
        ">

    <!--自动扫描器:自动扫描特定包下所有的注解,由IOC容器统一管理-->
    <context:component-scan base-package="com.changGe.li.servlets"/>
    <!--默认的servlet处理器(不处理静态资源,如css文件),注解驱动-->
    <mvc:default-servlet-handler/>
    <mvc:annotation-driven/>

    <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
        <!--前后缀-->
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"/>
    </bean>

</beans>

真实的controller类

使用注解Controller后,就必须配置注解解析器

package com.changGe.li.servlets;

import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

//被Spring接管,
//其下所有方法只要返回值是String,就会被视图解析器解析
@org.springframework.stereotype.Controller

//http://localhost:8080/SpringMVC/hello/SpringMVC
@RequestMapping("/hello")
public class HelloSpringMVC {

  @RequestMapping("/SpringMVC")
  public String helloSpringMVC(Model model){
    model.addAttribute("msg","helloSpringMVC");

    return "hello";
  }

}

两个不同的路径,最后都跳转到同一个页面,这个是博客网站的思想,页面复用

@org.springframework.stereotype.Controller
@RequestMapping("/hello")
public class HelloSpringMVC {

  @RequestMapping("/SpringMVC")
  public String helloSpringMVC(Model model){
    model.addAttribute("msg","helloSpringMVC");

    return "hello";
  }


  @RequestMapping("/Spring")
  public String helloSpring(Model model){
    model.addAttribute("msg","helloSpringMVC");

    //文件名中尽量不要用数字,就用英文就好
    return "test";
  }

}

RestFul风格:通过相同的请求方式实现不同的效果

RequestMapping源码中的value别名是path

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package org.springframework.web.bind.annotation;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.core.annotation.AliasFor;

@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Mapping
public @interface RequestMapping {
  String name() default "";

  @AliasFor("path")//定义别名
  String[] value() default {};

  @AliasFor("value")
  String[] path() default {};

  RequestMethod[] method() default {};

  String[] params() default {};

  String[] headers() default {};

  String[] consumes() default {};

  String[] produces() default {};
}
@org.springframework.stereotype.Controller
public class HelloSpringMVC {

  @RequestMapping(path ="/mvc/{a}/{b}",method = RequestMethod.GET)
  public String helloSpringMVC(@PathVariable("a") int a,@PathVariable("b") int b, Model model){
    model.addAttribute("msg",a+b);

    return "hello";
  }

  
	//这里有bug,报404,暂时没有找到原因
  @PostMapping("/post/{a}/{b}")
  public String helloSpring(@RequestParam("a") int a, @RequestParam("b") int b, Model model){
    model.addAttribute("msg",a+b);

    return "hello";
  }

}
<form action="/post" method="post">
  <input type="text" name="a" value="1">
  <input type="text" name="b" value="3">
  <input type="submit" value="提交">
</form>

转发和重定向

有视图解析器的情况 下,默认是转发

把springmvc-servlet.xml中的视图解析器注掉

@RequestMapping(path ="/mvc/{a}/{b}")
public String helloSpringMVC(@PathVariable("a") int a,@PathVariable("b") int b, Model model){
  model.addAttribute("msg",a+b);

  //没有视图解析器:转发
  return "forward:/WEB-INF/jsp/hello.jsp";
}

@RequestMapping("/get")
public String helloSpring(){
  //重定向
  return "redirect:/index.jsp";
}

过滤器

springMVC的乱码过滤

web.xml

<filter>
  <filter-name>encodingFilter</filter-name>
  <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  <init-param>
    <!--CharacterEncodingFilter中有一个encoding属性,被@Nullale注解-->
    <param-name>encoding</param-name>
    <param-value>utf-8</param-value>
  </init-param>
</filter>
<filter-mapping>
  <filter-name>encodingFilter</filter-name>
  <url-pattern>/</url-pattern>
  <url-pattern>/*</url-pattern>
</filter-mapping>

CharacterEncodingFilter中有一个encoding属性,被@Nullale注解

public class CharacterEncodingFilter extends OncePerRequestFilter {
  @Nullable
  private String encoding;

测试

@RequestMapping("/mvc")
public String helloSpringMVC(Model model){
  model.addAttribute("msg","李长歌");
  
  return "hello";
}

大神自定义乱码过滤器

/**
* 解决get和post请求 全部乱码的过滤器
*/
public class GenericEncodingFilter implements Filter {
 
   @Override
   public void destroy() {
  }
 
   @Override
   public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
       //处理response的字符编码
       HttpServletResponse myResponse=(HttpServletResponse) response;
       myResponse.setContentType("text/html;charset=UTF-8");
 
       // 转型为与协议相关对象
       HttpServletRequest httpServletRequest = (HttpServletRequest) request;
       // 对request包装增强
       HttpServletRequest myrequest = new MyRequest(httpServletRequest);
       chain.doFilter(myrequest, response);
  }
 
   @Override
   public void init(FilterConfig filterConfig) throws ServletException {
  }
 
}
 
//自定义request对象,HttpServletRequest的包装类
class MyRequest extends HttpServletRequestWrapper {
 
   private HttpServletRequest request;
   //是否编码的标记
   private boolean hasEncode;
   //定义一个可以传入HttpServletRequest对象的构造函数,以便对其进行装饰
   public MyRequest(HttpServletRequest request) {
       super(request);// super必须写
       this.request = request;
  }
 
   // 对需要增强方法 进行覆盖
   @Override
   public Map getParameterMap() {
       // 先获得请求方式
       String method = request.getMethod();
       if (method.equalsIgnoreCase("post")) {
           // post请求
           try {
               // 处理post乱码
               request.setCharacterEncoding("utf-8");
               return request.getParameterMap();
          } catch (UnsupportedEncodingException e) {
               e.printStackTrace();
          }
      } else if (method.equalsIgnoreCase("get")) {
           // get请求
           Map<String, String[]> parameterMap = request.getParameterMap();
           if (!hasEncode) { // 确保get手动编码逻辑只运行一次
               for (String parameterName : parameterMap.keySet()) {
                   String[] values = parameterMap.get(parameterName);
                   if (values != null) {
                       for (int i = 0; i < values.length; i++) {
                           try {
                               // 处理get乱码
                               values[i] = new String(values[i]
                                      .getBytes("ISO-8859-1"), "utf-8");
                          } catch (UnsupportedEncodingException e) {
                               e.printStackTrace();
                          }
                      }
                  }
              }
               hasEncode = true;
          }
           return parameterMap;
      }
       return super.getParameterMap();
  }
 
   //取一个值
   @Override
   public String getParameter(String name) {
       Map<String, String[]> parameterMap = getParameterMap();
       String[] values = parameterMap.get(name);
       if (values == null) {
           return null;
      }
       return values[0]; // 取回参数的第一个值
  }
 
   //取所有值
   @Override
   public String[] getParameterValues(String name) {
       Map<String, String[]> parameterMap = getParameterMap();
       String[] values = parameterMap.get(name);
       return values;
  }
}

JSON

切记,更新一次maven,就去配置一次项目设置中的工件

SpringMVC默认就是jackson

<dependency>
  <groupId>com.fasterxml.jackson.core</groupId>
  <artifactId>jackson-databind</artifactId>
  <version>2.13.1</version>
</dependency>
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
  
    private String name;
    private int age;
    private char sex;
    
}

@RestController = @Controller + @ResponseBody组成

ResponseBody可以规定不走视图解析器,返回值是字符串

@RestController
public class HelloSpringMVC {

  //解决中文乱码
  @RequestMapping(value = "/mvc",produces ="application/json;charset=utf-8")
  @ResponseBody
  public String helloSpringMVC() throws Exception{

    ObjectMapper mapper = new ObjectMapper();
    User user = new User("李长歌", 18, '女');

    return mapper.writeValueAsString(user);
  }

}

Json返回时间戳的工具类

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;

import java.text.SimpleDateFormat;

public class JsonUtils {

    //传入一个时间,将其转换格式
    public static String getJson(Object object){
        return getJson(object,"yyyy-MM-dd HH:mm:ss");
    }

    public static String getJson(Object object,String dateFormat){
        ObjectMapper mapper = new ObjectMapper();

        //不使用时间戳
        mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS,false);

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(dateFormat);

        mapper.setDateFormat(simpleDateFormat);

        String s = null;
        try {
            s = mapper.writeValueAsString(object);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }

        return s;

    }

}
@RequestMapping(value = "/mvc",produces ="application/json;charset=utf-8")
public String helloSpringMVC(){

  Date date = new Date();

  return JsonUtils.getJson(date);
}

FastJson

<dependency>
  <groupId>com.alibaba</groupId>
  <artifactId>fastjson</artifactId>
  <version>1.2.73</version>
</dependency>
@org.junit.Test
  public void test(){
  User user = new User();
  User user1 = new User();

  ArrayList<Object> arrayList = new ArrayList<>();
  arrayList.add(user);
  arrayList.add(user1);

  //转成json
  JSONObject json1 = (JSONObject)JSON.toJSON(user);//{"sex":"\u0000","age":0}

  String json = JSON.toJSONString(arrayList);
  json = JSON.toJSONString(user);//[{"age":0,"sex":"\u0000"},{"age":0,"sex":"\u0000"}]


  //转回java
  User java = JSON.parseObject(json,User.class);//User(name=null, age=0, sex= )
  java  = (User)JSON.toJavaObject(json1, User.class);//User(name=null, age=0, sex= )

  System.out.println(java);
}

8.0版本的数据库有时需要在properties配置文件中里加一个时区


三大框架整合

从底层往上写

【后端】SSM(Spring + SpringMVC + Mybatis)框架整合(一) - 双份浓缩馥芮白 - 博客园 (cnblogs.com)

(7条消息) 关于maven打包时, 资源文件没有被打包进来的问题_抠脚的大灰狼的博客-CSDN博客_maven打包resource文件没打进去

web.xml

<!--系统默认去/WEB-INF/目录下查找applicationContext.xml作为Spring的配置文件-->
<!--<context-param>
        <param-name>javax.faces.CONFIG_FILES</param-name>
        <param-value>classpath:beans.xml</param-value>
    </context-param>

    &lt;!&ndash;ContextLoaderListener 加载IOC容器,Spring框架的底层是listener &ndash;&gt;
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>-->

<!--springMVC的配置文件-->
<servlet>
  <servlet-name>springmvc</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <init-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
  </init-param>

  <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
  <servlet-name>springmvc</servlet-name>
  <url-pattern>/</url-pattern>
</servlet-mapping>

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"
       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">

  <!--处理器映射器和处理器适配器,不能和注解配置同时出现-->
  <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
  <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>

  <mvc:interceptors>
    <mvc:interceptor>
      <mvc:mapping path="/jsp/*"/>
      <bean class="com.changGe.li.filters.ForcoLoginFilter"/>
    </mvc:interceptor>
  </mvc:interceptors>

  <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
    <!--前后缀-->
    <property name="prefix" value="/jsp/"/>
    <property name="suffix" value=".jsp"/>
  </bean>

  <bean class="com.changGe.li.servlets.Online" id="/jsp/online.do"/>
  <bean class="com.changGe.li.servlets.Register" id="/jsp/register.do"/>
  <bean class="com.changGe.li.servlets.ModifyPasswordServlet" id="/jsp/user.do"/>
  <bean class="com.changGe.li.servlets.LoginServlet" id="/login"/>
  <bean class="com.changGe.li.servlets.LogoutServlet" id="/jsp/logout.do"/>

</beans>

spring-mvc-annotation.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 https://www.springframework.org/schema/context/spring-context.xsd">

  <context:component-scan base-package="com.changGe.li.servlets"/>
  <mvc:default-servlet-handler/>
  <mvc:annotation-driven/>

  <!--文件上传,id不能换,不然spring找不到-->
  <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    <!--要等于jsp的pageEncoding="UTF-8"-->
    <property name="defaultEncoding" value="UTF-8"/>
    <!--文件大小和内存大小(超出进入缓存区)限定,单位字节byte 1kb = 8 *1024-->
    <property name="maxUploadSize" value="81920000"/>
    <property name="maxInMemorySize" value="81920000"/>
  </bean>

</beans>

applicationContext.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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

  <import resource="classpath*:beans.xml"/>
  <import resource="classpath*:spring-mvc.xml"/>
  <import resource="classpath*:spring-mvc-annotation.xml"/>

</beans>

Ajax:异步的JavaScript和XML

不需要刷新网页,刷新部分网页

$.ajax({
  type:"GET",
  url:path+"/jsp/user.do",
  data:{method:"deluser",uid:obj.attr("userid")},
  dataType:"json",
  success:function(data){
    if(data.delResult == "true"){//删除成功:移除删除行
      cancleBtn();
      obj.parents("tr").remove();

      window.location.href = path+"/jsp/user.do?method=query";
 
      return;

    }else if(data.delResult == "false"){
      changeDLGContent("对不起,删除用户【"+obj.attr("username")+"】失败");
    }else if(data.delResult == "notexist"){
      changeDLGContent("对不起,用户【"+obj.attr("username")+"】不存在");
    }
  },
  error:function(data){
    changeDLGContent("对不起,删除失败");
  }
});

js中所有的值或变量,最好提前获取

简化ajax:url,data,seccues,error

$.get(path+"/jsp/user.do",{method:"deluser",uid:obj.attr("userid")},function(data){
  if(data.delResult == "true"){//删除成功:移除删除行
    cancleBtn();
    obj.parents("tr").remove();

    window.location.href = path+"/jsp/user.do?method=query";

    return;

  }else if(data.delResult == "false"){
    changeDLGContent("对不起,删除用户【"+obj.attr("username")+"】失败");
  }else if(data.delResult == "notexist"){
    changeDLGContent("对不起,用户【"+obj.attr("username")+"】不存在");
  }
},function(data){
  changeDLGContent("对不起,删除失败");
});

看一下ajax跨域的东西

ajax跨域,这应该是最全的解决方案了 - 简书 (jianshu.com)


拦截器:AOP思想的具体应用

暂时理解成servlet中的过滤器

原先实现Filter的用户登录过滤器

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException {

  HttpServletRequest req = (HttpServletRequest) request;
  HttpServletResponse resq = (HttpServletResponse) response;

  User user = (User)req.getSession().getAttribute(USER_SESSION);

  if(user == null){
    request.setAttribute("error","请先登录,谢谢");

    resq.sendRedirect(req.getContextPath()+"/login.jsp?error="+ URLEncoder.encode("请先登录,谢谢","utf-8"));

  }else{
    chain.doFilter(req, resq);
  }

}

实现handlerIntercetor就是一个拦截器

//处理器执行前
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

  HttpServletRequest req = (HttpServletRequest) request;
  HttpServletResponse resq = (HttpServletResponse) response;

  User user = (User)req.getSession().getAttribute(USER_SESSION);

  if(user == null){
    request.setAttribute("error","请先登录,谢谢");

    resq.sendRedirect(req.getContextPath()+"/login.jsp?error="+ URLEncoder.encode("请先登录,谢谢","utf-8"));

  }

  return true;//放行到下一个拦截器

}
<mvc:interceptors>
  <mvc:interceptor>
    <mvc:mapping path="/jsp/*"/>
    <bean class="com.changGe.li.filters.ForcoLoginFilter"/>
  </mvc:interceptor>
</mvc:interceptors>

文件上传下载

文件操作以out目录为基准

Spring MVC文件上传 (biancheng.net)

文件上传的配置

<!--文件上传,id不能换,不然spring找不到-->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
  <!--要等于jsp的pageEncoding="UTF-8"-->
  <property name="defaultEncoding" value="UTF-8"/>
  <!--文件大小和内存大小(超出进入缓存区)限定,单位字节byte 1kb = 8 *1024-->
  <property name="maxUploadSize" value="81920000"/>
  <property name="maxInMemorySize" value="81920000"/>
</bean>
@Controller
@RequestMapping("/jsp")
public class FileUpLoad{

  @RequestMapping(value="/upload.do",method = RequestMethod.POST,produces ="application/json;charset=utf-8")
  //将name=filed的控件,得到的文件封装成MultipartFile对象
  public String fileUpload(@RequestParam("filed") MultipartFile multipartFile,HttpServletRequest request) throws Exception{

    // 使用fileupload组件完成文件上传
    // 上传的位置
    String path = request.getSession().getServletContext().getRealPath("/WEB-INF/upload/");

    // 判断,该路径是否存在
    File file = new File(path);
    if(!file.exists()){
      // 创建该文件夹
      file.mkdirs();
    }

    // 获取上传文件的名称
    String filename = multipartFile.getOriginalFilename();

    // 把文件的名称设置唯一值,uuid
    String uuid = UUID.randomUUID().toString().replace("-", "");
    filename = uuid+"_"+filename;

    // 完成文件上传
    multipartFile.transferTo(new File(path,filename));

    request.setAttribute("msg","文件上传成功");

    return "fileUpload";
  }

}

文件上传下载-KuangStudy-文章

@RequestMapping(value="/download")
public String downloads(HttpServletResponse response , HttpServletRequest request) throws Exception{

  //要下载的图片地址
  String path = request.getServletContext().getRealPath("/WEB-INF/upload/");
  String fileName = "springMVC执行流程.png";


  //设置页面不缓存,清空
  response.reset();


  //字符编码
  response.setCharacterEncoding("UTF-8");


  //设置响应头
  response.setHeader("Content-Disposition", "attachment;fileName="+ URLEncoder.encode(fileName, "UTF-8"));     File file = new File(path,fileName);

  //设置响应头:以二进制传输数据
  response.setContentType("multipart/form-data");


  //2、 读取文件--输入流
  InputStream input=new FileInputStream(file);
  //3、 写出文件--输出流
  OutputStream out = response.getOutputStream();
  byte[] buff =new byte[1024]; int index=0;
  //4、执行 写出操作
  while((index= input.read(buff))!= -1){
    out.write(buff, 0, index);
    out.flush();
  }

  out.close();
  input.close();

  return null;

}

.transferTo(new File(path,filename));

request.setAttribute("msg","文件上传成功");

return "fileUpload";

}

}




 [文件上传下载-KuangStudy-文章](https://www.kuangstudy.com/bbs/1456814880131661826) 

```java
@RequestMapping(value="/download")
public String downloads(HttpServletResponse response , HttpServletRequest request) throws Exception{

  //要下载的图片地址
  String path = request.getServletContext().getRealPath("/WEB-INF/upload/");
  String fileName = "springMVC执行流程.png";


  //设置页面不缓存,清空
  response.reset();


  //字符编码
  response.setCharacterEncoding("UTF-8");


  //设置响应头
  response.setHeader("Content-Disposition", "attachment;fileName="+ URLEncoder.encode(fileName, "UTF-8"));     File file = new File(path,fileName);

  //设置响应头:以二进制传输数据
  response.setContentType("multipart/form-data");


  //2、 读取文件--输入流
  InputStream input=new FileInputStream(file);
  //3、 写出文件--输出流
  OutputStream out = response.getOutputStream();
  byte[] buff =new byte[1024]; int index=0;
  //4、执行 写出操作
  while((index= input.read(buff))!= -1){
    out.write(buff, 0, index);
    out.flush();
  }

  out.close();
  input.close();

  return null;

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

helloses

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值