SpringMVC——請求參數的綁定

參數綁定說明

綁定機制

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轉換爲日期類型導致的,則需要我們自定義類型轉換器

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章