JSF 2.1 ValueExpression in action attribute

Section 3.1.4 of the JSF 2.1 specification states that all attributes of standard components have a value expressed .

I want to assign a value expression to the action attribute of the command:

<h:commandButton value="OK" action="#{myBean.valExp}" /> 

I also defined the corresponding getValExp and setValExp methods in the bean class.

However, my JSF implementation (JBoss 6) interprets this expression as a method expression and therefore gives a "method not found" error because there is no valExp () method.

Am I doing something wrong or is the specification too sloppy and doesn’t really mean everything, but only attributes of the non method expression? Or am I misunderstanding the specification?

[Note: the reason for this question is not a real technical problem, but I'm trying to understand the difference in the expressions of values ​​and methods.]

+6
source share
1 answer

Value expressions are tied to properties that are exposed to the public getter / setter methods.

 <h:inputText value="#{bean.value}" /> 

This requires the methods public T getValue() and public void setValue(T value) . Please note that the presence of the property private T value; with literally the same name is optional. In pure output components such as <h:outputText> , <h:dataTable> , <f:selectItems> , etc., the setter method is also optional.

Method expressions are tied to non-getter / setter methods ("action" methods).

 <h:commandButton value="submit" action="#{bean.submit}" /> 

This requires the public T submit() method, where T can ultimately be void , and ultimately the method can take additional arguments, depending on the signature of the attribute method expression. You can read the exact details in the documentation for the declaration description document , for example <h:inputText> , <h:commandButton> and <f:ajax> . Here is an excerpt from the action and actionListener attributes of the <h:commandButton> attributes:

 Name: action Type: javax.el.MethodExpression (signature must match java.lang.Object action()) Description: MethodExpression representing the application action to invoke when this component is activated by the user. The expression must evaluate to a public method that takes no parameters, and returns an Object (the toString() of which is called to derive the logical outcome) which is passed to the NavigationHandler for this application. Name: actionListener Type: javax.el.MethodExpression (signature must match void actionListener(javax.faces.event.ActionEvent)) Description: MethodExpression representing an action listener method that will be notified when this component is activated by the user. The expression must evaluate to a public method that takes an ActionEvent parameter, with a return type of void, or to a public method that takes no arguments with a return type of void. In the latter case, the method has no way of easily knowing where the event came from, but this can be useful in cases where a notification is needed that "some action happened". 

Yes, I agree that the specification is somewhat sloppy, stating that all attributes support value expressions. As a rule, they actually mean that all attributes support the language of expression, as in #{} . On the other hand, you can also interpret method expressions as if they were just “special” value expressions, although they are not quite like that. I sent a specification release report about this asking for some confusion: issue 1036 .

+8
source

Source: https://habr.com/ru/post/896978/


All Articles