SpringMVC中Pojo作为参数的应用

首先在页面添加一个表单.

    <form action="springmvc/testPojo" method="post">
        <input type="text" name="username" />
        <br>
        <input type="text" name="email" />
        <br>
        <input type="text" name="address.city" />
        <br>
        <input type="text" name="address.provincial" />
        <br>
        <input type="text" name="age" />
        <br>
        <input type="submit" value="提交" />
    </form>

在操作类中添加一个方法来接收这个表单.

	@RequestMapping("/testPojo")
	public String testPojo(User user){
		System.out.println(user);
		return SUCCESS;
	}

另外需要添加一个User类,并添加GET和SET方法,为了后台显示清楚,重写toString方法。

该应用应该还包含级联模式,所以里面有一个Address类。

package com.yoni.springmvc.entities;

public class User {

	private String username;
	private String email;
	private Address address;
	private int age;
	
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public Address getAddress() {
		return address;
	}
	public void setAddress(Address address) {
		this.address = address;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	@Override
	public String toString() {
		return "User [username=" + username + ", email=" + email + ", address=" + address + ", age=" + age + "]";
	}
}

添加一个Address类,添加GET和SET方法,并重写toString方法。

package com.yoni.springmvc.entities;

public class Address {

	private String city;
	private String provincial;
	
	public String getCity() {
		return city;
	}
	public void setCity(String city) {
		this.city = city;
	}
	public String getProvincial() {
		return provincial;
	}
	public void setProvincial(String provincial) {
		this.provincial = provincial;
	}
	@Override
	public String toString() {
		return "Address [city=" + city + ", provincial=" + provincial + "]";
	}
}

将页面跑起来后,填写表单,提交后在后台可以看到如下结果。

User [username=aa, email=aa@aa.com, address=Address [city=wuhan, provincial=hubei], age=36]


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值