0

如何在 google sitebricks 中编辑实体是正确的方法。现在我这样做了。

@Show("/WEB-INF/templates/adm/editentry.html")
@Decorated
@At("/adm/entry/:id")
public class EditEntry extends AdminLayout {

  @Inject
  private EntryService entryService;

  private Entry entry = new Entry();

  public Entry getEntry() {
      return entry;
  }

  public void setEntry(Entry entry) {
      this.entry = entry;
  }

  @Get
  public void getForm(@Named("id") String id) {
      this.entry = entryService.get(id);
  }

  @Post
  public String save() {
      Entry exist = entryService.get(entry.getId());
      if (exist == null) {
          FlashMap.setErrorMessage("No entry found with id:" + entry.getId());
      } else {
          if (StringUtils.isBlank(entry.getExcerpt())) {
              entry.setExcerpt(entry.getContent());
          }
          entry.setBlog(exist.getBlog());
          entry.setType(exist.getType());
          entry.setStatus(exist.getStatus());
          entry.setAuthor(exist.getAuthor());
          entryService.saveOrUpdate(entry);
      }
      return request.getContextPath() + "/adm/entries";
  }

}

在 POST 方法中,我从数据库加载现有条目,然后将未修改的字段设置回表单绑定中的条目。然后对数据库进行更新。

这是sitebrciks中的正确方法吗?有没有办法在保存方法中更新条目,就像条目创建方式一样。谢谢。

4

1 回答 1

0

您应该使用 @Put 方法来编辑现有实体,而不是@Post. 那里的合同将覆盖该值,无论它是否存在。

@Post用于向集合添加新值。

于 2013-01-03T04:27:25.133 回答