一、SpringMVC的简介

SpringMVC是什么?


SpringMVC目前最好的实现MVC设计模式的框架,是Spring框架的一个分支产品,以SpringIOC容器为基础,并利用容器的特性来简化它的配置。SpringMVC相当于Spring的一个子模块,可以很好的和Spring结合起来进行开发,是JavaWeb开发者必须要掌握的框架。


SpringMVC能干什么?


实现了MVC设计模式,MVC设计模式是一种常用的软件架构方式:以Controller(控制层),Model(模型层),View(视图层)三个模块分离的形式来组织代码。


MVC流程:控制层接受到客户端请求,调用模型层生成业务数据,传递给视图层,将最终的业务数据和视图响应给客户端做展示。



SpringMVC_第1张图片



SpringMVC就是对这套流程的封装,屏蔽掉很多底层代码,开放出接口,让开发者可以更加轻松快捷的完成基于MVC模式的Web开发。


SpringMVC实现原理:


核心组件:


1.DispatcherServlet:前端控制器,是整个流程控制的核心,控制其他组件的执行,统一调度,降低组件之间的耦合性,相当于总指挥。


2.Handler:处理器,完成具体业务逻辑,相当于Servlet或Action。


3.HandlerMapping:DispatcherServlet接收到请求之后,通过HandlerMapping将不同的请求分发到不同的Handler


4.HandlerInterceptor:处理器拦截器,是一个接口,如果我们需要做一些拦截处理,可以来实现这个接口。


5.HandlerExecutionChain:处理器执行链,包括两部分内容:Handler和HandlerInterceptor(系统会有一个默认的HandlerInterceptor,如果需要额外拦截处理,可以添加拦截器设置)。


6.HandlerAdapter:处理器适配器,Handler执行业务方法之前,需要进行一系列的操作包括表单数据的验证,数据类型的转换,将表单数据封装到JavaBean等等,这一系列的操作,都是由HandlerAdapter来完成,DispatcherServlet通过HandlerAdapter执行不同的Handler。


7.ModelAndView:装载了模型数据和视图信息,作为Handler的处理结果,返回给DispatcherServlet。


8.ViewResolver:视图解析器,DispatcherServlet通过它将逻辑视图解析成物理视图,最终将渲染结果响应给客户端。


以上就是SpringMVC的核心组件。那么这些组件之间是如何进行交互的呢?


我们来看SpringMVC的实现流程:


1.客户端请求被DispatcherServlet(前端控制器)接收。


2.根据HandlerMapping映射到Handler。


3.生成Handler和HandlerInterceptor(如果有则生成)。


4.Handler和HandlerInterceptor以HandlerExecutionChain的形式一并返回给DispatcherServlet。


5.DispatcherServlet通过HandlerAdapter调用Handler的方法做业务逻辑处理。


6.返回一个ModelAndView对象给DispatcherServlet。


7.DispatcherServlet将获取的ModelAndView对象传给ViewResolver视图解析器,将逻辑视图解析成物理视图View。


8.ViewResolver返回一个View给DispatcherServlet。


9.DispatcherServlet根据View进行视图渲染(将模型数据填充到视图中)。


10.DispatcherServlet将渲染后的视图响应给客户端。


SpringMVC_第2张图片


如何使用?

真正需要我们开发者进行编写的组件只有两个,Handler:处理业务逻辑, View:JSP做展示。


二、第一个程序

1.web.xml中配置SpringMVC的DispatcherServlet。



<web-app version="2.5"
   xmlns="http://java.sun.com/xml/ns/javaee"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
   http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
>

 <display-name>display-name>    
     <welcome-file-list>
          <welcome-file>index.jspwelcome-file>
     welcome-file-list>

   <servlet>
       <servlet-name>springmvcservlet-name>
       <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
       <init-param>
             <param-name>contextConfigLocationparam-name>
             
             <param-value>classpath:springmvc.xmlparam-value>
       init-param>
     servlet>
     <servlet-mapping>
       <servlet-name>springmvcservlet-name>
       <url-pattern>/url-pattern>
     servlet-mapping>

web-app>


2.创建springmvc.xml配置文件。



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


   
   <context:component-scan base-package="com.southwind.handler">context:component-scan>

   
   <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
       
       <property name="prefix" value="/">property>
       
       <property name="suffix" value=".jsp">property>
   bean>

beans>

记住一条:目标资源路径=前缀+返回值+后缀。


比如DispatcherServlet返回index,配置文件中前缀是/,后缀是.jsp代入上述公式:


目标资源路径=/index.jsp

简单流程

1.DispatcherServlet接收到URL请求index,结合@RequestMapping("/index")注解将该请求交给index业务方法。


2.执行index业务方法,控制打印日志,并返回"index"字符串。


3.结合springmvc.xml中的视图解析器配置,找到目标资源:/index.jsp,即根目录的index.jsp,将该jsp资源返回客户端完成响应。


三、注解

@RequestMapping

SpringMVC通过@RequestMapping注解将URL请求与业务方法进行进行映射。

@RequestMapping(value="/postTest",method=RequestMethod.POST)
   public String postTest(@RequestParam("name") String name){
       System.out.println("postTest");
       return "index";
   }

SpringMVC同时也支持restful风格的URL。


    @RequestMapping(value="rest/{name}")
   public String restTest(@PathVariable("name") String name){
       System.out.println(name);
       return "index";
   }

映射Cookie:

SpringMVC通过映射可以直接在业务方法中获取Cookie的值。


    @RequestMapping("/cookieTest")
   public String getCookie(@CookieValue(value="JSESSIONID") String sessionId){
       System.out.println(sessionId);
       return "index";
   }

使用pojo绑定参数:

jsp

"addUser" method="post">
       编号:"text" name="id"/>

       姓名:"text" name="name"/>

       地址:"text" name="address.name"/>

       "submit" value="提交"/>
   
  @RequestMapping("/addUser")
   public String getPOJO(User user){
       System.out.println(user);
       return "index";
   }

SpringMVC解决中文乱码很简单,在web.xml中添加过滤器即可。

<filter>  
       <filter-name>encodingFilterfilter-name>  
       <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>  
       <init-param>  
           <param-name>encodingparam-name>  
           <param-value>UTF-8param-value>  
       init-param>  
   filter>  
   <filter-mapping>  
       <filter-name>encodingFilterfilter-name>  
       <url-pattern>/*url-pattern>  
   filter-mapping>

JSP页面的转发和重定向

转发:


    @RequestMapping("forwardTest")
   public String forwardTest(){
       return "forward:/index.jsp";
   }

重定向:


    @RequestMapping("redirectTest")
   public String redirectTest(){
       return "redirect:/index.jsp";
   }

四、数据绑定

SpringMVC的HandlerAdapter组件会在执行Handler业务方法之前,完成参数的绑定

@ResponseBody注解直接返回字符串到前端,不需要返回jsp页面。

  @RequestMapping(value="/packageType")
   @ResponseBody
   public String packageType(@RequestParam(value="id",required=false,defaultValue="1") Integer id){
       return "id:"+id;
   }

处理@ResponseBody中文乱码:


在springmvc.xml中配置消息转换器

<mvc:annotation-driven >
       
       <mvc:message-converters register-defaults="true">
         <bean class="org.springframework.http.converter.StringHttpMessageConverter">
           <property name="supportedMediaTypes" value="text/html;charset=UTF-8"/>
         bean>
       mvc:message-converters>
   mvc:annotation-driven>

List


SpringMVC不支持List类型的直接转换,需要包装成Object。

input的name指向自定义包装类UserList中的users属性,级联到name和age,同时以下标区分集合中不同的对象。

"listType" method="post">
       用户1姓名:"text" name="users[0].name"/>

       用户1年龄:"text" name="users[0].age"/>

       用户2姓名:"text" name="users[1].name"/>

       用户2年龄:"text" name="users[1].age"/>

       用户3姓名:"text" name="users[2].name"/>

       用户3年龄:"text" name="users[2].age"/>

       "submit" value="提交"/>
   

Set


和List一样,需要封装自定义包装类,将Set集合作为属性。不同的是,使用Set集合,需要在包装类构造函数中,为Set添加初始化对象。


    public class UserSet {
   private Set users = new HashSet();

   public Set getUsers() {
       return users;
   }

   public void setUsers(Set users) {
       this.users = users;
   }

   public UserSet(){  
       users.add(new User());  
       users.add(new User());  
       users.add(new User());  
   }
}

Map

JSP,与List和Set不同的是,不能通过下标区分不同的对象,改为通过key值区分。


    
"mapType" method="post">
       用户1姓名:"text" name="users['a'].name"/>

       用户1年龄:"text" name="users['a'].age"/>

       用户2姓名:"text" name="users['b'].name"/>

       用户2年龄:"text" name="users['b'].age"/>

       用户3姓名:"text" name="users['c'].name"/>

       用户3年龄:"text" name="users['c'].age"/>

       "submit" value="提交"/>
   

JSON


JSP:Ajax请求后台业务方法,并将json格式的参数传给后台。




注意

1.json数据必须用JSON.stringify()方法转换成字符串。

2.contentType不能省略。

业务方法:


    @RequestMapping(value="/jsonType")
   @ResponseBody
   public User jsonType(@RequestBody User user){
       //修改年龄
       user.setAge(user.getAge()+10);
       //返回前端
       return user;
   }


@RequestBody注解

读取http请求参数,通过SpringMVC提供的HttpMessageConverter接口将读取的参数转为json,xml格式的数据,绑定到业务方法的形参。


@ResponseBody注解

将业务方法返回的对象,通过HttpMessageConverter接口转为指定格式的数据,json,xml等,响应给客户端。


我们使用的是阿里的fastjson来取代Spring默认的Jackson进行数据绑定。

fastjson的优势在于如果属性为空就不会将其转化为json,数据会简洁很多。


如何使用fastjson

1.pom.xml引入fastjson依赖jar包。


    <dependency>
      <groupId>com.alibabagroupId>
      <artifactId>fastjsonartifactId>
      <version>1.2.18version>
  dependency>


2.springmvc.xml中配置fastjson。


   <mvc:annotation-driven >
       
       <mvc:message-converters register-defaults="true">
         <bean class="org.springframework.http.converter.StringHttpMessageConverter">
           <property name="supportedMediaTypes" value="text/html;charset=UTF-8"/>
         bean>
         
         <bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter4"/>
       mvc:message-converters>
   mvc:annotation-driven>

五、模型数据解析

模型数据的绑定是由ViewResolver来完成的,开发时,我们先添加模型数据,再交给ViewResolver来绑定。

模型数据绑定到request域对象

1.Model

 @RequestMapping("/modelTest")
   public String modelTest(Model model){
       User user = new User();
       user.setId(1);
       user.setName("张三");
       model.addAttribute("user", user);
       return "index";
   }

2.ModelAndView

 @RequestMapping("/modelAndViewTest1")
   public ModelAndView modelAndViewTest1(){
       ModelAndView modelAndView = new ModelAndView();
       User user = new User();
       user.setId(1);
       user.setName("张三");
       modelAndView.addObject("user", user);
       modelAndView.setViewName("index");
       return modelAndView;
   }

3.HttpServletRequest

@RequestMapping("requestTest")
   public String requestTest(HttpServletRequest request){
       User user = new User();
       user.setId(1);
       user.setName("张三");
       request.setAttribute("user", user);
       return "index";
   }



模型数据绑定到session域对象

以上的方式全部是将模型数据绑定到request对象中,如果需要将模型数据绑定到session对象中,只需要在类定义处添加@SessionAttributes(value="user")注解即可。


@Controller
@SessionAttributes(value="user")
public class HelloHandler {

@SessionAttributes可同时绑定多个模型数据。


@Controller
@SessionAttributes(value={"user","address"})
public class HelloHandler {


六、自定义数据转换器

Date类型,HandlerAdapter是不会将String类型转换为Date类型的,我们需要实现Converter接口来协助SpringMVC完成数据类型的转换。

1.创建DateConverter转换器,实现org.springframework.core.convert.converter.Converter接口,

泛型为,将String类型的数值转换为Date类型。


import org.springframework.core.convert.converter.Converter;

public class DateConverter implements Converter<String,Date>{

   private String pattern;

   public DateConverter(String pattern){
       this.pattern = pattern;
   }

   public Date convert(String source) {
       // TODO Auto-generated method stub
       SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
       try {
           return simpleDateFormat.parse(source);
       } catch (ParseException e) {
           // TODO Auto-generated catch block
           e.printStackTrace();
       }
       return null;
   }

}


2.springmvc.xml中配置conversionService bean

bean的类名称必须是org.springframework.context.support.ConversionServiceFactoryBean。


bean必须包含一个converters属性,它将列出在应用程序中用到的所有定制Converter。将我们自定义的DateConverter添加到converters中,

通过有参构造函数创建DateConverter。


同时annotation-driven元素的conversion-service属性赋bean名称。

    <bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean">
       <property name="converters">
           <list>
               <bean class="com.southwind.utils.DateConverter">
                   
                   <constructor-arg type="java.lang.String" value="yyyy-MM-dd"/>
               bean>
           list>
       property>
   bean>

   <mvc:annotation-driven conversion-service="conversionService"/>

3.创建addDate.jsp,form表单提交数据到后台。

    
"dateConverterTest" method="post">
       请输入日期:"text" name="date"/>"font-size:13px">(yyyy-MM-dd)

       "submit" value="提交"/>
   

4.创建业务方法。

    @RequestMapping(value="/dateConverterTest")
   @ResponseBody
   public String dateConverterTest(Date date){
       return date.toString();
   }

除了Date类型的转换,还可以自定义数据格式,比如注册一个Student,前端页面按照"id-name-age"的形式输入String类型的数据,通过转换器,可以将该String类型的数据直接转换为Student对象。

创建addStudent.jsp。

    
"studentConverterTest" method="post">
       学生信息:"text" name="student"/>"font-size:13px">(id-name-age)

       "submit" value="提交"/>
   


3.创建业务方法。

    @RequestMapping(value="/studentConverterTest")
   @ResponseBody
   public String studentConverterTest(Student student){
       return student.toString();
   }


4.创建StudentConverter转换器。

import org.springframework.core.convert.converter.Converter;

import com.southwind.entity.Student;

public class StudentConverter implements Converter<String,Student>{

   public Student convert(String source) {
       // TODO Auto-generated method stub
       String[] args = source.split("-");
       Student student = new Student();
       student.setId(Integer.parseInt(args[0]));
       student.setName(args[1]);
       student.setAge(Integer.parseInt(args[2]));
       return student;
   }

}

5.springmvc.xml中配置StudentConverter转换器。


    <bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean">
       <property name="converters">
           <list>
               
               <bean class="com.southwind.utils.DateConverter">
                   <constructor-arg type="java.lang.String" value="yyyy-MM-dd"/>
               bean>
               
               <bean class="com.southwind.utils.StudentConverter">bean>
           list>
       property>
   bean>


七、RESTful思想

RESTfuk思想


八、文件上传和下载

单文件上传


1.底层使用的是Apache fileupload组件完成上传,SpringMVC只是进行了封装,让开发者使用起来更加方便,所以首先需要引入fileupload组件的依赖。

    <dependency>
       <groupId>commons-iogroupId>
       <artifactId>commons-ioartifactId>
       <version>1.3.2version>
   dependency>

   <dependency>
       <groupId>commons-fileuploadgroupId>
       <artifactId>commons-fileuploadartifactId>
       <version>1.2.1version>
   dependency>


2.JSP

1.input的type设置为file。

2.form表单的method设置为post。(get请求只会将文件名传给后台)

3.form表单的enctype设置为multipart/form-data,以二进制的形式传输数据。


<%@ page language="java" contentType="text/html; charset=UTF-8"
   pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
   String path = request.getContextPath();
   String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
"-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">


"Content-Type" content="text/html; charset=UTF-8">
Insert title here


   
"upload" method="post" enctype="multipart/form-data">
       "file" name="img">
       "submit" name="提交">
   


   if test="${filePath!=null }">
       

上传的图片



       "300px" src="<%=basePath %>${filePath}"/>
   
if>



如果上传成功,返回当前页面,展示上传成功的图片,这里需要使用JSTL标签进行判断。


pom文件中引入JSTL依赖。


    <dependency>
       <groupId>jstlgroupId>
       <artifactId>jstlartifactId>
       <version>1.2version>
   dependency>
   <dependency>
       <groupId>taglibsgroupId>
       <artifactId>standardartifactId>
       <version>1.1.2version>
   dependency>


3.业务方法,使用MultipartFile对象作为参数,接收前端发送过来的文件,并完成上传操作。


   @RequestMapping(value="/upload", method = RequestMethod.POST)
  public String upload(@RequestParam(value="img")MultipartFile img, HttpServletRequest request)
          throws Exception
{
      //getSize()方法获取文件的大小来判断是否有上传文件
      if (img.getSize() > 0) {
         //获取保存上传文件的file文件夹绝对路径
         String path = request.getSession().getServletContext().getRealPath("file");
         //获取上传文件名
         String fileName = img.getOriginalFilename();
         File file = new File(path, fileName);
         img.transferTo(file);
         //保存上传之后的文件路径
         request.setAttribute("filePath", "file/"+fileName);
         return "upload";
       }
     return "error";
 }


4.springmvc.xml配置CommonsMultipartResolver。


    
   <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
       
       <property name="defaultEncoding" value="utf-8"/>
       
       <property name="maxUploadSize" value="1048576"/>
       
       <property name="maxUploadSizePerFile" value="1048576"/>
   bean>

   
   <bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
       <property name="defaultErrorView" value="/error.jsp"/>
   bean>

多文件上传


1.JSP。

<%@ page language="java" contentType="text/html; charset=UTF-8"
   pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
   String path = request.getContextPath();
   String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
"-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">


"Content-Type" content="text/html; charset=UTF-8">
Insert title here


   
"uploads" method="post" enctype="multipart/form-data">
       file1:"file" name="imgs">

       file2:"file" name="imgs">

       file3:"file" name="imgs">
 
       "submit" name="提交">
   

   if test="${filePaths!=null }">
       

上传的图片



       "${filePaths }" var="filePath">
           "300px" src="<%=basePath %>${filePath}"/>
       

   
if>



2.业务方法,使用MultipartFile数组对象接收上传的多个文件。


   @RequestMapping(value="/uploads", method = RequestMethod.POST)
  public String uploads(@RequestParam MultipartFile[] imgs, HttpServletRequest request)
          throws Exception
{
      //创建集合,保存上传后的文件路径
      List filePaths = new ArrayList();
      for (MultipartFile img : imgs) {
          if (img.getSize() > 0) {
             String path = request.getSession().getServletContext().getRealPath("file");
             String fileName = img.getOriginalFilename();
             File file = new File(path, fileName);
             filePaths.add("file/"+fileName);
             img.transferTo(file);
           }
      }
      request.setAttribute("filePaths", filePaths);
      return "uploads";
  }


文件下载


1.JSP,使用超链接,下载之前上传的logo.jpg。


<%@ page language="java" contentType="text/html; charset=UTF-8"
   pageEncoding="UTF-8"%>
"-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">


"Content-Type" content="text/html; charset=UTF-8">
Insert title here


   "download?fileName=logo.jpg">下载图片



2.业务方法。


  @RequestMapping("/download")
 public void downloadFile(String fileName,HttpServletRequest request,
      HttpServletResponse response)
{
    if(fileName!=null){
      //获取file绝对路径
      String realPath = request.getServletContext().getRealPath("file/");
      File file = new File(realPath,fileName);
      OutputStream out = null;
      if(file.exists()){
         //设置下载完毕不打开文件
         response.setContentType("application/force-download");
         //设置文件名
         response.setHeader("Content-Disposition", "attachment;filename="+fileName);
         try {
            out = response.getOutputStream();
            out.write(FileUtils.readFileToByteArray(file));
            out.flush();
         } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
         }finally{
             if(out != null){
                 try {
                    out.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
         }                    
      }          
   }          
}