Spring4MVC之使用 POJO 对象绑定请求参数值

 

使用 POJO 对象绑定请求参数值

 

Spring MVC 会按请求参数名和 POJO 属性名进行自动匹配,自动为该对象填充属性值。支持级联属性。

如:dept.deptId、dept.address.tel 等

 

示例

 

1. JSP页面

 

<li>使用 POJO 对象绑定请求参数值
            <form action="user/testPOJOMapping" method="POST">
                <table>
                    <tr>
                        <td>用户名:</td><td><input type="text" name="username"  value=""/></td>
                    </tr>
                    <tr>
                        <td>密码:</td><td><input type="password" name="password"  value=""/></td>
                    </tr>
                    <tr>
                        <td>电子邮件:</td><td><input type="text" name="email"  value=""/></td>
                    </tr>
                    <tr>
                        <td>联系电话:</td><td><input type="text" name="phonenumber"  value=""/></td>
                    </tr>
                    <tr>
                        <td>省份:</td><td><input type="text" name="address.province"  value=""/></td>
                    </tr>
                    <tr>
                        <td>城市:</td><td><input type="text" name="address.city"  value=""/></td>
                    </tr>
                    <tr align="right">
                        <td colspan="2"><input type="submit"/></td>
                    </tr>
                </table>
            </form>
        </li>

 

2. 添加模型类

 

package org.rabbitx.web.spring4mvc.requestmapping.model;

public class Address {

	private String province;
	
	private String city;

	public String getProvince() {
		return province;
	}

	public void setProvince(String province) {
		this.province = province;
	}

	public String getCity() {
		return city;
	}

	public void setCity(String city) {
		this.city = city;
	}

	@Override
	public String toString() {
		return "Address [province=" + province + ", city=" + city + "]";
	}
}

 

package org.rabbitx.web.spring4mvc.requestmapping.model;

public class User {

	private String username;
	
	private String password;
	
	private String email;
	
	private String phonenumber;
	
	private Address address;

	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 String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getPhonenumber() {
		return phonenumber;
	}

	public void setPhonenumber(String phonenumber) {
		this.phonenumber = phonenumber;
	}

	public Address getAddress() {
		return address;
	}

	public void setAddress(Address address) {
		this.address = address;
	}

	@Override
	public String toString() {
		return "User [username=" + username + ", password=" + password
				+ ", email=" + email + ", phonenumber=" + phonenumber
				+ ", address=" + address + "]";
	}
	
}

 

3. 添加处理类

 

	/**
	 * 使用 POJO 对象绑定请求参数值
	 * 
	 * Spring MVC 会按请求参数名和 POJO 属性名进行自动匹配,自动为该对象填充属性值。支持级联属性。
	 * 如address.province,address.city等
	 * 
	 * 遗留问题:
	 * 1. 表单内容为中文时,乱码
	 */
	@RequestMapping("testPOJOMapping")
	public String testPOJOMapping(User user)
	{
		System.out.println("------testPOJOMapping - user :" + user +" ------");
		return LOGIN;
	}

 

 

 

你可能感兴趣的:(Spring4MVC之使用 POJO 对象绑定请求参数值)