0

How to edit an entity in google sitebricks is the correct way. Now I did it like this.

@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";
  }

}

At the POST method I load a exist entry from database,then set the not modified fields back into the entry from the form binding. then do a update to the database.

Is this the correct way in sitebrciks? Is there a way for just update entry in save method just like the entry create way. thanks.

Rory Ye
  • 1
  • 2
  • Hello. I don't get the question. What do you mean by "just update entry in save method just like the entry create way" – Thomas Aug 14 '12 at 22:32

1 Answers1

0

You should use the @Put method to edit an existing entity, instead of @Post. The contract there is to overwrite the value whether it exists or not.

@Post is meant for adding new values to a collection.