SSM_SpringMVC(上)

目录

一:SpringMVC概述

1.1 SpringMVC概念

1.2 SpringMVC原理

1.3 SpringMVC优势 

2、MVC模式回顾 

3、入门程序

 3.1 创建一个maven项目

 3.2 在pom文件中添加依赖

 3.3 创建Spring和SpringMVC的配置文件

 3.4 在web.xml中进行Spring和SpringMVC配置

 3.5 编写业务代码

 3.6 创建前端页面

 3.7 运行 

 3.8 解析

4.SpringMVC工作流程

4.1 工作流程分析 

4.2 SpringMVC组件 

5. @RequestMapping 注解 

 5.1@RequestMapping出现的位置

 5.2 指定请求提交方式

 5.3 补充url-pattern解析

 6.处理器方法的参数

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

 6.2 使用对象接收多个参数

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

 6.4 使用HttpServletRequest 对象获取参数

 6.5 直接使用URL地址传参 

 6.6 获取日期类型的参数 

 6.7 获取数组类型的参数 

 6.8 获取集合类型的参数

7、请求参数中文乱码 

 7.1 乱码解决方案

 7.2 解决方案原理 

 8、处理器方法的返回值

 8.1 案例准备

 8.2 返回ModelAndView

 8.3 返回String

 8.4 返回对象类型 @RequestBody

 8.5 无返回值 void-了解


一:SpringMVC概述

1.1 SpringMVC概念

SpringMVC 也叫 Spring web mvc。是 Spring内置的一个MVC框架,在 Spring3.0 后发布。SpringMVC框架解决了WEB开发中常见的问题(参数接收、文件上传、表单验证等等),而且使用简单,与Spring无缝集成。支持 RESTful风格的URL请求。采用了松散耦合可插拔组件结构,比其他 MVC 框架更具扩展性和灵活性.

1.2 SpringMVC原理

在没有使用SpringMVC之前我们都是使用Servlet在做Web开发。但是使用Servlet开发在接收请求参数,数据共享,页面跳转等操作相对比较复杂。servlet是java进行web开发的标准,既然springMVC是对servlet的封装,那么很显然SpringMVC底层就是Servlet,SpringMVC就是对Servlet进行深层次的封装。 

1.3 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
                 View: 视图 JSP技术 负责收集和展示数据
                 Controller: 控制器 servlet技术 中间调度

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

3、入门程序

3.1 创建一个maven项目

(1) 创建工程

(2)修改工程名字及GroupId 

(3)在pom文件中添加打包方式

 (4)接下来在项目中添加其他配置:

 

 当你如图操作后 你会显示和我一样的文件

(5)接下来补齐目录:

 3.2 在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.lxy</groupId>
    <artifactId>SpringMVC_test</artifactId>
    <version>1.0-SNAPSHOT</version>

    <packaging>war</packaging>

    <dependencies>
        <!--spring-webmvc依赖-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.2.13.RELEASE</version>
        </dependency>
        <!--springmvc底层还是servlet,所以必须添加serlvet依赖-->
        <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-->
                    <source>1.8</source>
                    <target>1.8</target>
                </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>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

</project>

 3.3 创建Spring和SpringMVC的配置文件

我们一般将除了 Controller 之外的所有 Bean 注册到 Spring 容器中,而将 Controller 注册到
SpringMVC 容器中。所以我们在 resources 目录下添加 applicationContext.xml 作为 spring 的配置,添加springmvc.xml作为springmvc的配置文件。

 (1) 创建Spring配置文件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"
    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
    ">

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

(2) 创建SpringMVC的配置文件springmvc.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: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/beans/spring-context.xsd
       http://www.springframework.org/schema/mvc
       http://www.springframework.org/schema/beans/spring-mvc.xsd">

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

 3.4 在web.xml中进行Spring和SpringMVC配置

<?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>
        <!--contextConfigLocation:表示用于加载 Bean的配置文件-->
        <param-name>contextConfigLocation</param-name>
        <!--指定spring配置文件的位置
            这个配置文件也有一些默认规则,它的配置文件名默认就叫 applicationContext.xml ,
            如果将这个配置文件放在 WEB-INF 目录下,那么这里就可以不用 指定配置文件位置,
            只需要指定监听器就可以。
            这段配置是 Spring 集成 Web 环境的通用配置;一般用于加载除控制器层的 Bean(如
            dao、service 等),以便于与其他任何Web框架集成。
            -->
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!--SpringMVC的配置-->
        <!--
            前端控制器:所有的请求都会经过此控制器,然后通过此控制器分发到各个分控制器.
            前端控制器本质上还是一个Servlet,因为SpringMVC底层就是使用Servlet编写的
        -->
    <servlet>
        <!--名称默认为类名首字母小写的形式-->
        <servlet-name>dispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!-- 创建前端控制器的时候读取springmvc配置文件启动ioc容器 -->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:springmvc.xml</param-value>
        </init-param>
        <!-- Tomcat启动就创建此对象 -->
        <!--大于0表示web服务器启动后立即创建对象,数字越小优先级越高-->
        <load-on-startup>1</load-on-startup>
    </servlet>
    <!-- 配置拦截路径url,所有以.do结尾的请求都会被前端控制器拦截处理 -->
    <servlet-mapping>
        <servlet-name>dispatcherServlet</servlet-name>
        <url-pattern>*.do</url-pattern>
    </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.5 编写业务代码

(1)编写TeamService

package com.lxy.service;

import org.springframework.stereotype.Service;

@Service
public class TeamService {

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

(2)编写TeamController 

package com.lxy.controller;

import com.lxy.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.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("team")
public class TeamController {
    //通过注解方式自动装配
    @Autowired
    private TeamService teamService;
    
    @RequestMapping("hello.do")
    public ModelAndView hello(){
        System.out.println("TeamController----add---");
        teamService.add();
        ModelAndView mv = new ModelAndView();
        mv.addObject("teamName","湖人队");//相当于request.setAttribute("teamName","湖人")
        mv.setViewName("index"); //未来经过springmvc的视图处理器,转换成物理资源路径 相当于request.getDispatcher("index.jsp").forward(req,resp)
        //经过InternalResourceViewResolver对象的处理后加上前缀就变成了  /jsp/index.jsp
        return mv;
    }
}

 (3)在springmvc.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: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">

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

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

    <!--试图解析器-->
    <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/jsp/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
</beans>

3.6 创建前端页面

因为前面TeamController中hello方法  最后通过试图解析器 传给了前端页面,

目录为/jsp/index.jsp,所以我们需要在webapp下创建一个jsp目录,然后在jsp目录中添加一个index.jsp页面。

<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2022-04-22 0022
  Time: 下午 01:41:03
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head>
    <title>index</title>
  </head>
  <body>
     <!--这里用来接收前面保存的值-->
     <h1>index----------${teamName}</h1>  
  </body>
</html>

3.7 运行 

 

控制台显示: 

 

打开网站,输入网址,因为pom文件中tomcat的端口号为8080,所以网址为:

http://localhost:8080/team/hello.do

 如果你跟我显示一样的话,那么恭喜你,第一次运行成功。如果有错误可以在下方评论题问,我很快就会给出回复。

3.8 解析

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

 

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

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


2.SpringMVC容器中可以扫描所有Bean嘛?
可以。可以在SpringMVC容器中扫描所有Bean。但是实际开发中一般不会这么做,原因如下:
(1)为了方便配置文件的管理
(2)未来在 Spring+SpringMVC+Mybatis组合中,要写的配置内容很多,一般都会根据功  能分开编写

4.SpringMVC工作流程

4.1 工作流程分析 

(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)前端控制器响应浏览器。

4.2 SpringMVC组件 

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 等。一般情况下需要通过页面标签或页面模版技术将模型数据通过页面展示给用户,需要由程序员根据业务需求开发具体的页面 

5. @RequestMapping 注解 

5.1@RequestMapping出现的位置

@RequestMapping 注解定义了处理器对于请求的映射规则。该注解可以定义在类上,也可以定义在方法上,但是含义不同。

 一个@Controller 所注解的类中,可以定义多个处理器方法。当然,不同的处理器方法所匹配的 URI 是不同的。这些不同的 URI 被指定在注解于方法之上的@RequestMapping 的value 属性中。但若这些请求具有相同的 URI 部分,则这些相同的 URI,可以被抽取到注解在类之上的RequestMapping 的 value属性中。此时的这个 URI 表示模块的名称。URI 的请求是相对于 Web 的根目录。在类的级别上的注解会将一个特定请求或者请求模式映射到一个控制器之上。之后你还可以另外添加方法级别的注解来进一步指定到处理方法的映射关系。

示例:

 在TeamController类中声明两个个方法,add、update

package com.lxy.controller;

import com.lxy.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.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

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

    @RequestMapping(value = "add.do")
    public ModelAndView add(){
        System.out.println("TeamController----addTeam---");
        ModelAndView mv = new ModelAndView();
        mv.setViewName("team/add"); //映射成为物理资源路径 /jsp/team/add.jsp
        return mv;
    }

    @RequestMapping(value = "update.do")
    public ModelAndView update(){
        System.out.println("TeamController----updateTeam---");
        ModelAndView mv = new ModelAndView();
        mv.setViewName("team/update");  //映射成为物理资源路径 /jsp/team/update.jsp
        return mv;
    }

    @RequestMapping("hello.do")
    public ModelAndView hello(){
        System.out.println("TeamController----add---");
        teamService.add();
        ModelAndView mv = new ModelAndView();
        mv.addObject("teamName","湖人队");//相当于request.setAttribute("teamName","湖人")
        mv.setViewName("index"); //未来经过springmvc的视图处理器,转换成物理资源路径 相当于request.getDispatcher("index.jsp").forward(req,resp)
        //经过InternalResourceViewResolver对象的处理后加上前缀就变成了  /jsp/index.jsp
        return mv;
    }
}

分别创建两个方法所对应的jsp

按照上述方式运行程序 打开网址

 

 5.2 指定请求提交方式

@RequestMapping的method属性,用来对被注解方法所处理请求的提交方式进行限制,即只有满足method 属性指定的提交方式的请求,才会执行该被注解方法。
Method 属性的取值为 RequestMethod 枚举常量。常用的为 RequestMethod.GET 与
RequestMethod.POST,分别表示提交方式的匹配规则为 GET 与 POST 提交。

 以下表格列出了常用的提交方式:

 5.3 补充url-pattern解析

在webapp/WEB-INF/web.xml中的springmvc的请求路径

 5.3.1 url-pattern解析:

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

1、*.do
在没有特殊要求的情况下,SpringMVC 的前端控制器 DispatcherServlet 的常使用后辍匹配方式,可以写为*.do 或者 *.action, *.mvc 等。
2、/
可以写为/,但是 DispatcherServlet 会将向静态内容--例如.css、.js、图片等资源的获取请求
时,也会当作是一个普通的 Controller 请求。前端控制器会调用处理器映射器为其查找相应的处理器。肯定找不到啊,所以所有的静态资源获取请求也均会报 404 错误。

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

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

(2)修改web.xml

 (3)此时访问图片无法显示

 5.3.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。-->

 (2)使用 < mvc:resources/ >

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

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

 解释:

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

 

 6.处理器方法的参数

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

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

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

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

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

(1)编写前端页面hello.jsp 和 ok.jsp(这个用于成功跳转)

<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2022-04-22 0022
  Time: 下午 04:02:10
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>hello</title>
</head>
<body>
   <h3>1.直接使用方法的参数逐个接收</h3>
   <form action="/param/test01.do" 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>
</html>
<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2022-04-22 0022
  Time: 下午 04:07:52
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>ok</title>
</head>
<body>
 <h3>ok---------------------${requestScope.teamName}</h3>
 <h2>从地址栏中获取的参数值: teamName=${param.teamName},teamId=${param.teamId}</h2>
</body>
</html>

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

package com.lxy.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("param")
public class ParamController {

    /**
     * 直接使用方法的参数逐个接收: 方法的参数名称必须与用户请求中携带的参数名称保持一致,否则就获取不到
     */
    @RequestMapping("test01.do")
    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");
    }

    @RequestMapping("hello.do")
    public ModelAndView hello(){
        return new ModelAndView("hello");
    }
}

 

 

 ${param.teamName} 相当于request.getParameter("teamName") 

6.2 使用对象接收多个参数

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

(1)编写Team类

package com.lxy.pojo;

import java.util.Date;

public class Team {
    private Integer teamId;
    private String teamName;
    private String teamLocation;
    private Date createTime;

    @Override
    public String toString() {
        return "Team{" +
                "teamId=" + teamId +
                ", teamName='" + teamName + '\'' +
                ", teamLocation='" + teamLocation + '\'' +
                ", 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 getTeamLocation() {
        return teamLocation;
    }

    public void setTeamLocation(String teamLocation) {
        this.teamLocation = teamLocation;
    }
}

(2)修改hello.jsp页面

<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2022-04-22 0022
  Time: 下午 04:02:10
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>hello</title>
</head>
<body>
    <h3>2.使用对象接收多个参数</h3>
    <form action="/param/test02.do" 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>

   <h3>1.直接使用方法的参数逐个接收</h3>
   <form action="/param/test01.do" 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>
</html>

(3)在controller中添加对应的请求方法

package com.lxy.controller;

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

@Controller
@RequestMapping("param")
public class ParamController {

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

    /**
     * 直接使用方法的参数逐个接收: 方法的参数名称必须与用户请求中携带的参数名称保持一致,否则就获取不到
     */
    @RequestMapping("test01.do")
    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");
    }

    @RequestMapping("hello.do")
    public ModelAndView hello(){
        return new ModelAndView("hello");
    }
}

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

3.请求参数和方法名的参数不一样:使用@RequestParam进行矫正
value属性表示请求中的参数名
required属性表示参数是否是必须的:true:必须赋值, 否则报出400错误; false: 可以不赋值,

 (1)修改hello.jsp页面

<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2022-04-22 0022
  Time: 下午 04:02:10
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>hello</title>
</head>
<body>

    <h3>3.请求参数和方法名称的参数不一致</h3>
    <form action="/param/test03.do" 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>

    <h3>2.使用对象接收多个参数</h3>
    <form action="/param/test02.do" 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>

   <h3>1.直接使用方法的参数逐个接收</h3>
   <form action="/param/test01.do" 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>
</html>

 (2)在controller中添加对应的请求方法

package com.lxy.controller;

import com.lxy.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.servlet.ModelAndView;

@Controller
@RequestMapping("param")
public class ParamController {

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

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

    /**
     * 直接使用方法的参数逐个接收: 方法的参数名称必须与用户请求中携带的参数名称保持一致,否则就获取不到
     */
    @RequestMapping("test01.do")
    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");
    }

    @RequestMapping("hello.do")
    public ModelAndView hello(){
        return new ModelAndView("hello");
    }
}

如果将 @RequestParam(value = "teamName",required = true)中teamName改为teamName1,那么就没对应上,这是要求必须赋值,那么就会报错400.

 6.4 使用HttpServletRequest 对象获取参数

 (1)修改hello.jsp页面

<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2022-04-22 0022
  Time: 下午 04:02:10
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>hello</title>
</head>
<body>
    <h3>4.使用HttpServletRequest 对象获取参数</h3>
    <form action="/param/test04.do" 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>

    <h3>3.请求参数和方法名称的参数不一致</h3>
    <form action="/param/test03.do" 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>

    <h3>2.使用对象接收多个参数</h3>
    <form action="/param/test02.do" 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>

   <h3>1.直接使用方法的参数逐个接收</h3>
   <form action="/param/test01.do" 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>
</html>

 (2)在controller中添加对应的请求方法

package com.lxy.controller;

import com.lxy.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.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

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

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

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

    /**
     * 直接使用方法的参数逐个接收: 方法的参数名称必须与用户请求中携带的参数名称保持一致,否则就获取不到
     */
    @RequestMapping("test01.do")
    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");
    }

    @RequestMapping("hello.do")
    public ModelAndView hello(){
        return new ModelAndView("hello");
    }
}

6.5 直接使用URL地址传参 

使用新注释@PathVariable来获取地址栏中的值

 (2)修改controller中的方法

package com.lxy.controller;

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

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("param")
public class ParamController {

    //5.直接使用URL地址传参:
    @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");
    }
    //4.使用HttpServletRequest 对象获取参数: 跟原来的javaWeb项目中使用的方法是一样的
    @RequestMapping("test04.do")
    public ModelAndView test04(HttpServletRequest request){
        System.out.println("test04---------------");
        String teamId = request.getParameter("teamId");
        String teamName = request.getParameter("teamName");
        String teamLocation = request.getParameter("teamLocation");
        if(teamId!=null){
            System.out.println(Integer.parseInt(teamId));
        }
        System.out.println(teamId);
        System.out.println(teamName);
        System.out.println(teamLocation);
        return new ModelAndView("ok");
    }

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

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

    /**
     * 直接使用方法的参数逐个接收: 方法的参数名称必须与用户请求中携带的参数名称保持一致,否则就获取不到
     */
    @RequestMapping("test01.do")
    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");
    }

    @RequestMapping("hello.do")
    public ModelAndView hello(){
        return new ModelAndView("hello");
    }
}

(2)使用传参地址来实验

http://localhost:8080/param/test05/1001/li/shanghai 

6.6 获取日期类型的参数 

(1)修改hello.jsp页面

<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2022-04-22 0022
  Time: 下午 04:02:10
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>hello</title>
</head>
<body>

    <h3>6.获取日期类型的参数</h3>
    <form action="/param/test06.do" method="post">
        球队id:<input type="text" name="teamId"/><br>
        球队名称:<input type="text" name="teamName"/><br>
        球队位置:<input type="text" name="teamLocation"/><br>
        创建日期:<input type="text" name="createTime"><br>
        <button type="submit">提交</button>
    </form>
    
    <h3>4.使用HttpServletRequest 对象获取参数</h3>
    <form action="/param/test04.do" 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>

    <h3>3.请求参数和方法名称的参数不一致</h3>
    <form action="/param/test03.do" 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>

    <h3>2.使用对象接收多个参数</h3>
    <form action="/param/test02.do" 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>

   <h3>1.直接使用方法的参数逐个接收</h3>
   <form action="/param/test01.do" 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>
</html>

 (2)在controller中添加对应的请求方法

package com.lxy.controller;

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

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("param")
public class ParamController {

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

    //5.直接使用URL地址传参:
    @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");
    }
    //4.使用HttpServletRequest 对象获取参数: 跟原来的javaWeb项目中使用的方法是一样的
    @RequestMapping("test04.do")
    public ModelAndView test04(HttpServletRequest request){
        System.out.println("test04---------------");
        String teamId = request.getParameter("teamId");
        String teamName = request.getParameter("teamName");
        String teamLocation = request.getParameter("teamLocation");
        if(teamId!=null){
            System.out.println(Integer.parseInt(teamId));
        }
        System.out.println(teamId);
        System.out.println(teamName);
        System.out.println(teamLocation);
        return new ModelAndView("ok");
    }

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

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

    /**
     * 直接使用方法的参数逐个接收: 方法的参数名称必须与用户请求中携带的参数名称保持一致,否则就获取不到
     */
    @RequestMapping("test01.do")
    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");
    }

    @RequestMapping("hello.do")
    public ModelAndView hello(){
        return new ModelAndView("hello");
    }
}

 如果像这样添加完就运行的话会报错

 因为你传过来的是字符串形式,无法自动转换成日期类型,所以你必须在实体类的日期上加上一个格式化的注释

package com.lxy.pojo;

import org.springframework.format.annotation.DateTimeFormat;

import java.util.Date;

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

    @Override
    public String toString() {
        return "Team{" +
                "teamId=" + teamId +
                ", teamName='" + teamName + '\'' +
                ", teamLocation='" + teamLocation + '\'' +
                ", 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 getTeamLocation() {
        return teamLocation;
    }

    public void setTeamLocation(String teamLocation) {
        this.teamLocation = teamLocation;
    }
}

6.7 获取数组类型的参数 

(1)修改hello.jsp页面

<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2022-04-22 0022
  Time: 下午 04:02:10
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>hello</title>
</head>
<body>

    <h3>7.获取数组类型的参数</h3>
    <form action="/param/test07.do" 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>

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

    <h3>4.使用HttpServletRequest 对象获取参数</h3>
    <form action="/param/test04.do" 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>

    <h3>3.请求参数和方法名称的参数不一致</h3>
    <form action="/param/test03.do" 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>

    <h3>2.使用对象接收多个参数</h3>
    <form action="/param/test02.do" 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>

   <h3>1.直接使用方法的参数逐个接收</h3>
   <form action="/param/test01.do" 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>
</html>

 (2)在controller中添加对应的请求方法

package com.lxy.controller;

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

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("param")
public class ParamController {

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

    //6.获取日期类型的参数
    @RequestMapping("test06.do")
    public ModelAndView test06(Team team){
        System.out.println("test06---------------");
        System.out.println(team);
        return new ModelAndView("ok");
    }

    //5.直接使用URL地址传参:
    @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");
    }
    //4.使用HttpServletRequest 对象获取参数: 跟原来的javaWeb项目中使用的方法是一样的
    @RequestMapping("test04.do")
    public ModelAndView test04(HttpServletRequest request){
        System.out.println("test04---------------");
        String teamId = request.getParameter("teamId");
        String teamName = request.getParameter("teamName");
        String teamLocation = request.getParameter("teamLocation");
        if(teamId!=null){
            System.out.println(Integer.parseInt(teamId));
        }
        System.out.println(teamId);
        System.out.println(teamName);
        System.out.println(teamLocation);
        return new ModelAndView("ok");
    }

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

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

    /**
     * 直接使用方法的参数逐个接收: 方法的参数名称必须与用户请求中携带的参数名称保持一致,否则就获取不到
     */
    @RequestMapping("test01.do")
    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");
    }

    @RequestMapping("hello.do")
    public ModelAndView hello(){
        return new ModelAndView("hello");
    }
}

 6.8 获取集合类型的参数

(1)修改hello.jsp页面

<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2022-04-22 0022
  Time: 下午 04:02:10
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>hello</title>
</head>
<body>

    <h3>8.获取集合类型的参数</h3>
    <form action="/param/test08.do" 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>

    <h3>7.获取数组类型的参数</h3>
    <form action="/param/test07.do" 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>

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

    <h3>4.使用HttpServletRequest 对象获取参数</h3>
    <form action="/param/test04.do" 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>

    <h3>3.请求参数和方法名称的参数不一致</h3>
    <form action="/param/test03.do" 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>

    <h3>2.使用对象接收多个参数</h3>
    <form action="/param/test02.do" 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>

   <h3>1.直接使用方法的参数逐个接收</h3>
   <form action="/param/test01.do" 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>
</html>

  (2)在controller中添加对应的请求方法

package com.kkb.controller;

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

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("param")
public class ParamController {

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

 还可以不使用@RequestParam来获取,直接将集合封装为一个对象,再使用对象的方式获取参数

编写一个类 QueryVo:

package com.lxy.vo;

import com.lxy.pojo.Team;

import java.util.List;

public class QueryVO {
    private List<Team> teamList;

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

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

修改hello.jsp页面:

    <form action="/param/test09.do" 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>

修改Controller中它所对应的方法:

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

7、请求参数中文乱码 

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

 7.1 乱码解决方案

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

<!--注册字符集过滤器:post请求中文乱码问题的解决方案-->
    <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>
        <!--强制request使用字符集encoding-->
        <init-param>
            <param-name>forceRequestEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
        <!--强制response使用字符集encoding-->
        <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>

7.2 解决方案原理 

 8、处理器方法的返回值

使用@Controller 注解的处理器的处理器方法,其返回值常用的有四种类型:
1. ModelAndView
2. String
3. 返回自定义类型对象
4. 无返回值 void

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

 8.1 案例准备

(1)创建一个新的控制器 ResultController

package com.lxy.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

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

(2)在jsp文件夹中添加新的页面result.jsp

<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2022-04-22 0022
  Time: 下午 07:18:59
  To change this template use File | Settings | File Templates.
--%>
<%@ 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>

 8.2 返回ModelAndView

如果是前后端不分的开发,大部分情况下,我们返回 ModelAndView,即数据模型+视图:
控制器ResultController.java中添加方法:

package com.lxy.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

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

    //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;
    }
}

Model 中,放我们的数据,然后在 ModelAndView 中指定视图名称。
当处理器方法处理完后,需要跳转到其它资源的同时传递数据,选择返回 ModelAndView 比较好,但是如果只是需要传递数据或者跳转之一,这个时候ModelAndView 就不是最优选择。 

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

<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2022-04-22 0022
  Time: 下午 07:18:59
  To change this template use File | Settings | File Templates.
--%>
<%@ 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>
</body>
</html>

 8.3 返回String

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

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

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

package com.lxy.controller;

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

import javax.servlet.http.HttpServletRequest;

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

    @RequestMapping("test02")
    public String test02(HttpServletRequest request){
        Team team = new Team();
        team.setTeamLocation("迈阿密");
        team.setTeamId(1002);
        team.setTeamName("热火");
        request.setAttribute("team",team);
        request.getSession().setAttribute("team",team);
        return "result"; //经过InternalResourceViewResolver对象的处理后加上前缀就变成了  /jsp/index.jsp
    }

    //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;
    }
}

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

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

 

 8.4 返回对象类型 @RequestBody

当处理器方法返回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>

 (1)返回基础类型

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

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

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

(2)返回自定义类型

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

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

在项目中引用jQuery来使用ajax

 result.jsp 中添加ajax代码:

<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2022-04-22 0022
  Time: 下午 07:18:59
  To change this template use File | Settings | File Templates.
--%>
<%@ 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.teamId}---${requestScope.team.teamName}---${requestScope.team.teamLocation}</h3>
<h3>test02---session作用域获取:---${sessionScope.team.teamId}---${requestScope.team.teamName}---${sessionScope.team.teamLocation}</h3>

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

 (3)返回集合List

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

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

result.jsp中添加如下内容:

<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2022-04-22 0022
  Time: 下午 07:18:59
  To change this template use File | Settings | File Templates.
--%>
<%@ 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.teamId}---${requestScope.team.teamName}---${requestScope.team.teamLocation}</h3>
<h3>test02---session作用域获取:---${sessionScope.team.teamId}---${requestScope.team.teamName}---${sessionScope.team.teamLocation}</h3>

<div>
    <button type="button" id="btn">ajax自定义对象请求</button>
    <h3>ajax请求自定义对象的展示结果</h3>
    <p id="res"></p>
</div>
<div>
    <button type="button" id="btn2">ajax集合对象请求</button>
    <h3>ajax请求list对象的展示结果</h3>
    <p id="res2"></p>
</div>
<script>
    $(function (){
        $("#btn").click(function (){
            $.ajax({
                type:"post",
                url:"/result/test03-3",
                data:"",
                success:function (msg){
                    alert(msg);
                    var name = msg.teamName;
                    var id = msg.teamId;
                    var loc = msg.teamLocation;
                    $("#res").html("name="+name+",id="+id+",location="+loc);
                }
            });
        });
        $("#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.teamLocation+"<br/>";
                    }
                    $("#res2").html(str);
                }
            });
        });
    });
</script>
</body>
</html>

 

 (4)返回集合Map

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

    @ResponseBody
    @RequestMapping("test03-5")
    public Map<String,Team> test035(){
        Map<String,Team> map = new HashMap<>();
        for(int i = 0 ; i <= 5; i++) {
            Team team = new Team();
            team.setTeamLocation("上上"+i);
            team.setTeamId(1002+i);
            team.setTeamName("下下"+i);
            //日期类型,在返回的时候是个数字,如果想要按日期格式展示需要在实体类对应属性添加注解@JsonFormat(pattern = "yyyy-MM-dd")
            team.setCreateTime(new Date());
            map.put(team.getTeamId()+"",team);
        }
        return map;
    }

因为这里是ajax的形式来返回数据 所以在实体类的日期上必须加上 

@JsonFormat(pattern = "yyyy-MM-dd") 才能将日期类型转换成json类型,后台才能拿到数据

实体类:

@DateTimeFormat 是前台传字符串类型,后台必须转换为日期类型才能显示

@JsonFormat 是后台传日期类型数据,前台ajax接收时必须转换成json才能接收到数据

result.jsp中添加如下内容:

<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2022-04-22 0022
  Time: 下午 07:18:59
  To change this template use File | Settings | File Templates.
--%>
<%@ 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.teamId}---${requestScope.team.teamName}---${requestScope.team.teamLocation}</h3>
<h3>test02---session作用域获取:---${sessionScope.team.teamId}---${requestScope.team.teamName}---${sessionScope.team.teamLocation}</h3>

<div>
    <button type="button" id="btn">ajax自定义对象请求</button>
    <h3>ajax请求自定义对象的展示结果</h3>
    <p id="res"></p>
</div>
<div>
    <button type="button" id="btn2">ajax集合对象请求</button>
    <h3>ajax请求list对象的展示结果</h3>
    <p id="res2"></p>
</div>
<div>
    <button type="button" id="btn3">ajax集合对象请求</button>
    <h3>ajax请求map对象的展示结果</h3>
    <p id="res3"></p>
</div>
<script>
    $(function (){
        $("#btn").click(function (){
            $.ajax({
                type:"post",
                url:"/result/test03-3",
                data:"",
                success:function (msg){
                    alert(msg);
                    var name = msg.teamName;
                    var id = msg.teamId;
                    var loc = msg.teamLocation;
                    $("#res").html("name="+name+",id="+id+",location="+loc);
                }
            });
        });
        $("#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.teamLocation+"<br/>";
                    }
                    $("#res2").html(str);
                }
            });
        });
        $("#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.teamLocation+",time:"+obj.createTime+"<br/>";
                    });
                    $("#res3").html(str);
                }
            });
        });
    });
</script>
</body>
</html>

 8.5无返回值 void-了解

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

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

    //通过 HttpServletRequest 做服务端跳转
    @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);
    }
    //通过 HttpServletResponse 做重定向
    @RequestMapping("test04-2")
    public void test042(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("直接使用HttpServletResponse重定向跳转");
        response.sendRedirect("/jsp/ok.jsp");
    }
    //通过 HttpServletResponse 给出响应
    @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");
    }

在网址中输入:

http://localhost:8080/result/test04-1

http://localhost:8080/result/test04-2

http://localhost:8080/result/test04-3

http://localhost:8080/result/test04-4

输入后可自行查看结果。 

如果您还有什么疑问或解答有问题,可在下方评论,我会及时回复。 

如果您看完后对您有帮助的话,请给博主点个赞支持一下吧

SpringMVC下会在近日更新。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

月亮被咬碎成星星

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

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

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

打赏作者

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

抵扣说明:

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

余额充值