0

我在带有 RichFaces 4.0 的 JSF 2.0 中使用以下代码。我有一个有枚举的托管 bean。现在我想通过 ActionParam 分配枚举的值。我怎样才能做到这一点?这是代码:

<a4j:commandLink id="pendingTransactions"
        action="#{tellerBean.getPendingTransactions}" value="Show Pending"
        styleClass="button category-btn">
    <a4j:actionparam name="first" value=""
        assignTo="" />  
</a4j:commandLink>

和我的托管 bean:

@ManagedBean
@SessionScoped
public class TellerBean implements Serializable{

    public enum TransactionType {
        PENDING,PROCESSED,ALL
    }

    private static final long serialVersionUID = -321111;
    private String recipientID;
    private String recipientName;
    private String transactionAmount;
    private TransactionType transactionType;


    public String getRecipientID() {
        return recipientID;
    }

    public void setRecipientID(String recipientID) {
        this.recipientID = recipientID;
    }

    public String getRecipientName() {
        return recipientName;
    }

    public void setRecipientName(String recipientName) {
        this.recipientName = recipientName;
    }

    public String getTransactionAmount() {
        return transactionAmount;
    }

    public void setTransactionAmount(String transactionAmount) {
        this.transactionAmount = transactionAmount;
    }

    public void searchTransactions() {}

    public TransactionType getTransactionType() {
        return transactionType;
    }

    public void setTransactionType(TransactionType transactionType) {
        this.transactionType = transactionType;
    }

    public void getTransactions() {}
}

现在我想将transactionType变量的值分配给 Enum 值。我怎样才能做到这一点?

4

1 回答 1

0

我不知道你想对变量做什么或者你想如何显示它,所以这里是一个通用的例子。

首先,JSF 页面必须能够“迭代”枚举以发现可能的值。我以h:selectOneMenu使用f:selectItems. f:selectItems期望 aList<>作为输入,因此我们需要在 中创建一个方法TellerBean

public List<TransactionType> getTransactionTypes()
{
    List<TransactionTypes> tt = new ArrayList<TransactionType>();
    for (TransactionType t : TransactionType.values())
    {
        tt.add(new TransactionType(t, t.toString()))
    }
    return tt;
}

然后是一个示例 JSF 页面:

<h:form>
    <h:selectOneMenu value="#{tellerBean.transactionType}">
        <f:selectItems value="#{tellerBean.transactionTypes}"/>
    </h:selectOneMenu>
    <h:commandButton value="Submit" action="#{tellerBean.someMethod}"/>
</h:form>

JSF 页面应该显示一个包含枚举值的下拉列表。单击标有“提交”的按钮时,它会someMethod()TellerBean. 当然这不起作用,因为该方法不存在,但这只是一个示例。;-)

于 2012-10-22T10:05:47.620 回答