私は文字通り、grails から始めたばかりで、いくつかの足場を生成したところです。ここには、すべてのコントローラーで繰り返されるかなり一般的なコードがいくつかあるようです。
- get() が成功したかどうかのテスト
- 楽観的ロックチェック
これをコントローラーからどのように削除することをお勧めしますか? 理想的には、私はただやりたいです
def personInstance = Person.get(id)
次に、すべてのコントローラーでデフォルトで生成されることを、すべてのコントローラーに対して単一の例外ハンドラーに実行させます。
def update(Long id, Long version) {
def personInstance = Person.get(id)
if (!personInstance) {
flash.message = message(code: 'default.not.found.message', args: [message(code: 'person.label', default: 'Person'), id])
redirect(action: "list")
return
}
if (version != null) {
if (personInstance.version > version) {
personInstance.errors.rejectValue("version", "default.optimistic.locking.failure",
[message(code: 'person.label', default: 'Person')] as Object[],
"Another user has updated this Person while you were editing")
render(view: "edit", model: [personInstance: personInstance])
return
}
}