Java 使用JSP表单更新对象

Java 使用JSP表单更新对象,java,spring,jsp,spring-mvc,Java,Spring,Jsp,Spring Mvc,若我将一个对象传递给jsp页面,那个么如何使用setter更新它的字段并将其发送回去 例如,如果我们有 public class Person { private int age; private String name; public int getAge() { return age; } public String getName() { return name; } public void s

若我将一个对象传递给jsp页面,那个么如何使用setter更新它的字段并将其发送回去

例如,如果我们有

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;
    }
}
和一个控制器

@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";
}
和一个jsp页面

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

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


如何使此JSP页面作为结果修改的person对象发送,而不是仅使用一个字段的新对象?

在控制器中添加一个处理表单提交的方法:

@RequestMapping(value = "/updatePerson", method = RequestMethod.POST)
public String alterPerson(@ModelAttribute Person person) {
    // do stuff
}
注意这些变化:

  • POST
    而不是
    GET
    :默认情况下提交表单使用
    POST
    -请求
  • @modeldattribute
    自动检索提交的数据并将其填充到
    Person
    对象中
但是,在表单中,
名称
字段将始终为空。添加另一个
以修复此问题

如果您不想让用户更改他们的名字,那么
Person
对象可能根本不应该出现在您的模型中;不过,这取决于这些对象的持久化方式。您可以使用这样一个单独的对象:

public class PersonChangeRequest {
    private int age;

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}
@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());
}
并将其用作
@ModelAttribute
,如下所示:

public class PersonChangeRequest {
    private int age;

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}
@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());
}

若要发送一个新的修改过的对象,您需要一个新的对象处理程序,最好配置为接受表单方法。@nikpon我不明白,您能给出一些链接的示例吗?问题是我不希望允许此人更改其名称。我只需要显示一个字段。当然我可以隐藏它(使用style属性display:none),但我认为这不是最好的解决方案;您接收该对象,验证它,最后用新值更新底层的
Person
。攻击者当前的设置方式可能会在POST请求中添加一个名称,
人的姓名将被更改。没有为
name
输入字段并不能阻止这一点。我认为这是唯一的解决方案。如果你把它贴出来,我会把它标记为correct@a76我已将其添加到我的帖子中。