2012-08-01 97 views
1

我正在運行查詢以檢查多個帳戶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的新手,希望對此有所幫助。

謝謝!

回答

2

2問題,那麼:

1 - 不是從你的message.properties檢索的消息:

您必須在您的項目中其他message.properties文件。給它一張支票。因爲如果找不到,grails會顯示代碼本身而不是消息,因爲它沒有找到它。也許它正在其他一些屬性文件中查找您的消息,例如您的Locale特定的消息(例如:pt_BR或en_US)。除此之外,您正在使用消息(代碼:...)構建正確。

2 - 您的Flash消息不消失:

相反flash.message,使用request.message的。

+0

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

+0

什麼你的意思是?閃存更改請求? – 2012-08-02 19:29:12

+0

另外,如果答案有幫助,請隨時接受。 ;) – 2012-08-02 20:21:21

0

我想你要遵循的例子是here

您只需要解析消息一次,即在控制器或視圖中。

所以在控制器:

flash.message = "test.not.created.message" 
flash.args = ["SAMLInfo"] 
flash.default = "<default text>" 

並在視圖:

<g:message code="${flash.message}" args="${flash.args}" 
      default="${flash.default}"/> 

這可以解釋爲什麼你仍然可以看到下一個頁面上的消息flash scope is cleared at the end of the next request

+1

也許你想分配'flash'並做一個'redirect' - 那麼這個消息只會顯示一次。 – 2012-08-01 09:51:51

+0

我試着做一個'redirect',但是我得到一個錯誤,說在這之前已經有一個'redirect',因此它不能被執行。這就是爲什麼我必須訴諸渲染視圖。 – 2012-08-01 18:11:53

+0

@Tiago Farias在我的項目中沒有其他messages.properties文件,我還沒有實現特定於語言環境的消息,所以這2種可能性都被消除了。 – 2012-08-01 18:14:24