参数绑定说明
绑定机制
1.表单中请求参数都是基于key=value格式的 例:username=hehe&password=123
2.SpringMVC的参数绑定过程就是把表单请求的请求参数,作为控制器中方法的参数进行绑定的
3.要求表单提交的请求参数和控制器中方法的参数名称相同
请求参数支持的数据类型
1.基本数据类型和字符串类型
2.POJO类型参数:包括实体类、以及关联的实体类
3.数组和集合类型参数:包括List和Map等
数据类型使用要求
1.基本类型或String类型:要求请求参数名称和控制器中方法参数名称保持一致(严格区分大小写)。
2.POJO类型参数:要求表单中请求参数名称和POJO类的属性名称保持一致,并且控制器方法的参数类型是POJO类型。
3.数组和集合类型参数:方式一:要求集合类型的请求参数必须在POJO中,在表单中请求参数名称和POJO中集合属性名称相同;
方式二:接收的请求参数是json格式数据。需要借助注解实现。
4.若其他特殊类型转换要求,需要自定义类型转换器。
示例代码
基本类型和String类型作为参数
编写jsp代码
<%@page contentType="text/html; charset=UTF-8" language="java" isELIgnored="false" %>
<html>
<head>
<title>requestMapping的使用</title>
</head>
<body>
<!--基本类型和String类型作为参数-->
<a href="account/findAccount?accountId=10&accountName=zhangsan">查询账户</a>
</body>
</html>
编写Controller类代码
package com.liang.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller("accountController")
@RequestMapping(path = "/account")
public class AccountController {
/**
* 查询所有账户
* @return
*/
@RequestMapping(path = "/findAccount")
public String findAccount(Integer accountId, String accountName){
System.out.println("查询了账户..."+accountId+" , "+accountName);
return "success";
}
}
POJO类型作为参数
编写jsp代码
<%@page contentType="text/html; charset=UTF-8" language="java" isELIgnored="false" %>
<html>
<head>
<title>requestMapping的使用</title>
</head>
<body>
<!--POJO类型作为参数-->
<form action="account/saveAccount" method="post">
账户名称:<input type="text" name="name"/><br/>
账户金额:<input type="text" name="money"/><br/>
账户省份:<input type="text" name="address.provinceName"/><br/>
账户城市:<input type="text" name="address.cityName"/><br/>
<input type="submit" value="保存"/>
</form>
</body>
</html>
编写实体类
package com.liang.domain;
import java.io.Serializable;
/**
* 账户实体类
*/
public class Account implements Serializable {
private Integer id;
private String name;
private Float money;
private Address address;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Float getMoney() {
return money;
}
public void setMoney(Float money) {
this.money = money;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
@Override
public String toString() {
return "Account{" +
"id=" + id +
", name='" + name + '\'' +
", money=" + money +
", address=" + address +
'}';
}
}
package com.liang.domain;
import java.io.Serializable;
/**
* 地址实体类
*/
public class Address implements Serializable {
private String provinceName;
private String cityName;
public String getProvinceName() {
return provinceName;
}
public void setProvinceName(String provinceName) {
this.provinceName = provinceName;
}
public String getCityName() {
return cityName;
}
public void setCityName(String cityName) {
this.cityName = cityName;
}
@Override
public String toString() {
return "Address{" +
"provinceName='" + provinceName + '\'' +
", cityName='" + cityName + '\'' +
'}';
}
}
编写Controller类代码
package com.liang.controller;
import com.liang.domain.Account;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller("accountController")
@RequestMapping(path = "/account")
public class AccountController {
/**
* 保存账户
* @return
*/
@RequestMapping(path = "/saveAccount")
public String saveAccount(Account account){
System.out.println("查询了账户..."+account);
return "success";
}
}
POJO类中包含集合类型参数
编写jsp代码
<%@page contentType="text/html; charset=UTF-8" language="java" isELIgnored="false" %>
<html>
<head>
<title>requestMapping的使用</title>
</head>
<body>
<!--POJO类中包含集合类型参数-->
<form action="account/updateAccount" method="post">
用户名称:<input type="text" name="username"/><br/>
用户密码:<input type="password" name="password"/><br/>
用户年龄:<input type="text" name="age"/><br/>
账户1名称:<input type="text" name="accountList[0].name"/><br/>
账户1金额:<input type="text" name="accountList[0].money"/><br/>
账户1省份:<input type="text" name="accountList[0].address.provinceName"/><br/>
账户1城市:<input type="text" name="accountList[0].address.cityName"/><br/>
账户2名称:<input type="text" name="accountList[1].name"/><br/>
账户2金额:<input type="text" name="accountList[1].money"/><br/>
账户3名称:<input type="text" name="accountMap['one'].name"/><br/>
账户3金额:<input type="text" name="accountMap['one'].money"/><br/>
账户3省份:<input type="text" name="accountMap['one'].address.provinceName"/><br/>
账户3城市:<input type="text" name="accountMap['one'].address.cityName"/><br/>
账户4名称:<input type="text" name="accountMap['two'].name"/><br/>
账户4金额:<input type="text" name="accountMap['two'].money"/><br/>
<input type="submit" value="保存"/>
</form>
</body>
</html>
编写实体类
package com.liang.domain;
import java.io.Serializable;
import java.util.List;
import java.util.Map;
public class User implements Serializable {
private String username;
private String password;
private Integer age;
private List<Account> accountList;
private Map<String, Account> accountMap;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public List<Account> getAccountList() {
return accountList;
}
public void setAccountList(List<Account> accountList) {
this.accountList = accountList;
}
public Map<String, Account> getAccountMap() {
return accountMap;
}
public void setAccountMap(Map<String, Account> accountMap) {
this.accountMap = accountMap;
}
@Override
public String toString() {
return "User{" +
"username='" + username + '\'' +
", password='" + password + '\'' +
", age=" + age +
", accountList=" + accountList +
", accountMap=" + accountMap +
'}';
}
}
编写Controller类代码
package com.liang.controller;
import com.liang.domain.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller("accountController")
@RequestMapping(path = "/account")
public class AccountController {
/**
* 更新账户
* @return
*/
@RequestMapping(path = "/updateAccount")
public String updateAccount(User user){
System.out.println("更新了账户..."+user);
return "success";
}
}
自定义类型转换器
编写jsp代码
<%@page contentType="text/html; charset=UTF-8" language="java" isELIgnored="false" %>
<html>
<head>
<title>requestMapping的使用</title>
</head>
<body>
<!--自定义类型转换器-->
<a href="account/deleteAccount?date=2020-01-01">根据日期2020-01-01删除账户</a><br/>
<a href="account/deleteAccount?date=2020/01/01">根据日期2020/01/01删除账户</a><br/>
</body>
</html>
编写Controller类代码
package com.liang.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.Date;
@Controller("accountController")
@RequestMapping(path = "/account")
public class AccountController {
/**
* 更新账户
* @return
*/
@RequestMapping(path = "/deleteAccount")
public String deleteAccount(Date date){
System.out.println("删除了账户..."+date);
return "success";
}
}
当点击根据日期2020-01-01删除账户
的超链接时。会出现如下页面
当点击根据日期2020/01/01删除账户的超链接时。则会成功执行,这是因为Spring内置转化器实现了一些数据类型格式的自动转换。能够实现一部分数据类型格式的自动转换,而此处的错误则是无法将2020-01-01转换为日期类型导致的,则需要我们自定义类型转换器