JSF 2.1 ValueExpression in action-attribute

Tho*_*mas 6 jsf el

JSF 2.1规范的第3.1.4节说明标准组件的所有属性都是启用了值表达式.

我想为commandButton的action属性赋值表达式:

<h:commandButton value="OK" action="#{myBean.valExp}" />
Run Code Online (Sandbox Code Playgroud)

我还在bean的类中定义了相应的getValExp和setValExp方法.

但是我的JSF实现(JBoss 6)将该表达式作为方法表达式,因此产生"找不到方法"错误,因为没有valExp()方法.

我做错了什么,或者规范是否过于草率,实际上并不意味着全部,而只是非方法表达属性?或者我误解了规范?

[备注:这个问题的原因不是实际的技术问题,而是我试图理解价值和方法表达的区别.]

Bal*_*usC 8

值表达式绑定到公共getter/setter方法公开的属性.

<h:inputText value="#{bean.value}" />
Run Code Online (Sandbox Code Playgroud)

这需要public T getValue()public void setValue(T value)方法.请注意,private T value;具有完全相同名称的属性的存在不是强制性的.在像纯输出组件<h:outputText>,<h:dataTable>,<f:selectItems>等,装定件的方法也没有强制性的.

方法表达式绑定到非getter/setter方法("action"方法).

<h:commandButton value="submit" action="#{bean.submit}" />
Run Code Online (Sandbox Code Playgroud)

这需要一个最终可以的public T submit()方法,并且该方法最终可以采用其他参数,具体取决于属性的方法表达式签名.您可以在视图声明语言文档中阅读确切的详细信息,例如,和.以下是:和属性定义的摘录:Tvoid<h:inputText><h:commandButton><f:ajax>actionactionListener<h:commandButton>

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". 
Run Code Online (Sandbox Code Playgroud)

是的,我同意规范有点草率,声明所有属性都支持值表达式.通常,它们实际上意味着所有属性都支持表达式语言#{}.另一方面,您也可以将方法表达式解释为它们只是"特殊"值表达式,尽管它们并非如此.我已经发布了一个关于此问题的规范问题报告以及澄清一些混淆的请求:问题1036.