Spring MVC之绑定参数

一. SpringMVC中如何绑定参数?

在SpringMVC中,需要接收客户端提交过来的参数进行操作。因此,我们需要进行参数绑定。

  1. 如何绑定
    我们都知道在Struts2中,接收参数有三种方式(具体自行百度,此处不详细讲解)。
    但是在SpringMVC中接收参数很简单,我们只需要在controller中声明一个方法,方法形参和提交参数的key一致即可。
    首先,在HTML中将input标签的name值设置为id,然后在controller方法中声明形参
    1
    2
    3
    4
    5
    // @RequestMapping("/xxx.action")为该方法的请求路径
    @RequestMapping("/xxx.action")
    public void test(Integer id){
    System.out.println(id);
    }

二. 默认支持的参数类型

  • HttpServletRequest
  • HttpServletResponse
  • HttpSession
  • 这三个参数就不多解释了,servlet时代的东西了。

三. 绑定简单类型

  1. 第一点中绑定的就是简单类型,SpringMVC支持以下简单类型:
  • 整形:Integer、int
  • 字符串:String
  • 单精度:Float、float
  • 双精度:Double、double
  • 布尔型:Boolean、boolean

推荐使用包装类型,因为基础数据类型不可以为null

  1. @RequestParam
  • @RequestParam常用于处理简单类型的绑定,当表单的name和controller方法中形参名不一致时使用。
    • value:参数名字,即形参的请求参数名字,如value=“itemId”表示表单中name的名字
    • required:是否必须,默认是true,表示请求中一定要有相应的参数,否则将报错TTP Status 400 - Required Integer parameter ‘XXXX’ is not present

使用如下:

1
2
3
4
5
// @RequestMapping("/xxx.action")为该方法的请求路径
@RequestMapping("/xxx.action")
public void test(@RequestParam(value = "testId",required = true)Integer id){
System.out.println(id);
}

四. 绑定pojo

pojo的绑定和简单类型绑定用法基本一致。只要在形参上设置了对应的pojo,将会自动进行封装

1
2
3
4
5
6
7
8
9
10
11
12
//pojo
public class Items {

private Integer id;
private String name;
private Float price;
private String pic;
private Date createtime;
private String detail;

//get、set方法
}

1
2
3
4
5
6
7
8
//表单
<form id="itemForm" action="${pageContext.request.contextPath }/updateitem.action" method="post">
<input type="hidden" name="id" value="${item.id }" />
商品名称:<input type="text" name="name" value="${item.name }" /
商品价格:<input type="text" name="price" value="${item.price }" />
商品简介:<textarea rows="3" cols="30" name="detail">${item.detail }</textarea>
<input type="submit" value="提交" />
</form>
1
2
3
4
5
6
7
8
// @RequestMapping("/xxx.action")为该方法的请求路径
@RequestMapping("/updateitem.action")
public void test(Items items){
System.out.println(items.id);
System.out.println(items.name);
System.out.println(items.price);
System.out.println(items.detail);
}

五. 绑定包装pojo

包装pojo:将pojo当做某类的属性进行包装。

1
2
3
4
5
//包装pojo
public class QueryVo {
private Items items;
//get、set
}

在表单提交的时候我们需要修改name值,因为我们提交的是属性中的属性。

1
2
3
4
5
6
7
8
//表单
<form id="itemForm" action="${pageContext.request.contextPath }/updateitem.action" method="post">
<input type="hidden" name="items.id" value="${item.id }" />
商品名称:<input type="text" name="items.name" value="${item.name }" /
商品价格:<input type="text" name="items.price" value="${item.price }" />
商品简介:<textarea rows="3" cols="30" name="items.detail">${item.detail }</textarea>
<input type="submit" value="提交" />
</form>

1
2
3
4
5
6
7
8
9
// @RequestMapping("/xxx.action")为该方法的请求路径
@RequestMapping("/updateitem.action")
public void test(QueryVo vo){
Items items = vo.getItems();
System.out.println(items.id);
System.out.println(items.name);
System.out.println(items.price);
System.out.println(items.detail);
}

六. 绑定List

List中存放对象,并将定义的List放在包装类中

1
2
3
4
5
Public class QueryVo {
Private List<Items> itemList;//商品列表

//get/set方法..
}

在Jsp中的表单定义如下,主要注意的就是name的声明

1
2
3
4
5
6
7
8
9
10
11
<c:forEach items="${itemList }" var="item" varStatus="s">
<tr>
<td><input type="checkbox" name="ids" value="${item.id}"></td>
<td><input type="text" value="${item.name}" name="itemsList[${s.index}].name"></td>
<td><input type="text" value="${item.price}" name="itemsList[${s.index}].price"></td>
<td><fmt:formatDate value="${item.createtime}" pattern="yyyy-MM-dd HH:mm:ss"/></td>
<td>${item.detail }</td>

<td><a href="${pageContext.request.contextPath }/itemEdit.action?id=${item.id}">修改</a></td>
</tr>
</c:forEach>

controller方法定义如下:

1
2
3
public void useraddsubmit(Model model,QueryVo queryVo)throws Exception{
System.out.println(queryVo.getItemList());
}

七. 绑定数组对象

这个和普通的参数绑定没什么太大区别,只需要在形参中声明为数组对象或者放入包装类中即可。

八. 自定义参数绑定实现日期类型绑定

对于controller形参中pojo对象,如果属性中有日期类型,需要自定义参数绑定。(否则会报404之类的错误)

将请求日期字符串转成 日期类型,要转换的日期类型和pojo中日期属性的类型保持一致。

1
2
3
4
5
6
7
8
9
10
11
12
13
//pojo
public class Items {

private Integer id;
private String name;
private Float price;
private String pic;
//日期类型
private Date createtime;
private String detail;

//get、set方法
}

  1. 首先创建转换类

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    package com.liweijian.controller.converter;
    import org.springframework.core.convert.converter.Converter;
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.Date;

    /**
    接口泛型参数说明:
    String:待转换类型
    Date:需要转换的类型
    */
    public class DateConverter implements Converter<String,Date> {

    @Override
    public Date convert(String s) {
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    try {
    Date date = simpleDateFormat.parse(s);
    return date;
    } catch (ParseException e) {
    e.printStackTrace();
    }
    return null;
    }
    }
  2. 在Spring或者SpringMVC配置文件中进行配置

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13

    <!-- 使用注解驱动自动配置映射器和适配器 -->
    <!-- 绑定自定义参数可以在这里进行配置 -->
    <mvc:annotation-driven conversion-service="formattingConversionService" />

    <!-- 绑定自定义参数,实现日期自动转换成合法的 -->
    <bean class="org.springframework.format.support.FormattingConversionServiceFactoryBean" id="formattingConversionService">
    <property name="converters">
    <list>
    <bean class="com.liweijian.controller.converter.DateConverter"/>
    </list>
    </property>
    </bean>

这样配置之后,客户端提交上来的参数就会转换成Date封装到pojo中。


####感谢阅读本博客。

####欢迎关注我的博客:https://li-weijian.github.io/

####欢迎关注我的CSDN:https://blog.csdn.net/qq352642663

####需要联系请加QQ:352642663

####欢迎联系我共同交流