控制器如何从jquery开发的表单中捕获数据?

发布于 2024-10-25 13:41:39 字数 2813 浏览 3 评论 0原文

[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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

淡写薰衣草的香 2024-11-01 13:41:39

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

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

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++;
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文