使用 Spring Forms 编辑对象属性

发布于 2024-11-07 16:15:30 字数 648 浏览 0 评论 0原文

我对如何使用 Spring Forms 编辑对象有点困惑,例如:假设我有一个对象 Person ,它有很多属性,因为我从 dao 获取它,并且我只想要提供“街道”字段进行编辑,我会在我的 PeopleController 中添加这个:

@RequestMapping("editPerson")
public void editPerson (Model model) {
    Person person = dao.getThatPerson ();
    model.addAttribute (person);
}

在我的 视图文件 中,除了这个我什么都没有:

<form:form method="post" action="" commandName="person">
    <form:input path="street" />
    <input type="submit" value="Edit!" />
</form:form>

所以当我 >post 表单,将表单的其他属性Person 对象被保留吗?比如人的名字、姓氏、用户名、年龄等等。否则,这怎么可能实现呢?

I'm a bit confused about how an object could be edited with Spring Forms, for instance: suppose I have an object Person who has a lot of attributes because I obtain it from a dao, and I only want to provide the field "street" for editing, I would have this in my PeopleController:

@RequestMapping("editPerson")
public void editPerson (Model model) {
    Person person = dao.getThatPerson ();
    model.addAttribute (person);
}

And in my view file I would have nothing but this:

<form:form method="post" action="" commandName="person">
    <form:input path="street" />
    <input type="submit" value="Edit!" />
</form:form>

So when I post the form, will the other attributes of the Person object be preserved? For instance person's name, surname, username, age, etc. Otherwise, how could this be accomplished?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

迷乱花海 2024-11-14 16:15:30

将模型对象存储在会话中,如下所示:

@Controller
@SessionAttributes("person")
public class YourController {
...
  @ModelAttribute("person")
  public Person getPerson() {
      return new Person();
  }

  @RequestMapping("editPerson")
  public void editPerson (@ModelAttribute("person") Person person) {
    person = dao.getThatPerson ();
  }

  @RequestMapping(value="postperson", method = RequestMethod.POST)
  public void postPerson(@ModelAttribute("person") Person person) {
    // do stuff with person
  }
}

Store your model object in session, like so:

@Controller
@SessionAttributes("person")
public class YourController {
...
  @ModelAttribute("person")
  public Person getPerson() {
      return new Person();
  }

  @RequestMapping("editPerson")
  public void editPerson (@ModelAttribute("person") Person person) {
    person = dao.getThatPerson ();
  }

  @RequestMapping(value="postperson", method = RequestMethod.POST)
  public void postPerson(@ModelAttribute("person") Person person) {
    // do stuff with person
  }
}
叫嚣ゝ 2024-11-14 16:15:30

您可能最好只是将人员 ID 和街道放入模型中,而不是整个人员。当用户提交时,只需通过以下任一方式更新街道即可:

1 - 在 DAO 中提供 updateStreet(String street, int personID) 方法。
2 - 从 DAO 中获取人员,设置街道,然后保留新人员。

You're probably best just putting the person ID and street in the model, not the whole Person. When the user submits then just update the street by either;

1 - Providing an updateStreet(String street, int personID) method in your DAO.
2 - Getting the person from the DAO, setting the street then persisting the new person.

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