<f:attribute name =“id”>在javax.faces.component.UIComponent.setValueExpression

时间:2016-08-30 10:57:05

标签: jsf attributes illegalargumentexception

我有一个带有vaildator的精选菜单如下:

<h:selectOneMenu value="#{planning.formuleCremation}"
     converter="#{formuleCremationConverter}" 
     validator="#{PlanningSalleAppareilFormuleCremationValidator}">
  <f:selectItems value="#{beanPlanningSalleAppareil.formules}"
         var="formule"
         itemLabel="#{formule.alias}"
         itemValue="#{formule}" />
  <f:attribute name="id" value="#{planning.id}" /> 
</h:selectOneMenu>

我尝试使用selectOneMenu传递属性,以便我可以在PlanningSalleAppareilFormuleCremationValidator中检索它,如下所示:

public void validate(FacesContext fc, UIComponent uic, Object o) throws ValidatorException {

     //Retrieve the attribute id passed with the current component
     int id = (int) uic.getAttributes().get("id");
     Object selectedObject = null;

     //Current Planning
     PlanningSalleAppareil planningSalleAppareil = planningSalleAppareilService.trouver(id);

     if(planningSalleAppareil.getAppareil() != null)
         selectedObject = planningSalleAppareil.getAppareil();
     if(planningSalleAppareil.getSalle() != null)
         selectedObject = planningSalleAppareil.getSalle();

     planningSalleAppareil.setFormuleCremation((FormuleCremation) o);

     if(selectedObject != null){
         String errorMessage = planningSalleAppareilService.validPlanningSalleAppareil(planningSalleAppareil,selectedObject);
         if(!errorMessage.equals("1")){
             FacesMessage msg = new FacesMessage(errorMessage);
            msg.setSeverity(FacesMessage.SEVERITY_ERROR);
             throw new ValidatorException(msg);
         }
     }
 }

但这不起作用,它引发了我:java.lang.IllegalArgumentException at javax.faces.component.UIComponent.setValueExpression

当我只向selectOneMenu添加<f:attribute name="id" value="#{planning.id}" />时,我收到此错误,否则验证器正在运行。

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:2)

有效地,

<h:selectOneMenu ...>
    <f:attribute name="id" value="#{planning.id}" />
    ...
</h:selectOneMenu>

完全相同:

<h:selectOneMenu id="#{planning.id}" ...>
    ...
</h:selectOneMenu>

这种方法的问题是JSF不允许将渲染时间值表达式设置为组件的id属性。此外,您最好不要为了不同/内部目的而覆盖/重用标准JSF组件属性。

将其重命名为其他内容,例如planningId

<f:attribute name="planningId" ... />