Renaud is Not Bill Gates
Renaud is Not Bill Gates

Reputation: 2084

<f:attribute name="id"> throws java.lang.IllegalArgumentException at javax.faces.component.UIComponent.setValueExpression

I have a select one menu with a vaildator as following:

<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>

I'm trying to pass an attribute with selectOneMenu so that I can retrieve it in PlanningSalleAppareilFormuleCremationValidator as following:

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);
         }
     }
 }

But this doesn't work and it throws me : java.lang.IllegalArgumentException at javax.faces.component.UIComponent.setValueExpression

I get this error when I only add: <f:attribute name="id" value="#{planning.id}" /> to the selectOneMenu, otherwise the validator is working.

How can I solve this?

Upvotes: 0

Views: 584

Answers (1)

BalusC
BalusC

Reputation: 1108792

Effectively,

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

does exactly the same as:

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

The problem with this approach is that JSF does not allow setting a render time value expression as id attribute of the component. Moreover, you'd better not override/reuse a standard JSF component attribute for different/internal purposes.

Rename it to something else, e.g. planningId.

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

Upvotes: 2

Related Questions