2011-04-06 78 views
1

我有一個生成確認彈出窗口的命令按鈕。確認的文本來自一個消息包。是否可以將參數傳遞給確認?JavaScript中的JSF資源包參數?

這就是我想:

<h:commandButton value="#{tkmsg.addAccount}" action="#{ebfAccountControllerBean.specifyEbfAddAccount}" 
         onclick="return confirm('#{tkmsg.confirmAddAccount}');"> 
      <f:param value="this account"/> 
      <f:param value="this email"/> 
</h:commandButton> 

但它不工作。我剛剛得到

Are you sure you want to add account {0} with email {1}? 

Do參數只能與OutputText或OutputFormat一起使用嗎?有沒有其他方法可以做到這一點?我的下一步是用表單中的數據替換「此帳戶」。

回答

3

你所做的確實不會那樣工作。 JSF無法將f:param標籤與該EL表達式相關聯。試想一下,即使是一個人也無法猜到這一點;)

你可以將你的信息預先渲染成可通過EL使用的東西,例如,戰斧緩衝液:

<t:buffer into="#{buffer['confirm']}"> 
    <h:outputFormat value="#{tkmsg.confirmAddAccount}"> 
     <f:param value="this account"/> 
     <f:param value="this email"/> 
    </h:outputFormat> 
</t:buffer> 

#{buffer}是簡單地聲明爲與請求範圍託管bean一個hasmap。之後,您可以在javascript語句中參考#{buffer['confirm']}

2

Arjan給出了一個很好的例子。另一種方法是自己寫一個JS函數。以下是一個基本的啓動示例,它將JS String原型擴展爲新的format()函數。

<script> 
    String.prototype.format = function() { 
     var args = arguments; 

     return this.replace(/\{(\d+)\}/g, function() { 
      return args[arguments[1]]; 
     }); 
    } 
</script> 

如下使用它:

onclick="return confirm('#{tkmsg.confirmAddAccount}'.format('this account', 'this email'));"