在JSF中分配“值表达式"代替“方法表达式" [英] Assign 'value expression' in place of 'method expression' in JSF

查看:142
本文介绍了在JSF中分配“值表达式"代替“方法表达式"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的复合组件中,我迭代了list<list<javaDetailClass>>.我通过#{iterator.value}之类的值表达式获得了所有<h:commandButon>属性的值.但是问题出在属性action上,其中操作仅接受method expression.而我只能在那里分配值表达式,导致MethodNotFoundException

In my composite component, I iterate a list<list<javaDetailClass>>. I get all my <h:commandButon> attribute's values through value expression like #{iterator.value}. But the problem comes with attribute action, where action accepts only method expression. whereas I can assign only value expression there, resulting in 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>

有人可以帮助我解决此问题吗? 预先感谢.

Can anyone help me in fixing this? Thanks in advance.

更新

在我的情况下,这将是细节类,

this will be the detail class in my situation,

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对象,并为其属性分配了一些默认值.

Bean.java simply holds an ArrayList of ArrayList. The constructor simply created five TestData objects and assigns some default value to its attributes.

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属性相关联即可调用组合

index.html simply calls the composite by assinging the value of "#{bean.list} to the name attribute

推荐答案

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

I'm assuming that your TestData.java has the following method public String getAction() (since I'm seeing a setAction(String)) and not public String action(). Therefore, the reason why you are getting a MethodNotFoundException is because you are supplying the wrong method name to the action attribute. In your case it should be iterator.getAction and not iterator.action. You only supply the abbreviated names when an attribute is expecting a value expression. The interface below has been modifed.

    <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>

这篇关于在JSF中分配“值表达式"代替“方法表达式"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆