【发布时间】:2019-02-02 14:00:57
【问题描述】:
我使用的是 Grails 2.4.5 版本。我在保存时检查条件。如果我的条件匹配,我会尝试返回一条消息而不是保存。但它向我显示了我想要的消息,但同时它将对象属性保存到数据库。
这是我的代码:
@Transactional
def save(AccountHead accountHeadInstance) {
if (!request.method.equals('POST')) {
redirect(action: 'index')
return
}
LinkedHashMap result = new LinkedHashMap()
String outPut
if (accountHeadInstance.id) {
if (accountHeadInstance.id == accountHeadInstance.subParentId) {
result.put(CommonUtils.IS_ERROR, Boolean.TRUE)
result.put(CommonUtils.MESSAGE, "Sorry, Same account can not be parent of it's own")
outPut = result as JSON
render outPut
return
// here should it return, is shows the message ok but saving the object. if I print something after this block it does not execute
}
}
if (accountHeadInstance.hasErrors()) {
def errorList = accountHeadInstance?.errors?.allErrors?.collect{messageSource.getMessage(it,null)}
result.put(CommonUtils.IS_ERROR, Boolean.TRUE)
result.put(CommonUtils.MESSAGE, errorList?.join('\n'))
outPut = result as JSON
render outPut
return
}
accountHeadInstance.hasChild = params.position == CommonUtils.POSITION_FIRST
accountHeadInstance.save flush:true
result.put(CommonUtils.MESSAGE, "Account head saved successfully.")
outPut = result as JSON
render outPut
return
}
【问题讨论】:
标签: grails grails-2.0