【问题标题】:JSF 2.1 ValueExpression in action-attribute动作属性中的 JSF 2.1 ValueExpression
【发布时间】:2011-09-09 12:32:34
【问题描述】:

JSF 2.1 规范的第 3.1.4 节规定标准组件的所有属性启用了值表达式

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

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

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

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

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

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

【问题讨论】:

    标签: jsf el


    【解决方案1】:

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

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

    这需要public T getValue()public void setValue(T value) 方法。请注意,具有完全相同名称的private T value; 属性的存在不是强制性的。在&lt;h:outputText&gt;&lt;h:dataTable&gt;&lt;f:selectItems&gt; 等纯输出组件中,setter 方法也是强制性的。

    方法表达式绑定到非 getter/setter 方法(“action”方法)。

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

    这需要public T submit() 方法,其中T 最终可以是void,并且该方法最终可以采用其他参数,具体取决于属性的方法表达式签名。您可以在view declaration language documentation 中阅读详细信息,例如&lt;h:inputText&gt;&lt;h:commandButton&gt;&lt;f:ajax&gt;。以下是&lt;h:commandButton&gt;actionactionListener 属性定义的摘录:

    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

    【讨论】:

    • 是否可以用方法表达式代替值表达式? IE。 &lt;h:inputText value="#{myBean.invoke(string)}" /&gt;
    • @St.Antario: stackoverflow.com/…
    猜你喜欢
    • 2016-01-22
    • 1970-01-01
    • 2015-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-06
    • 2014-03-10
    • 1970-01-01
    相关资源
    最近更新 更多