2013-08-27 121 views
0

我正在使用基於SEAM的JSF Web應用程序。我有一個簡單的表單,其中包含一個查找(彈出窗口),用於檢索類別名稱和標識(兩者都是沒有問題的檢索並存儲在myView中)。查找返回時,類別名稱用於填充「selectedCategoryName」(使用Jquery)。表單驗證清除JavaScript字段

當您嘗試提交表單並且另一個字段驗證失敗(簡單的必填字段丟失)時,會出現此問題。我所有的其他字段保持不變,但selectedCategoryName被清除(即使bean仍然有它的值,所以修復驗證錯誤並重新提交解析爲正確的提交)。所以我的問題是,如何在表單驗證失敗時保持顯示「selectedCategoryName」中的值?

在此先感謝!

<s:decorate id="selectedCategoryField" template="/layout/edit.xhtml"> 
<span> 
    <h:panelGroup rendered="#{! myView.persisted}"> 
    <img class="lookup" src="${request.contextPath}/images/lookup.gif" width="15" height="14" alt="" 
         title="Category Lookup" 
         onclick="return openNewWindow('${CategoryLookupUrl}?#{myView.lookupParameters}', 'id');"/> 
    </h:panelGroup> 

      <h:inputText id="selectedCategoryName" required="true" 
         value="#{myView.selectedCategoryName}" 
         size="50" 
         readonly="true"> 

      <a:support event="onchanged" reRender="selectedCategoryField" ajaxSingle="true"/> 
    <a:support event="oninputchange" reRender="selectedCategoryField" ajaxSingle="true"/> 
      </h:inputText> 
    </span> 

回答

2

的問題是,實際上豆不成立,因爲readonly="true"選擇的值。當您將該屬性應用於UIInput時,UIInput將不會在提交時處理。

要解決此問題,您可以執行以下操作:
如果使用JSF2,則可以使用readonly="#{facesContext.renderResponse}"代替。
如果不是,請在您的支持bean上定義一個方法isReadonly並使用readonly="#{myView.isReadonly}"

public boolean isReadonly() { 
    return FacesContext.getCurrentInstance().getRenderResponse(); 
} 

看一看以下similiar問題,尤其是答案爲什麼這工作的更多信息: Data in <h:inputText readonly="true"> disappears when command button is clicked

+0

這是完美的,謝謝! – Ace