Spring MVC 參數綁定

基本類型和 String 類型參數

一、Controller 類

@Controller("helloController")
public class HelloController {

    @RequestMapping("/hello")
    public String sayHello(Integer id, String str) {
        System.out.println("id : " + id + " str : " + str);
        return "success";
    }
}

二、測試

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
    <body>
        <h2>Hello World!</h2>
        <a href="/hello?id=10&str=hello World!">請求參數</a>
    </body>
</html>

      請求參數是基本數據類型或者 String 類型,參數名必須和 Controller 類中方法參數名一致,並且區分大小寫。

POJO 類型參數

一、創建 POJO

public class Address implements Serializable {
    private String province;
    private String city;
	// 省略 Get、Set 方法
}
public class User implements Serializable {
    private String name;
    private String sex;
    private Address address;
	// 省略 Get、Set 方法
}

二、Controller 類

@Controller("helloController")
public class HelloController {

    @RequestMapping("/hello")
    public String sayHello(User user) {
        System.out.println("user : " + user.toString());
        return "success";
    }
}

三、測試

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
    <body>
        <form action="/hello" method="post">
            用戶姓名:<input type="text" name="name"/><br/>
            用戶性別:<input type="text" name="sex"/><br/>
            地址-省份:<input type="text" name="address.province"><br/>
            地址-城市:<input type="text" name="address.city"><br/>
            <input type="submit" value="保存">
        </form>
    </body>
</html>

      如果參數類型是 POJO 類型,參數名必須和 POJO 類的屬性名稱一致,Controller 類中方法的參數類型必須是 POJO 類型。

POJO 類中包含集合類型參數

一、POJO 類

public class Address implements Serializable {
    private String province;
    private String city;
	// 省略 Get、Set 方法
}
public class Account implements Serializable {
    private Float money;
    private Address address;
	// 省略 Get、Set 方法
}
public class User implements Serializable {
    private String name;
    private String sex;
    private List<Account> accounts;
    private Map<String,Account> accountMap;
	// 省略 Get、Set 方法
}

二、Controller 類

@Controller("helloController")
public class HelloController {

    @RequestMapping("/hello")
    public String sayHello(User user) {
        System.out.println("user : " + user.toString());
        return "success";
    }
}

三、測試

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
    <body>
        <form action="/hello" 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="accounts[0].name" ><br/>
            賬戶 1 金額:<input type="text" name="accounts[0].money" ><br/>
            賬戶 2 名稱:<input type="text" name="accounts[1].name" ><br/>
            賬戶 2 金額:<input type="text" name="accounts[1].money" ><br/>
            賬戶 3 名稱:<input type="text" name="accountMap['one'].name" ><br/>
            賬戶 3 金額:<input type="text" name="accountMap['one'].money" ><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>

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