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>

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