[春季3.0.5]
我的用户对象:
public class UserForm {
@NotEmpty
@Size(max = 19)
private String firstName;
@NotEmpty
@Size(max = 19)
private String lastName;
@NotEmpty
@Size(max = 19)
@Email
private String email;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
UserAddress对象
public class AddressForm {
@NotEmpty
@Size(max = 19)
private String addressName;
@NotEmpty
@Size(max = 19)
private String street;
@NotEmpty
@Size(max = 19)
private String city;
@NotEmpty
@Size(max = 19)
private String country;
public String getAddressName() {
return addressName;
}
public void setAddressName(String addressName) {
this.addressName = addressName;
}
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
}
我的JSP页面
<form:form id="registerForm" commandName="userForm" action="registered">
<form:input id="firstName" path="firstName" /><form:errors path="firstName" />
<form:input id="lastName" path="lastName" /><form:errors path="lastName" />
<form:input id="email" path="email" /><form:errors path="email" />
<input type="button" value="add address" />
<input type="button" value="del address" />
<input type="button" value="SUBMIT" />
</form:form>
我想在按下Add URL(删除地址)按钮后看到,对象AddressForm有字段。使用jquery添加字段的做法如下:
var i = 0:
$("#addbtn").click(function() {
$("#someId").append(
'<input type="text" name="street_' + i + '" />' +
'<input type="text" name="city_' + i + '" />');
i++;
});
我的控制器有映射方法
@RequestMapping(value = "/registered", method= RequestMethod.POST)
public String varify(@ModelAttribute("userForm") UserForm userForm, HttpServletRequest req) {
return "redirect:index";
}
控制器如何从jquery中开发的表单中捕获数据?
请帮忙。
答案 0 :(得分:0)
您需要将值绑定到数组/列表中。对于这种情况,如果列表是动态的,您可能需要在表单对象中使用LazyList(apache common collection)。
private List<String> city = LazyList.decorate(new ArrayList(),
FactoryUtils.instantiateFactory(String.class));
// getter /setter
在你的jquery中:
var i = 0:
$("#addbtn").click(function() {
$("#someId").append(
'<input type="text" name="street[' + i + ']" />' +
'<input type="text" name="city[' + i + ']" />');
i++;
});