gpt4 book ai didi

java - Controller 如何从jquery开发的表单中捕获数据?

转载 作者:行者123 更新时间:2023-12-01 05:48:12 27 4
gpt4 key购买 nike

[ 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;
}
}

用户地址对象

 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++;
});

我的 Controller 有映射方法

@RequestMapping(value = "/registered", method= RequestMethod.POST)
public String varify(@ModelAttribute("userForm") UserForm userForm, HttpServletRequest req) {
return "redirect:index";
}

Controller 如何从jquery开发的表单中捕获数据?

请帮忙。

最佳答案

您需要将数组/列表中的值绑定(bind)到您的 bean。对于这种情况,如果列表是动态的,您可能需要在表单对象中使用 LazyList(apache 公共(public)集合)。

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++;
});

关于java - Controller 如何从jquery开发的表单中捕获数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5369350/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com