控制器如何从jquery开发的表单中捕获数据?
[spring 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>
我想在按下添加 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开发的表单中捕获数据?
请帮忙。
[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() {
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 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要将值绑定到您的 bean 的数组/列表中。对于这种情况,如果列表是动态的,您可能需要在表单对象中使用 LazyList(apache 公共集合)。
在你的 jquery 中:
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.
And in your jquery :