动作属性中的JSF 2.1 ValueExpression [英] JSF 2.1 ValueExpression in action-attribute
问题描述
JSF 2.1规范的3.1.4节说,标准组件的所有属性都启用了值表达.
Section 3.1.4 of the JSF 2.1 Specification says that all attributes of standard components are value expression enabled.
我想为commandButton的action属性分配一个值表达式:
I want to assign a value expression to the action attribute of a commandButton:
<h:commandButton value="OK" action="#{myBean.valExp}" />
我还在bean的类中定义了相应的getValExp和setValExp方法.
I also defined corresponding getValExp and setValExp methods in the bean's class.
但是我的JSF实现(JBoss 6)将该表达式作为方法表达式,并由于没有valExp()方法而产生未找到方法"错误.
However my JSF implementation (JBoss 6) takes that expression to be a method expression and thus yields a "method not found" error because there's no valExp()-method.
我是在做错什么,还是规范太草率,实际上并不意味着全部,而只是非方法表达式属性?还是我误解了规格?
Am I doing something wrong or is the specification just too sloppy and actually doesn't mean all, but only the non method expression attributes? Or am I misunderstanding the spec?
[备注:这个问题的原因不是实际的技术问题,而是我试图理解值和方法表达式的区别.]
[Remark: The reason for this question is no actual technical problem but me trying to understand the difference of value and method expressions.]
推荐答案
值表达式绑定到公共getter/setter方法公开的属性.
Value expressions are bound to properties which are exposed by public getter/setter methods.
<h:inputText value="#{bean.value}" />
这需要public T getValue()
和public void setValue(T value)
方法.请注意,不是强制存在名称实际上完全相同的private T value;
属性.在纯输出组件(例如<h:outputText>
,<h:dataTable>
,<f:selectItems>
等)中,setter方法也不是必需的.
This requires public T getValue()
and public void setValue(T value)
methods. Note that the presence of a private T value;
property with literally exactly the same name is not mandatory. In pure output components like <h:outputText>
, <h:dataTable>
, <f:selectItems>
, etc, the setter method is also not mandatory.
方法表达式绑定到非getter/setter方法("action"方法).
Method expressions are bound to non-getter/setter methods ("action" methods).
<h:commandButton value="submit" action="#{bean.submit}" />
这需要一个public T submit()
方法,其中T
最终可以为void
,并且该方法最终可以采用其他参数,具体取决于属性的方法表达式签名.您可以在查看声明语言文档,例如 <h:inputText>
, <h:commandButton>
和 <f:ajax>
一个>.这是<h:commandButton>
的action
和actionListener
属性定义的摘录:
This requires a public T submit()
method where T
can eventually be void
and the method can eventually take additional arguments, depending on the attribute's method expression signature. You can read the exact details in the view declaration language documentation, for example the <h:inputText>
, <h:commandButton>
and <f:ajax>
ones. Here's an extract of the action
and actionListener
attribute definitions of the <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".
是的,我同意规范在说明所有属性支持值表达式方面有些草率.通常,它们实际上表示所有属性都支持#{}
中的表达语言.另一方面,您也可以将方法表达式解释为仅仅是特殊"值表达式,尽管它们并不完全一样.我已经发布了与此有关的规范问题报告,并要求清除一些混淆:问题1036 .
Yes, I agree that the spec is somewhat sloppy in stating that all attributes support value expressions. Generally, they actually mean that all attributes support expression language as in #{}
. From the other hand on, you can also interpret method expressions as if they are just "special" value expressions, although they are not exactly that. I've posted a spec issue report about this with the request to clear up some confusion: issue 1036.
这篇关于动作属性中的JSF 2.1 ValueExpression的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!