11-SSM_SpringMVC(上)

时间:2022-11-01 07:56:16


目录

​​一,SpringMVC介绍​​

​​1,SpringMVC简单介绍​​

​​2,MVC模式回顾​​

​​二,SpringMVC入门程序​​

​​1,创建项目​​

​​2,引入spring相关配置​​

​​3,编写业务代码​​

​​4,解析​​

​​三,SpringMVC工作流程​​

​​1,工作流程分析​​

​​2,重要组件​​

​​四,@RequestMapping注解解析​​

​​1,@RequestMapping出现的位置​​

​​2,指定请求提交方式​​

​​五,url-pattern解析&静态资源处理​​

​​1,url-pattern解析​​

​​2,静态资源访问​​

​​2.1 使用< mvc:default-servlet-handler/ >​​

​​2.2 使用 < mvc:resources/ >​​

​​六,处理器方法的参数​​

​​1,直接使用方法的参数逐个接收​​

​​2,使用对象接收多个参数​​

​​3,请求参数和方法名称的参数不一致​​

​​4,使用HttpServletRequest 对象获取参数​​

​​5,直接使用URL地址传参@PathVariable​​

​​6,获取日期类型的参数​​

​​7,获取数组类型的参数​​

​​8,获取集合类型的参数​​

​​8.1 集合中元素为普通类型​​

​​8.2 集合中元素为对象​​

​​七,中文乱码的解决方案​​

​​1,解决方案​​

​​3,原理​​

​​ 八,处理器方法的返回值​​

​​1,案例准备​​

​​2,返回ModelAndView​​

​​3,返回String​​

​​4,返回对象类型@ResponseBody​​

​​4.1 返回基础类型​​

​​4.2 返回自定义类型​​

​​4.3 返回列表List​​

​​4.4 返回集合Map​​

​​5,无返回值 void-了解(原生的处理方式)​​


一,SpringMVC介绍

1,SpringMVC简单介绍

介绍

SpringMVC 也叫 Spring web mvc。是 Spring内置的一个MVC框架,在 Spring3.0 后发布。

SpringMVC框架解决了WEB开发中常见的问题(参数接收、文件上传、表单验证等等),而且使用简单,与Spring无缝集成。

支持 RESTful风格的URL请求。采用了松散耦合可插拔组件结构,比其他 MVC 框架更具扩展性和灵活性。

原理

在没有使用SpringMVC之前我们都是使用Servlet在做Web开发。但是使用Servlet开发在接收请求参数,数据共享,页面跳转等操作相对比较复杂。

servlet是java进行web开发的标准,既然springMVC是对servlet的封装,那么很显然SpringMVC底层就是Servlet,SpringMVC就是对Servlet进行深层次的封装。

SpringMVC优势

1、基于 MVC 架构,功能分工明确。解决页面代码和后台代码的分离。

2、简单易用。SpringMVC 也是轻量级的,jar 很小。不依赖的特定的接口和类就可以开发一个注解的SpringMVC 项目。

3、作 为 Spring 框 架 一 部 分 , 能 够 使 用Spring的IoC和AOP 。 方 便 整MyBatis,Hiberate,JPA等其他框架。

4、springMVC的注解强大易用。

2,MVC模式回顾

模型1:jsp+javabean模型---在jsp页面中嵌入大量的java代码

模型2:jsp+servlet+javabean模型---jsp页面将请求发送给servlet,由servlet调用javabean,再由servlet将制定jsp页面响应给用户。

模型2一般就是现在的MVC模式,也是我们一直使用的。

Model-View-Controller:模型--视图--控制器

  • Model: 模型层 javaBean 负责数据访问和业务处理(dao、service、pojo。pojo贯穿三层,但是划分时分在了模型层)
  • View: 视图 JSP技术 负责收集和展示数据(前端页面)
  • Controller: 控制器 servlet技术 中间调度(controller)

控制器的工作:

  • 1、接受客户端的请求(包括请求中携带的数据)
  • 2、处理请求:调用后台的模型层中的业务逻辑
  • 3、页面导航:处理完毕给出响应:JSP页面

二,SpringMVC入门程序

1,创建项目

1,创建Maven项目

11-SSM_SpringMVC(上)

11-SSM_SpringMVC(上)

11-SSM_SpringMVC(上)

2,在pom文件中添加打包方式

11-SSM_SpringMVC(上)

3,在项目中添加其他配置

File-》ProjectStructure-》Modules 

11-SSM_SpringMVC(上)

11-SSM_SpringMVC(上)

11-SSM_SpringMVC(上)

11-SSM_SpringMVC(上)

11-SSM_SpringMVC(上)

11-SSM_SpringMVC(上)

4,补全目录配置

11-SSM_SpringMVC(上)

5,在pom中添加依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.xxy</groupId>
<artifactId>SpringMVC</artifactId>
<version>1.0-SNAPSHOT</version>

<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<!--aop、beans、context、core等依赖均被包含进来-->
<artifactId>spring-webmvc</artifactId>
<version>5.2.13.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<!--注意!:tomcat中已经提供了servlet相关jar包,这里需要将范围设置为provided-->
<scope>provided</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<!--设置使用jdk1.8-->
<target>1.8</target>
<source>1.8</source>
</configuration>
</plugin>
<plugin>
<!--通过tomcat插件运行web项目-->
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<path>/</path>
<port>8080</port>
</configuration>
</plugin>
</plugins>
</build>
</project>

2,引入spring相关配置

6,创建applicationContext.xml、springmvc.xml配置文件

applicationContext.xml(扫描pojo、service、dao包中的类)

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


<!--spring的配置文件:除了控制器之外的bean对象都在这里扫描-->
<context:component-scan base-package="com.xxy.dao,com.xxy.service,com.xxy.pojo"/>

</beans>

 springmvc.xml(负责controller部分)

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


<!--springmvc的配置文件:控制器的bean对象都在这里扫描-->
<context:component-scan base-package="com.xxy.controller"/>
</beans>

7,修改webapp\WEB-INFO\web.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">

<!--spring配置-->
<context-param>
<!--监听器默认去找webapp\WEB-INF下的spring配置文件,
但maven项目中通常将配置文件放在resources下
因此这里需要进行指定-->
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<!--web项目启动后,自动加载spring配置文件
spring配置文件中扫描的bean对象就放在了spring的IoC容器中-->
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<!--springMVC的前端/核心/*控制器-->
<servlet>
<!--名称默认为类名首字母小写的形式-->
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<!-- 这里默认查找{servlet-name}-servlet.xml,所以需要重新配置参数-->
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<!--大于0表示web服务器启动后立即创建对象,数字越小优先级越高-->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>*.do</url-pattern><!--*.do *.action *.mvc /-->
</servlet-mapping>

</web-app>

SpringMVC的配置解析: 

1、servlet-class:

前端控制器的完全限定名,在spring-webmvc-5.2.5.RELEASE.jar包中的 org.springframework.web.servlet下 

2、load-on-startup:

标记是否在Web服务器(这里是Tomcat)启动时会创建这个 Servlet 实 例,即是否在 Web 服务器启动时调用执行该 Servlet 的 init()方 法,而不是在真正访问时才创建。 要求取值是整数。 值大于0:表示容器在启动时就加载并初始化这个 servlet,数值越小,该 Servlet的优先级就 越高,其被创建的也就越早 值小于0或者省略:表示该 Servlet 在真正被使用时才会去创建。 值相同:容器会自己选择创建顺序 

3、url-pattern:

可以写为 / ,可以写为*.do 、*.action、*.mvc等形式,此处先写*.do,以后 介绍不同写法的区别。 

4、init-param:表示了springmvc配置文件的名称和位置。

如果没有配置,默认在项目的WEB-INF 目录下找名称为 Servlet 名称-servlet.xml 的配置文件。 如果没有配置,启用默认的规则:即如果配置文件放在 webapp/WEB-INF/ 目录下,并且配置文 件的名字等于 DispatcherServlet 的名字+ -servlet(即这里的配置文件路径是 webapp/WEB- INF/dispatcherServlet-servlet.xml),如果是这样的话,可以不用添加 init-param 参数,即不 用手动配置 springmvc 的配置文件,框架会自动加载。 
而一般情况下,配置文件是放在类路径下,即 resources 目录下。所以,在注册前端控制器时, 还需要设置查找 SpringMVC 配置文件路径。 其中contextConfigLocation属性:来自DispatcherServlet的父类FrameworkServlet, 该类中的contextConfigLocation属性用来配置springmvc的路径和名称。

3,编写业务代码

1,编写TeamService

package com.xxy.service;

import org.springframework.stereotype.Service;

@Service
public class TeamService {
public void add(){
System.out.println("TeamService---- add-----");
}
}

2,编写TeamController

package com.xxy.controller;

import com.xxy.service.TeamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class TeamController {
// 通过注解的方式自动装配
@Autowired
TeamService teamService;

@RequestMapping("add")
public ModelAndView add() {
System.out.println("TeamController-----add");
teamService.add();
ModelAndView mv = new ModelAndView();
mv.addObject("teamName", "万达国际");//相当于request.setAttrubuite("","");
// 未来经过springmvc的视图解析器处理,转换成物理资源路径,相当于request.getRequestDispatcher("index.jsp").forward();
// 经过InternalResourceViewResolver对象的处理之后加上前后缀就变为了 /jsp/index.jsp
mv.setViewName("index");
return mv;
}
}

3,在springmvc.xml中添加注解驱动设置和视图解析器

<!--这里可能提示多个相同名称的选择,别选错了-->
<mvc:annotation-driven/>
<!--annotation-driven是一种简写形式,也可以手动配置替代这种简写形式,简写形式可以让初学者快速 应用默认配置方案。
该注解会自动注册DefaultAnnotationHandlerMapping与AnnotationMethodHandlerAdapter 两个bean,是springMVC为@Controller分发用户请求所必须的,解决了@Controller注解使用的前提配 置。
同时它还提供了:数据绑定支持,@NumberFormatannotation支持,@DateTimeFormat支持,@Valid支 持,读写XML的支持(JAXB,读写JSON的支持(Jackson)。
我们处理响应ajax请求时,就使用到了对json 的支持(配置之后,在加入了jackson的core和mapper包之后,不写配置文件也能自动转换成json)。
-->

<!--视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!--本项目中,以webapp为根目录下的前缀和后缀限定-->
<property name="prefix" value="/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>

4,运行

若插件plugins中没有tomcat7的图标,可以刷新下

11-SSM_SpringMVC(上)

11-SSM_SpringMVC(上)

11-SSM_SpringMVC(上)

4,解析

当 Spring 和 SpringMVC 同时出现,我们的项目中将存在两个容器,一个是 Spring 容器,另一个是SpringMVC 容器,Spring 容器通过 ContextLoaderListener 来加载,SpringMVC 容器则通过DispatcherServlet 来加载,这两个容器不一样,在web.xml中配置如下

<!--spring配置-->
<context-param>
<!--监听器默认去找webapp\WEB-INF下的spring配置文件,
但maven项目中通常将配置文件放在resources下
因此这里需要进行指定-->
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<!--web项目启动后,自动加载spring配置文件
spring配置文件中扫描的bean对象就放在了spring的IoC容器中-->
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<!--springMVC的前端/核心/*控制器-->
<servlet>
<!--名称默认为类名首字母小写的形式-->
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<!-- 这里默认查找{servlet-name}-servlet.xml,所以需要重新配置参数-->
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<!--大于0表示web服务器启动后立即创建对象,数字越小优先级越高-->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>*.do</url-pattern><!--*.do *.action *.mvc /-->
</servlet-mapping>

1,关于web.xml

11-SSM_SpringMVC(上)

11-SSM_SpringMVC(上)

如图所示:

  • ContextLoaderListener 初始化的上下文加载的 Bean 是对于整个应用程序共享的,不管是使用什么表现层技术,一般如 dao层、service层 的bean;
  • DispatcherServlet 初始化的上下文加载的 bean 是只对 Spring Web MVC 有效的 bean,如Controller、HandlerMapping、HandlerAdapter 等等,该初始化上下文应该只加载 Web相关组件。

2,Spring容器中不能扫描所有Bean嘛?

不可以。当用户发送的请求达到服务端后,会寻找前端控制器DispatcherServlet去处理,只在 SpringMVC容器中找,所以Controller必须在SpringMVC容器中扫描。 

3,SpringMVC容器中可以扫描所有Bean嘛?

可以。可以在SpringMVC容器中扫描所有Bean。但是实际开发中一般不会这么做,原因如下:

  • 为了方便配置文件的管理
  • 未来在 Spring+SpringMVC+Mybatis组合中,要写的配置内容很多,一般都会根据功能分开编写

三,SpringMVC工作流程

1,工作流程分析

11-SSM_SpringMVC(上)

(1)用户通过浏览器发送请求到前端控制器DispatcherServlet。 

(2)前端控制器直接将请求转给处理器映射器HandleMapping。 

(3)处理器映射器HandleMapping会根据请求,找到负责处理该请求的处理器,并将其封装为处理器执行链HandlerExecutionChina后返回给前端控制器DispatcherServlet。 

(4)前端控制器DispatcherServlet根据处理器执行链中的处理器,找到能够执行该处理器的处理器适配器HandlerAdaptor。 

(5)处理器适配器HandlerAdaptor调用执行处理器Controller。 

(6)处理器Controller将处理结果及要跳转的视图封装到一个对象 ModelAndView 中,并将其返回给处理器适配器HandlerAdaptor。 

(7)处理器适配器直接将结果返回给前端控制器DispatcherServlet。 

(8)前端控制器调用视图解析器,将 ModelAndView 中的视图名称封装为视图对象。

(9)视图解析器ViewResolver将封装了的视图View对象返回给前端控制器DispatcherServlet.

(10)前端控制器DispatcherServlet调用视图对象,让其自己进行渲染,即进行数据填充,形成响应对象。

(11)前端控制器响应浏览器。

2,重要组件

1.DispatcherServlet:前端控制器,也称为*控制器或者核心控制器。

  • 用户请求的入口控制器,它就相当于 mvc 模式中的c,DispatcherServlet 是整个流程控制的中心,相当于是 SpringMVC 的大脑,由它调用其它组件处理用户的请求,DispatcherServlet 的存在降低了组件之间的耦合性。
  • SpringMVC框架提供的该核心控制器需要我们在web.xml文件中配置。

2.HandlerMapping:处理器映射器

  • HandlerMapping也是控制器,派发请求的控制器。我们不需要自己控制该类,但是他是springmvc运转历程中的重要的一个控制器。
  • HandlerMapping负责根据用户请求找到 Handler 即处理器(也就是我们所说的 Controller),SpringMVC 提供了不同的映射器实现不同的映射方式,例如:配置文件方式,实现接口方式,注解方式等,在实际开发中,我们常用的方式是注解方式。

3.Handler:处理器

  • Handler 是继 DispatcherServlet 前端控制器的后端控制器,在DispatcherServlet 的控制下 Handler 对具体的用户请求进行处理。由于 Handler 涉及到具体的用户业务请求,所以一般情况需要程序员根据业务需求开发 Handler。(这里所说的 Handler 就是指我们的 Controller)

4.HandlAdapter:处理器适配器

  • 通过 HandlerAdapter 对处理器进行执行,这是适配器模式的应用,通过扩展处理器适配器,支持更多类型的处理器,调用处理器传递参数等工作。

5.ViewResolver:视图解析器

  • ViewResolver 负责将处理结果生成 View 视图,ViewResolver 首先根据逻辑视图名解析成物理视图名称,即具体的页面地址,再生成 View 视图对象,最后对 View 进行渲染将处理结果通过页面展示给用户。
  • SpringMVC 框架提供了很多的 View 视图类型,包括:jstlView、freemarkerView、pdfView 等。一般情况下需要通过页面标签或页面模版技术将模型数据通过页面展示给用户,需要由程序员根据业务需求开发具体的页面。

四,@RequestMapping注解解析

1,@RequestMapping出现的位置

一个@Controller 所注解的类中,可以定义多个处理器方法。

当然,不同的处理器方法所匹配的 URI 是不同的。这些不同的 URI 被指定在注解于方法之上的@RequestMapping 的value 属性中。

但若这些请求具有相同的 URI 部分,则这些相同的 URI,可以被抽取到注解在类之上的RequestMapping 的 value属性中,此时的这个 URI 表示模块的名称。

URI 的请求是相对于 Web 的根目录。在类的级别上的注解会将一个特定请求或者请求模式映射到一个控制器之上。之后你还可以另外添加方法级别的注解来进一步指定到处理方法的映射关系。

以往使用servlet时,需要对每个请求重写doGet、doPost方法,并在web.xml中进行配置

11-SSM_SpringMVC(上)

11-SSM_SpringMVC(上)

 

11-SSM_SpringMVC(上)

关于servlet的用法可以参考这里​​@&再见萤火虫&【06-JavaWEB_Servlet】​​,温故知新。

springmvc中可以方便的使用注解来完成servlet的功能。

1,在Team类中声明三个方法,add、update、delete

@RequestMapping中的值,都是简单的"方法名+.do"

package com.xxy.controller;

import com.xxy.service.TeamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class TeamController {
// 通过注解的方式自动装配
@Autowired
TeamService teamService;

@RequestMapping("add.do")
public ModelAndView add() {
System.out.println("TeamController-----add");
teamService.add();
ModelAndView mv = new ModelAndView();
mv.addObject("teamName", "万达国际");//相当于request.setAttrubuite("","");
// 未来经过springmvc的视图解析器处理,转换成物理资源路径,相当于request.getRequestDispatcher("index.jsp").forward();
// 经过InternalResourceViewResolver对象的处理之后加上前后缀就变为了 /jsp/index.jsp
mv.setViewName("index");
return mv;
}

@RequestMapping("update.do")
public ModelAndView update() {
System.out.println("TeamController-----update");
teamService.add();
ModelAndView mv = new ModelAndView();
mv.addObject("teamName", "万达国际");
mv.setViewName("team/update");
return mv;
}

@RequestMapping("delete.do")
public ModelAndView delete() {
System.out.println("TeamController-----delete");
teamService.add();
ModelAndView mv = new ModelAndView();
mv.addObject("teamName", "万达国际");
mv.setViewName("team/delete");
return mv;
}
}

 

11-SSM_SpringMVC(上)

2,当类增多时,RequestMapping中的值不可避免的会发生重复,比如Team中的add请求和Player中的add。为了进行区分,可以在请求之前添加与类相关的信息,比如"team/add.do",这样就变成了下面的样子

@RequestMapping("team/add.do")
public ModelAndView add() {

}

@RequestMapping("team/update.do")
public ModelAndView update() {

}

@RequestMapping("team/delete.do")
public ModelAndView delete() {

}

3,发现,所有方法前都有team前缀,这时就可以把共有的前缀提取到类的前面

@Controller
@RequestMapping("team")
public class TeamController {
// 通过注解的方式自动装配
@Autowired
TeamService teamService;

@RequestMapping("add.do")
public ModelAndView add() {

}

@RequestMapping("update.do")
public ModelAndView update() {

}

@RequestMapping("delete.do")
public ModelAndView delete() {

}
}

11-SSM_SpringMVC(上)

2,指定请求提交方式

@RequestMapping的method属性,用来对被注解方法所处理请求的提交方式进行限制,即只有满足method 属性指定的提交方式的请求,才会执行该被注解方法。

Method 属性的取值为 RequestMethod 枚举常量。常用的为 RequestMethod.GET 与RequestMethod.POST,分别表示提交方式的匹配规则为 GET 与 POST 提交(不添加RequestMethod 属性,则GET、POST都可。地址栏提交默认是GET方式。提交方式出错会报405错误)

11-SSM_SpringMVC(上)

11-SSM_SpringMVC(上)

常用提交方式

11-SSM_SpringMVC(上)

五,url-pattern解析&静态资源处理

在web.xml配置SpringMVC的前端控制器时有这个节点。这个节点中的值一般有两种写法:

1,url-pattern解析

1、*.do 

在没有特殊要求的情况下,SpringMVC 的前端控制器 DispatcherServlet 的常使用后辍匹配方 式,可以写为*.do 或者 *.action, *.mvc 等。

2、/ 

可以写为/,但是 DispatcherServlet 会将向静态内容--例如.css、.js、图片等资源的获取请求 时,也会当作是一个普通的 Controller 请求。前端控制器会调用处理器映射器为其查找相应的处理器。肯定找不到,所以所有的静态资源获取请求也均会报 404 错误。

案例:在index.jsp页面添加一张图片,如果节点中的值为*.do,图片可以正常访问,但是如果为/就不能访问。

1,项目中添加图片,同时修改index.jsp页面

这时运行程序,在index页面是可以正常看到图片的

11-SSM_SpringMVC(上)

2,修改web.xml

11-SSM_SpringMVC(上)

3,这时Dispatcher会拦截所有请求,包括图片,但是由于图片属于静态资源,没有为其添加映射(RequestMapping),所以会加载不出来

11-SSM_SpringMVC(上)

2,静态资源访问

如果的值配置为/后,静态资源可以通过以下两种方法解决。

2.1 使用< mvc:default-servlet-handler/ >

在springmvc的配置文件中添加如下内容:

<mvc:default-servlet-handler/>

声 明 了 <mvc:default-servlet-handler /> 后 ,springmvc框架会在容器中创建 DefaultServletHttpRequestHandler处理器对象。

该对象会对所有进入 DispatcherServlet的URL 进行检查。如果发现是静态资源的请求,就将该请求转由Web应用服务器默认的Servlet 处理。 

一般的服务器都有默认的 Servlet。例如使用的Tomcat服务器中,有一个专门用于处理静态资源访问的 Servlet 名叫 DefaultServlet。其<servlet-name/>为default。可以处理各种静态资源访问请求。

该Servlet注册在 Tomcat 服务器的 web.xml 中。在 Tomcat安装目录/conf/web.xml。

11-SSM_SpringMVC(上)

2.2 使用 < mvc:resources/ >

在springmvc的配置文件中添加如下内容:

<mvc:resources location="/images/" mapping="/images/**" />
  • location: 表示静态资源所在目录。当然,目录不要使用/WEB-INF/及其子目录。 
  • mapping: 表示对该资源的请求。注意,后面是两个星号**。

在 Spring3.0 版本后,Spring 定义了专门用于处理静态资源访问请求的处理器ResourceHttpRequestHandler。并且添加了< mvc:resources/ >标签,专门用于解决静态资源无法访问问题。

六,处理器方法的参数

处理器方法可以包含以下四类参数,这些参数会在系统调用时由系统自动赋值.所以我们可以在方法内直接使用。以下是这四类参数:

  • HttpServletRequest
  • HttpServletResponse
  • HttpSession
  • 请求中所携带的请求参数

准备工作:创建新的控制器ParamController.java和前端页面hello.jsp页面

1,直接使用方法的参数逐个接收

直接使用方法的参数逐个接收:方法的参数名称必须与用户请求中携带的参数名称保持一致,否则就获取不到,默认设置为空值。

1,编写前端页面hello.jsp

<body>
<h3>1、直接使用方法的参数逐个接收</h3>
<form action="/param/test01" method="post">
球队id:<input type="text" name="teamId"/><br/>
球队名称:<input type="text" name="teamName"/><br/>
球队位置:<input type="text" name="teamLocation"/><br/>
<button type="submit">提交</button>
</form>
</body>

2,在ParamController.java中添加处理请求的方法

/**
* 1、直接使用方法的参数逐个接收:方法的参数名称必须与用户请求中携带的参数名称保持一致,否则就获取不到
* 好处:不需要类型转换
*/
@RequestMapping("test01")
public ModelAndView test01(Integer teamId,String teamName,String teamLocation){
System.out.println("test01-----------------");
System.out.println(teamId);
System.out.println(teamName);
System.out.println(teamLocation);
return new ModelAndView("ok");
}

2,使用对象接收多个参数

使用对象接收多个参数:要求用户请求中携带的参数名称必须是实体类中的属性保持一致,否则就获取不到,默认设置为空值。

1,编写Team类

public class Team {
private Integer teamId;
private String teamName;
private String location;
@DateTimeFormat(pattern = "yyyy-MM-dd")
@JsonFormat(pattern = "yyyy-MM-dd")
private Date createTime;

@Override
public String toString() {
return "Team{" +
"teamId=" + teamId +
", teamName='" + teamName + '\'' +
", location='" + location + '\'' +
", createTime=" + createTime +
'}';
}

public Date getCreateTime() {
return createTime;
}

public void setCreateTime(Date createTime) {
this.createTime = createTime;
}

public Integer getTeamId() {
return teamId;
}

public void setTeamId(Integer teamId) {
this.teamId = teamId;
}

public String getTeamName() {
return teamName;
}

public void setTeamName(String teamName) {
this.teamName = teamName;
}

public String getLocation() {
return location;
}

public void setLocation(String location) {
this.location = location;
}
}

2,修改jsp页面

<h3>2、使用对象接收多个参数</h3>
<form action="/param/test02" method="post">
球队id:<input type="text" name="teamId"/><br/>
球队名称:<input type="text" name="teamName"/><br/>
球队位置:<input type="text" name="location"/><br/>
<button type="submit">提交</button>
</form>

3,在Controller中添加对应的处理请求方法

//2、使用对象接收多个参数:要求用户请求中携带的参数名称必须是实体类中的属性保持一致,否则就获取不到
@RequestMapping("test02")
public ModelAndView test02(Team team){
System.out.println("test02-----------------");
System.out.println(team);
return new ModelAndView("ok");
}

3,请求参数和方法名称的参数不一致

在处理请求的方法中使用RequestParam对参数名进行矫正。但是当jsp中的参数与RequestParam没有对应时,由于默认是必须存在的,所以会报错,避免报错可以添加required属性,并设置为false

1,jsp页面

<h3>3、请求参数和方法名称的参数不一致</h3>
<form action="/param/test03" method="post">
球队id:<input type="text" name="teamId"/><br/>
球队名称:<input type="text" name="teamName"/><br/>
球队位置:<input type="text" name="location"/><br/>
<button type="submit">提交</button>
</form>

2,修改controller中的方法

//3、请求参数和方法名称的参数不一致:使用@RequestParam进行矫正,
// value属性表示请求中的参数名称
// required属性表示参数是否是必须的:true:必须赋值,否则报出400错;false:可以不赋值,结果就是null
@RequestMapping("test03")
public ModelAndView test03(@RequestParam(value = "teamId",required = false) Integer id,
@RequestParam(value = "teamName",required = true) String name,
@RequestParam("location") String loc){
System.out.println("test03-----------------");
System.out.println(id);
System.out.println(name);
System.out.println(loc);
return new ModelAndView("ok");
}

4,使用HttpServletRequest 对象获取参数

1,jsp页面

<h3>4、使用HttpServletRequest 对象获取参数</h3>
<form action="/param/test04" method="post">
球队id:<input type="text" name="teamId"/><br/>
球队名称:<input type="text" name="teamName"/><br/>
球队位置:<input type="text" name="location"/><br/>
<button type="submit">提交</button>
</form>

2,修改Controller中的方法

//4、使用HttpServletRequest 对象获取参数:跟原来的javaWeb项目中使用的方式是一样的
@RequestMapping("test04")
public ModelAndView test04(HttpServletRequest request){
System.out.println("test04-----------------");
String teamId = request.getParameter("teamId");
String teamName = request.getParameter("teamName");
String location = request.getParameter("location");
if(teamId!=null)
System.out.println(Integer.valueOf(teamId));
System.out.println(teamName);
System.out.println(location);
return new ModelAndView("ok");
}

5,直接使用URL地址传参@PathVariable

1,直接使用地址传参

http://localhost:8080/param/test05/1001/lacker/las

2,修改Controller中的方法

//5、直接使用URL地址传参: 借助@PathVariable 注解
// 例如http://localhost:8080/param/test05/1001/lacker/las
@RequestMapping("test05/{id}/{name}/{loc}")
public ModelAndView test05(@PathVariable("id") Integer teamId,
@PathVariable("name") String teamName,
@PathVariable("loc") String teamLocation){
System.out.println("test05-----------------");
System.out.println(teamId);
System.out.println(teamName);
System.out.println(teamLocation);
return new ModelAndView("ok");
}

6,获取日期类型的参数

1,借助对象的日期属性来获取日期参数。修改Team类,添加Date属性(之前给出的Team类中已经有了该属性)

11-SSM_SpringMVC(上)

2,修改jsp页面

<h3>6、获取日期类型的参数</h3>
<form action="/param/test06" method="post">
球队id:<input type="text" name="teamId"/><br/>
球队名称:<input type="text" name="teamName"/><br/>
球队位置:<input type="text" name="location"/><br/>
创建日期:<input type="text" name="createTime"/><br/>
<button type="submit">提交</button>
</form>

3,修改Controller中的方法

//6、获取日期类型的参数:实体类中的对应的属性上必须有注解 @DateTimeFormat(pattern = "yyyy-MM-dd")
@RequestMapping("test06")
public ModelAndView test06(Team team){
System.out.println("test06-----------------");
System.out.println(team);
return new ModelAndView("ok");
}

7,获取数组类型的参数

1,修改jsp页面

<h3>7、获取数组类型的参数</h3>
<form action="/param/test07" method="post">
球队名称1:<input type="text" name="teamName"/><br/>
球队名称2:<input type="text" name="teamName"/><br/>
球队名称3:<input type="text" name="teamName"/><br/>
<button type="submit">提交</button>
</form>

2,修改Controller中的方法

//7、获取数组类型的参数
@RequestMapping("test07")
public ModelAndView test07(String[] teamName,HttpServletRequest request){
System.out.println("test07-----------------");
//方式1:
for (String s : teamName) {
System.out.println(s);
}
System.out.println("---------------");
//方式2:
String[] teamNames = request.getParameterValues("teamName");
for (String name : teamNames) {
System.out.println(name);
}
return new ModelAndView("ok");
}

8,获取集合类型的参数

8.1 集合中元素为普通类型

1,修改jsp

<h3>8、获取集合类型的参数</h3>
<form action="/param/test08" method="post">
球队名称1:<input type="text" name="teamName"/><br/>
球队名称2:<input type="text" name="teamName"/><br/>
球队名称3:<input type="text" name="teamName"/><br/>
<button type="submit">提交</button>
</form>

2,传递集合类型时,需要添加@RequestParam注解,否则会报错(单纯的数组类型,比如String[] 不需要添加注解)

//8、获取集合类型的参数: 简单类型的可以通过@RequestParam注解实现;对象集合不支持直接获取,必须封装在类中,作为一个属性操作
@RequestMapping("test08")
public ModelAndView test08(@RequestParam("teamName") List<String> nameList){
System.out.println("test08-----------------");
for (String s : nameList) {
System.out.println(s);
}
return new ModelAndView("ok");
}

8.2 集合中元素为对象

1,新编写一个类QueryVO,封装对象的集合

public class QueryVO {
private List<Team> teamList;

public List<Team> getTeamList() {
return teamList;
}

public void setTeamList(List<Team> teamList) {
this.teamList = teamList;
}
}

2,修改jsp页面

<form action="/param/test09" method="post">
球队id1:<input type="text" name="teamList[0].teamId"/><br/>
球队id2:<input type="text" name="teamList[1].teamId"/><br/>
球队id3:<input type="text" name="teamList[2].teamId"/><br/>
球队名称1:<input type="text" name="teamList[0].teamName"/><br/>
球队名称2:<input type="text" name="teamList[1].teamName"/><br/>
球队名称3:<input type="text" name="teamList[2].teamName"/><br/>
<button type="submit">提交</button>
</form>

3,修改Controller

@RequestMapping("test09")
public ModelAndView test09(QueryVO vo){
System.out.println("test09-----------------");
for (Team team : vo.getTeamList()) {
System.out.println(team);
}
return new ModelAndView("ok");
}

七,中文乱码的解决方案

对于前面所接收的请求参数,若含有中文,则会出现中文乱码问题。Spring 对于请求参数中的中文乱码问题,给出了专门的字符集过滤器CharacterEncodingFilter 类。如图所示。

11-SSM_SpringMVC(上)

1,解决方案

在 web.xml 中注册字符集过滤器,推荐将该过滤器注册在其它过滤器之前。因为过滤器的执行是按照其注册顺序进行的。

在web.xml配置文件直接注册字符集(其中forceRequestEncoding、forceResponseEncoding为true,表示即使controller中添加了对字符集的约束,也仍按照这里设定的编码格式)

<!--配置中文乱码的过滤-->
<filter>
<filter-name>characterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceRequestEncoding</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>forceResponseEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern><!--过滤所有请求-->
</filter-mapping>

3,原理

11-SSM_SpringMVC(上)

 八,处理器方法的返回值

使用@Controller 注解的处理器的处理器方法,其返回值常用的有四种类型:

  • 1. ModelAndView
  • 2. String
  • 3. 返回自定义类型对象
  • 4. 无返回值 void

根据实际的业务选择不同的返回值。

1,案例准备

1,创建控制器ResultController

package com.kkb.controller;

import com.kkb.pojo.Team;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.time.temporal.Temporal;
import java.util.*;

@Controller
@RequestMapping("result")
public class ResultController {
}

2,在jsp文件夹中添加页面result.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>result</title>
<script src="/js/jquery-1.11.1.js"></script>
</head>
<body>
<h1>result----------------</h1>
</body>
</html>

2,返回ModelAndView

如果是前后端不分的开发,大部分情况下,我们返回 ModelAndView,即数据模型+视图,Model 中放我们的数据,然后在 ModelAndView 中指定视图名称。

控制器ResultController.java中添加方法:

//1、返回值是ModelAndView: 这种方式既有数据的携带还有资源的跳转,可以选择该种方式
@RequestMapping("test01")
public ModelAndView test01(){
ModelAndView mv=new ModelAndView();//模型与视图
//携带数据
mv.addObject("teamName","湖人队");//相当于request.setAttribute("teamName","湖人队“);
mv.setViewName("result");// 经过视图解析器InternalResourceViewResolver的处理,将逻辑视图名称加上前后缀变为物理资源路径 /jsp/result.jsp
return mv;
}

当处理器方法处理完后,需要跳转到其它资源的同时传递数据,选择返回 ModelAndView 比较好,但是如果只是需要传递数据或者跳转之一,这个时候ModelAndView 就不是最优选择。

result.jsp页面中添加如下内容:

<h3>test01------${teamName}---</h3>

3,返回String

上一种方式中的 ModelAndView 可以拆分为两部分,Model 和 View,在 SpringMVC 中,Model 可以直接在参数中指定,然后返回值是逻辑视图名,视图解析器解析可以将逻辑视图名称转换为物理视图地址。

11-SSM_SpringMVC(上)

视图解析器通过内部资源视图解析器InternalResourceViewResolver将字符串与解析器中的prefix和suffix结合形成要跳转的额URI。

控制器ResultController.java中添加方法:

//2、返回字符串
@RequestMapping("test02")
public String test02(HttpServletRequest request){
Team team=new Team();
team.setLocation("迈阿密");
team.setTeamId(1002);
team.setTeamName("热火");
//携带数据
request.setAttribute("team",team);
request.getSession().setAttribute("team",team);
//资源的跳转
return "result";// 经过视图解析器InternalResourceViewResolver的处理,将逻辑视图名称加上前后缀变为物理资源路径 /jsp/result.jsp
}

result.jsp中添加如下内容:

<h3>test02---request作用域获取:---${requestScope.team.teamName}---${requestScope.team.teamId}---${requestScope.team.location}</h3>
<h3>test02---session作用域获取:---${sessionScope.team.teamName}---${sessionScope.team.teamId}---${sessionScope.team.location}</h3>

11-SSM_SpringMVC(上)

4,返回对象类型@ResponseBody

当处理器方法返回Object对象类型的时候,可以是Integer、String、Map、List,也可以是自定义的对象类型。

但是无论是什么类型,都不是作为逻辑视图出现,而是直接作为数据返回然后展示的。

一般前端发起Ajax请求的时候都会使用直接返回对象的形式。返回对象的时候,需要使用@ResponseBody 注解(可以添加在方法或类级别上),将转换后的 JSON 数据放入到响应体中。

pom.xml文件中添加两个依赖:

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

4.1 返回基础类型

控制器ResultController.java中添加方法:

//3、返回对象类型:Integer Double String  自定义类型   List  Map  返回的不是逻辑视图的名称,而直接就是数据返回 ,一般是ajax请求搭配使用 ,将json格式的数据直接返回给响应体
// 一定要与@ResponseBody
@ResponseBody
@RequestMapping("test03-1")
public Integer test031(){
return 666;
}

@ResponseBody
@RequestMapping("test03-2")
public String test032(){
return "test";
}

4.2 返回自定义类型

控制器ResultController.java中添加方法:

@ResponseBody
@RequestMapping("test03-3")
public Team test033(){
Team team=new Team();
team.setLocation("迈阿密");
team.setTeamId(1002);
team.setTeamName("热火");
team.setCreateTime(new Date());
return team;
}

项目中引入jQuery,result.jsp内容如下:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>result</title>
<script src="/js/jquery-1.11.1.js"></script>
</head>
<body>
<h1>result----------------</h1>
<h3>test01------${teamName}---</h3>
<h3>test02---request作用域获取:---${requestScope.team.teamName}---${requestScope.team.teamId}---${requestScope.team.location}</h3>
<h3>test02---session作用域获取:---${sessionScope.team.teamName}---${sessionScope.team.teamId}---${sessionScope.team.location}</h3>

<div>
<button type="button" >ajax请求自定义对象</button>
<h3>ajax请求自定义对象的结果展示</h3>
<p ></p>
</div>
<script>
$(function(){
$("#btn1").click(function () {
$.ajax({
type: "POST",
url: "/result/test03-3",
data: "",
success: function(msg){
alert( "Data Saved: " + msg );
var name=msg.teamName;
var id=msg.teamId;
var loc=msg.location;
$("#res").html("name:"+name+",id:"+id+",location:"+loc);
}
});
});
});
</script>
</body>
</html>

11-SSM_SpringMVC(上)

4.3 返回列表List

1,控制器ResultController.java中添加方法

@ResponseBody
@RequestMapping("test03-4")
public List<Team> test034(){
List<Team> list=new ArrayList<>(5);
for(int i=1;i<=5;i++) {
Team team = new Team();
team.setLocation("迈阿密"+i);
team.setTeamId(1002+i);
team.setTeamName("热火"+i);
team.setCreateTime(new Date());
list.add(team);
}
return list;
}

2,result.jsp中添加如下内容:

<button type="button" >ajax请求List</button>
<h3>ajax请求List的结果展示</h3>
<p ></p>

script标签中添加如下代码:

$("#btn2").click(function () {
$.ajax({
type: "POST",
url: "/result/test03-4",
data: "",
success: function(list){
alert( "Data Saved: " + list );
var str="";
for(var i=0;i<list.length;i++){
var obj=list[i];
str+="name:"+obj.teamName+",id:"+obj.teamId+",location:"+obj.location+"<br/>";
}
$("#res2").html(str);
}
});
});

11-SSM_SpringMVC(上)

4.4 返回集合Map

1,控制器ResultController.java中添加方法

@ResponseBody
@RequestMapping("test03-5")
public Map<String,Team> test035(){
Map<String,Team> map=new HashMap();
for(int i=1;i<=5;i++) {
Team team = new Team();
team.setLocation("金州"+i);
team.setTeamId(1000+i);
team.setTeamName("勇士"+i);
team.setCreateTime(new Date());
map.put(team.getTeamId()+"",team);
}
return map;
}

2,script标签中添加如下代码:

<button type="button" >ajax请求Map</button>
<h3>ajax请求Map的结果展示</h3>
<p ></p>
$("#btn3").click(function () {
$.ajax({
type: "POST",
url: "/result/test03-5",
data: "",
success: function(map){
alert( "Data Saved: " + map );
var str="";
$.each(map,function (i,obj) {
str+="name:"+obj.teamName+",id:"+obj.teamId+",location:"+obj.location+"<br/>";
});
$("#res3").html(str);
}
});
});

5,无返回值 void-了解(原生的处理方式)

方法的返回值为 void,并不一定真的没有返回值,我们可以通过其他方式给前端返回。实际上,这种方式也可以理解为 Servlet 中的的处理方案。

1,控制器ResultController.java中添加方法

//4、没有返回void : 其实就是原生的servlet中的使用方式
//转发 重定向
@RequestMapping("test04-1")
public void test041(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("直接使用HttpServletRequest进行服务器端的转发");
request.getRequestDispatcher("/jsp/ok.jsp").forward(request,response);
}

@RequestMapping("test04-2")
public void test042(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("直接使用HttpServletResponse重定向跳转");
response.sendRedirect("/jsp/ok.jsp");
}

@RequestMapping("test04-3")
public void test043(HttpServletResponse response) throws ServletException, IOException {
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
PrintWriter writer = response.getWriter();
writer.write("返回void类型测试---直接返回字符串");
writer.flush();
writer.close();
}
@RequestMapping("test04-4")
public void test044(HttpServletResponse response) throws ServletException, IOException {
response.setStatus(302);//设置响应码,302表示重定向
response.setHeader("Location","/jsp/ok.jsp");
}

2,直接在地址栏中国输入test04-1、test04-2、test04-3、test04-4即可看到效果