简体   繁体   English

使用 JSP 形式更新对象

[英]Update object using JSP form

If I pass an object to jsp page, how can I update its fields using setters and send it back?如果我将一个对象传递给 jsp 页面,如何使用 setter 更新其字段并将其发回?

If for example we have例如,如果我们有

public class Person {

    private int age;
    private String name;

    public int getAge() {
        return age;
    }

    public String getName() {
        return name;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public void setName(String name) {
        this.name = name;
    }
}

And a controller还有一个控制器

@RequestMapping(value = "/updatePerson", method = RequestMethod.GET)
public String showPerson(Model model) {
    Person person = new Person();
    person.setAge(23);
    person.setName("Jack");
    model.addAttribute("person", person);

    return "updatePerson";
}

And a jsp page和一个jsp页面

<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>

<form:form modelAttribute="person">
    <form:input path="age"/>
    <input type="submit"/>
</form:form>

How to make this JSP page send as a result modified person Object, not new with only one field?如何使这个 JSP 页面作为结果发送修改过的 person 对象,而不是只有一个字段的新对象?

Add a method in the controller that handles the form submit:在控制器中添加一个处理表单提交的方法:

@RequestMapping(value = "/updatePerson", method = RequestMethod.POST)
public String alterPerson(@ModelAttribute Person person) {
    // do stuff
}

Note the changes:注意变化:

  • POST instead of GET : submitting a form by default uses POST -Requests. POST而不是GET :默认情况下提交表单使用POST -Requests。
  • @ModelAttribute automatically retrieves the submitted data and fills a Person object with it @ModelAttribute自动检索提交的数据并用它填充一个Person对象

With the form you have the name field will always be empty, though.但是,对于表单,您的name字段将始终为空。 Add another <form:input path="name"/> to fix that.添加另一个<form:input path="name"/>来解决这个问题。

If you don't want to let users change their name, the Person object probably shouldn't be in your model at all;如果您不想让用户更改他们的名字,那么Person对象可能根本不应该出现在您的模型中; it depends on how these objects are persisted, though.不过,这取决于这些对象是如何持久化的。 You could use a separate object like this:您可以像这样使用一个单独的对象:

public class PersonChangeRequest {
    private int age;

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

And use it as the @ModelAttribute like this:并将其用作@ModelAttribute如下所示:

@RequestMapping(value = "/updatePerson", method = RequestMethod.GET)
public String showPerson(Model model) {
    PersonChangeRequest person = new PersonChangeRequest();
    person.setAge(23);
    model.addAttribute("person", person);

    return "updatePerson";
}

@RequestMapping(value = "/updatePerson", method = RequestMethod.POST)
public String alterPerson(@ModelAttribute PersonChangeRequest personChangeRequest) {
    Person person = findPersonToChange(personChangeRequest);
    person.setAge(personChangeRequest.getAge());
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM