值表达式绑定到由公共 getter/setter 方法公开的属性。
<h:inputText value="#{bean.value}" />
这需要public T getValue() 和public void setValue(T value) 方法。请注意,具有完全相同名称的private T value; 属性的存在不是强制性的。在<h:outputText>、<h:dataTable>、<f:selectItems> 等纯输出组件中,setter 方法也是非强制性的。
方法表达式绑定到非 getter/setter 方法(“action”方法)。
<h:commandButton value="submit" action="#{bean.submit}" />
这需要public T submit() 方法,其中T 最终可以是void,并且该方法最终可以采用其他参数,具体取决于属性的方法表达式签名。您可以在view declaration language documentation 中阅读详细信息,例如<h:inputText>、<h:commandButton> 和<f:ajax>。以下是<h:commandButton> 的action 和actionListener 属性定义的摘录:
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".
是的,我同意规范在声明 所有属性 支持值表达式方面有些草率。通常,它们实际上意味着所有属性都支持#{} 中的表达式语言。另一方面,您也可以将方法表达式解释为就好像它们只是“特殊”值表达式一样,尽管它们并非完全如此。我已经发布了一份关于此的规范问题报告,请求澄清一些困惑:issue 1036。