How does the controller to capture data from a form developed in jquery?
[spring 3.0.5]
My user object:
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() {
retu开发者_开发知识库rn 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 object
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;
}
}
My JSP page
<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>
I want to see after pressing the button Add URL (remove the address), there are fields for the object AddressForm. Adding fields using jquery did something like this:
var i = 0:
$("#addbtn").click(function() {
$("#someId").append(
'<input type="text" name="street_' + i + '" />' +
'<input type="text" name="city_' + i + '" />');
i++;
});
My Controller have mapping method
@RequestMapping(value = "/registered", method= RequestMethod.POST)
public String varify(@ModelAttribute("userForm") UserForm userForm, HttpServletRequest req) {
return "redirect:index";
}
How does the controller to capture data from a form developed in jquery?
Please help.
You need to bind values into array/list to your bean. For this case if list is dynamic you might need to use LazyList (apache common collection) in your form object.
private List<String> city = LazyList.decorate(new ArrayList(),
FactoryUtils.instantiateFactory(String.class));
// getter /setter
And in your jquery :
var i = 0:
$("#addbtn").click(function() {
$("#someId").append(
'<input type="text" name="street[' + i + ']" />' +
'<input type="text" name="city[' + i + ']" />');
i++;
});
精彩评论