SpringMvc参数获取

目录

一、封装为简单数据类型

二、封装为单个对象

(1)实体类

(2)控制层

三、封装为关联对象

(1)实体类

(2)控制层

(3)使用表单发送带有参数的请求

四、封装为List集合

(1)控制层

五、封装为对象类型集合

(1)实体类

六、封装为Map集合

(1)实体类

七、使用Servlet原生对象获取参数

八、自定义参数类型转换器

(1)定义转换器类,实现Converter接口

(2)注册类型转换器对象


一、封装为简单数据类型

SpringMvc支持参数注入的方式用于获取请求数据,即将请求参数直接封装到方法的参数中。如下:

@Controller
public class MyController1 {
  
    @RequestMapping("/c1/h1")
    public void t1(String name,int age){
        System.out.println("name="+name+" age="+age);
    }
}

访问该方法时,请求参数名和方法参数名相同就可以自动完成封装。

http://localhost:8080/c1/h1?name=%E5%BC%A0%E4%B8%89&age=90

二、封装为单个对象

SpringMvc支持将参数直接封装为对象,如下:

(1)实体类

public class Student {
    private int id;
    private String name;
    private String sex;
    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", sex='" + sex + '\'' +
                '}';
    }

   //省略了get和set
}

(2)控制层

 @RequestMapping("/c1/h2")
    public void t2(Student student){
        System.out.println(student);
    }

访问该方法时,请求参数名和方法参数的属性名相同,即可自动完成封装。

http://localhost:8080/c1/h2?name=%E6%9D%8E%E5%9B%9B&sex=%E7%94%B7&&id=3

三、封装为关联对象

(1)实体类

public class Student {
    private int id;
    private String name;
    private String sex;
    private Address address;

    //忽略get和set了,记住一定要有空参函数,不然会报空指针异常
}

public class Address {
    private String info;//地址信息
    private String postcode;//邮编
  //忽略get和set了,记住一定要有空参函数,不然会报空指针异常
}

(2)控制层

 @RequestMapping("/c1/h2")
    public void t2(Student student){
        System.out.println(student);
    }

访问该方法时,请求参数名和方法参数的属性名相同,既可自动完成封装。

http://localhost:8080/c1/h2?name=%E6%9D%8E%E5%9B%9B&sex=%E7%94%B7&&id=3&address.info=beijing&address.postcode=20

(3)使用表单发送带有参数的请求

我们也可以使用表单发送带有参数的请求

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>表单提交</title>
</head>
<body>
<form action="/c1/h2" method="post">
    id:<input name="id">
    姓名:<input name="name">
    性别:<input name="sex">
    住址:<input name="address.info">
    邮编:<input name="address.postcode">
    <input type="submit">
</form>
</body>
</html>

四、封装为List集合

(1)控制层

//绑定简单数据类型List参数,参数前必须添加@RequestParam注解

 @RequestMapping("/c1/h4")
    public void t5(@RequestParam List<String> u){
        System.out.println(u);
    }

请求的参数写法:http://localhost:8080/c1/h4?u=89&u=sdsd&u=beijing

不仅仅可以绑定List,同时他也可以绑定数组,如下:

@RequestMapping("/c1/h3")
    public void t4(@RequestParam String[] users){
        System.out.println("------------------");
        for(String u:users){
            System.out.println(u);
        }
    }

 请求参数写法:http://localhost:8080/c1/h4?users=908&users=jhg

五、封装为对象类型集合

SpringMvc不支持将参数封装为对象类型的LIst集合,但可以封装到有List属性的对象中

(1)实体类

public class Student {
  private int id;
  private String name;
  private String sex;
  private List<Address> address; // 地址集合
  // 省略getter/setter/tostring
}

请求的参数写法:http://localhost:8080/c1/h2?name=op&sex=male&id=12&address[0].info=beijing&address[0].postcode=9090&address[1].info=anhui&address[1].postcode=87

六、封装为Map集合

SpringMvc要封装Map集合,需要封装到装有Map属性的对象中

(1)实体类

package com.gq.pojo;

import java.util.List;
import java.util.Map;

public class Student {
    private int id;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public Map<String, Address> getAddress() {
        return address;
    }

    public void setAddress(Map<String, Address> address) {
        this.address = address;
    }

    private String name;
    private String sex;
    private Map<String,Address> address;

    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", sex='" + sex + '\'' +
                ", address=" + address +
                '}';
    }

    public Student() {

    }



}

请求的参数写法:

http://localhost:8080/c1/h2?id=1&name=bz&sex=female&address[%E2%80%98one%E2%80%99].info=bj&address[%E2%80%98one%E2%80%99].postcode=100010&address[%E2%80%98two%E2%80%99].info=sh&address[%E2%80%98two%E2%80%99].postcode=100011

七、使用Servlet原生对象获取参数

SpringMvc也支持使用Servlet原生对象,在方法参数中定义HttpServletRequest,HttpServletResponse,HttpSession等类型的参数即可直接在方法中使用。

一般情况下,在SpringMvc中都会有对Servlet原生对象的方法的替代,推荐使用SpringMvc的方式代替Servlet原生对象。

    @RequestMapping("/c1/h5")
    public void t6(HttpServletRequest request, HttpServletResponse response, HttpSession session){
        System.out.println(request.getParameter("name"));
        System.out.println(response.getCharacterEncoding());
        System.out.println(session.getId());
    }

访问路径:http://localhost:8080/c1/h5?name=ioio

八、自定义参数类型转换器

前端传来的参数全部为字符串类型,SpringMvc使用自带的转换器将字符串参数转为需要的类型。如:

@RequestMapping("/c1/h6")
    public void t7(String name,int age){
        System.out.println(name.length());
        System.out.println(age*2);
    }

获取过来的name确实是String类型,age是int型。

但是在某些情况下,无法将字符串转为需要的类型,如:

@RequestMapping("/c1/h7")
    public void t8(Date date){
        System.out.println(date);
    }

这是因为日期数据有很多种格式,SpringMvc没办法将所有格式的字符串转换成日期类型。比如参数date=1786-01-01时,SpringMvc就无法解析参数。此时就需要自定义参数类型转换器。

(1)定义转换器类,实现Converter接口


//类型转换器必须要实现Converter接口,两个泛型表示转换之前的类型和转换后的类型
public class DateConverter implements Converter<String, Date> {
//source是转换类型前的数据
    @Override
    public Date convert(String source) {
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
//date是转换类型后的数据
        Date date=null;
        try{
            date=simpleDateFormat.parse(source);
        }catch (ParseException e){
            e.printStackTrace();
        }
        return date;
    }
}

(2)注册类型转换器对象

在核心配置文件SpringMvcConfig.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd">
    <!-- 扫描包 -->
    <context:component-scan base-package="com.gq"/>
    <!-- 开启SpringMVC注解的支持 -->
    <mvc:annotation-driven/>




    <!-- 配置转换器工厂 -->
    <bean id="converterFactory" class="org.springframework.context.support.ConversionServiceFactoryBean">
        <!-- 转换器集合 -->
        <property name="converters">
            <set>
                <!-- 自定义转换器 -->
                <bean class="com.gq.converter.DateConverter"></bean>
            </set>
        </property>
    </bean>

            <!-- 使用转换器工厂 -->
    <mvc:annotation-driven conversion-service="converterFactory"></mvc:annotation-driven>


</beans>

请求路径:

此时再访问http://localhost:8080/c1/param9?date=1987-12-01时,SpringMVC即可将请求参数封装为Date类型的参数。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

菜到极致就是渣

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

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

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

打赏作者

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

抵扣说明:

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

余额充值