【问题标题】:Assign 'value expression' in place of 'method expression' in JSF在 JSF 中分配“值表达式”代替“方法表达式”
【发布时间】:2013-06-13 06:13:45
【问题描述】:

在我的复合组件中,我迭代了一个list<list<javaDetailClass>>。我通过像#{iterator.value} 这样的值表达式来获取我所有的<h:commandButon> 属性值。但问题在于属性action,其中action 只接受method expression。而我只能在那里分配值表达式,导致MethodNotFoundException

<cc:interface>
    <cc:attribute name="formElements" />
</cc:interface>
<cc:implementation>
    <c:forEach items="#{cc.attrs.formElements}" var="element">
        <c:forEach items="#{element}" var="iterator">

                <h:commandButton id="#{iterator.id}" 
                value="#{iterator.value}"

                action="#{iterator.action}">

                </h:commandButton>
        </c:forEach>
  </c:forEach>
</cc:implementation>

谁能帮我解决这个问题? 提前致谢。

更新

这将是我的情况下的详细信息类,

package com.stackoverflow.test;

public class TestData {

/*Properties based on the implementation of your composite.
Change type where it is needed*/
private String id; 
private String value; 
private String attributeName; 
private String action; 

public TestData() {
}

/*Getters and setters omitted*/


}

Bean.java 只包含一个ArrayList 的ArrayList。构造函数只是创建了五个 TestData 对象并为其属性分配了一些默认值。

package com.stackoverflow.test;

import java.util.ArrayList;
import javax.faces.bean.*; 

@ManagedBean
@RequestScoped
public class Bean {

private ArrayList<ArrayList<TestData>> list = new ArrayList<ArrayList<TestData>>(); 

public Bean() {
    ArrayList<TestData> testDataList = new ArrayList<TestData>(); 
    TestData data; 

    for(int i = 0; i < 5; i++) { 
        data = new TestData(); 
        data.setId("ID" + i);
        data.setValue("VALUE" + i);
        data.setAttributeName("ATTRIBUTE" + i);
        /**this sets the action attribute of TestData with a API from some other managed bean**/
        data.setAction("someOtherManagedbean.someactionAPI");
        testDataList.add(data);
    }

    list.add(testDataList); 
}

public ArrayList<ArrayList<TestData>> getList() {
    return list;
}

public void setList(ArrayList<ArrayList<TestData>> list) {
    this.list = list;
}

}

index.html 只是通过将 "#{bean.list} 的值赋给 name 属性来调用组合

【问题讨论】:

  • try action="#{iterator.action.toString}"
  • @zargarf:感谢您的评论...但它不起作用
  • @zargarf:在发布可能的废话之前请先尝试一下。

标签: jsf action el commandbutton


【解决方案1】:

我假设您的 TestData.java 具有以下方法 public String getAction()(因为我看到的是 setAction(String))而不是 public String action()。因此,您获得MethodNotFoundException 的原因是您为action 属性提供了错误的方法名称。在您的情况下,它应该是 iterator.getAction 而不是 iterator.action。仅当属性需要值表达式时才提供缩写名称。下面的界面已经修改。

    <cc:interface>
        <cc:attribute name="formElements" />
    </cc:interface>

    <cc:implementation>
        <c:forEach items="#{cc.attrs.formElements}" var="element">
            <c:forEach items="#{element}" var="iterator">
                <h:commandButton id="#{iterator.id}" 
                                 value="#{iterator.value}"

                                 action="#{iterator.getAction}">
                </h:commandButton>
            </c:forEach>
        </c:forEach>
    </cc:implementation>

【讨论】:

    猜你喜欢
    • 2023-03-17
    • 1970-01-01
    • 2018-01-10
    • 2011-12-30
    • 2020-01-10
    • 1970-01-01
    • 2022-01-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多