我正在運行查詢以檢查多個帳戶ID是否存在實體ID。如果結果集不爲空,那麼我需要拋出一個錯誤或顯示一條flash消息。Grails - 自定義Flash.message呈現
用於該方法的代碼是如下:
def save() {
def SAMLInfoInstance = new SAMLInfo(params)
def account = Account.get(params?.accountId)
SAMLInfoInstance.setAccount(account)
def samlInfoInstanceList = SAMLInfo.executeQuery("from SAMLInfo " +
" where account.id <> ? " +
" and entityId = ?", [SAMLInfoInstance.accountId, SAMLInfoInstance.entityId])
if (samlInfoInstanceList?.size > 0){
flash.message = message(code: 'test.not.created.message', args: [message(code: 'SAMLInfo.label', default: 'SAMLInfo'), SAMLInfoInstance.entityId])
/*flash.message = "default.not.created.message"
flash.args = ["SAMLInfo", SAMLInfoInstance.entityId]
flash.default = "SAMLInfo cannot be created"
*/
render(view: "create", model: [SAMLInfoInstance: SAMLInfoInstance])
return
}
if (!SAMLInfoInstance.save(flush: true)) {
render(view: "create", model: [SAMLInfoInstance: SAMLInfoInstance])
return
}
flash.message = message(code: 'default.created.message', args: [message(code: 'SAMLInfo.label', default: 'SAMLInfo'), SAMLInfoInstance.entityId])
redirect(action: "list", id: SAMLInfoInstance.account.id)
}
我認爲我呈現閃光消息,並以下列方式中的錯誤:
<g:if test="${flash.message}">
<br/>
<div class="message" role="status">
<g:message code="${flash.message}" args="${flash.args}" default="${flash.default}"/>
</div>
<br/>
</g:if>
<br/>
<g:renderErrors bean="${SAMLInfoInstance}" as="list" />
以我message.properties文件,我有以下行:
test.not.created.message=The SP url {1} is not allowed for this account. Please enter a different value.
當我運行此代碼時,閃爍消息顯示爲字符串I pas s作爲消息,即「test.not.created.message」。此外,只要我導航到顯示flash.message的任何其他頁面,該字符串就會傳遞到顯示。 我是grails和groovy的新手,希望對此有所幫助。
謝謝!
That works :) 我繼續解決我的問題,使用下面的代碼: 'Obj.errors.rejectValue('entityId','Obj.entityId.clash.not.created.message',[message(code: 'Obj.label',默認:'Obj url'),Obj.entityId]爲Object [],'此Obj url不允許用於此帳戶,請輸入不同的值。')' 它使用的方法簽名可以在http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/validation/Errors.html#rejectValue – 2012-08-02 19:24:08
什麼你的意思是?閃存更改請求? – 2012-08-02 19:29:12
另外,如果答案有幫助,請隨時接受。 ;) – 2012-08-02 20:21:21