2012-12-24 51 views
2

我正在JSF2.0中使用bean驗證。我有一個驗證組,我根據一些條件指定並鏈接到託管bean中的屬性。該屬性在頁面首次加載並正確工作時(即提交表單時驗證了正確的組)時被分配。但是,如果我更改此屬性,驗證組不會更新,並且將使用原始值設置爲。JSF f:validateBean validationGroups not updating

例如: JSF片段:

<h:selectOneMenu id="unitOfMerchandise" value="#itemManager[targetBean].unitOfMerchandise}"> 
    <f:selectItem itemLabel="-- select --" itemValue="" /> 
    <f:selectItems value="#{itemManager.unitsOfMerchandise}" /> 
    <f:validateBean validationGroups="#{itemManager.validatorClass}" /> 
</h:selectOneMenu> 

方法:

@ManagedBean 
@ViewScoped 
public class ItemManager implements Serializable { 
private String validatorClass = "com.rcs.itemmngr.model.validation.RegularItem" 
private OpenItemRequest openItemRequest 


private void onItemTypeSelected() { 

     validatorClass = itemManagerModel.getValidatorItemRequestClass(openItemRequest).getName(); 

} 
///getters setters 
} 

如何得到這個工作任何想法?我還尋找了一種在託管bean中以編程方式更改驗證組的方式,但沒有任何快樂。

回答

0

f:validateBean gruops被評估一次:構建組件樹時。似乎沒有簡單的方法來更新它們。

  • 您可以手動更新它們每個組件:

    //bind you component here 
    EditableValueHolder input;  
    
    //call this to update groups   
    public void setValidationGroups(String validationGroups) { 
        for (Validator validator : input.getValidators()) { 
         if (!(validator instanceof BeanValidator)) { 
          continue; 
         } 
         BeanValidator beanValidator = (BeanValidator) validator; 
         beanValidator.setValidationGroups(validationGroups); 
        } 
    } 
    
  • 或者您可以使用本文中介紹的方法: Delete the components holding unwanted state

    的想法是使用f刪除組件:validateBean從樹上,所以 他們將重新初始化與新組渲染:

    parentComponent.getChildren().clear(); 
    

    例如如果您正在執行和渲染部分,您可以撥打 somethig像這樣actionListner:

    public void resetContactsValidationGroups() { 
        FacesContext ctx = FacesContext.getCurrentInstance(); 
        Iterator<String> ids = ctx.getPartialViewContext().getExecuteIds().iterator(); 
        while (ids.hasNext()) { 
         ctx.getViewRoot().findComponent(ids.next()).getChildren().clear(); 
        } 
    }