将Enum与ActionParam一起使用

时间:2012-10-22 09:06:13

标签: jsf richfaces

我在使用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值。我怎么能这样做?

1 个答案:

答案 0 :(得分:0)

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

首先,JSF页面必须能够在枚举上“迭代”以发现可能的值。我使用h:selectOneMenu作为示例,使用f:selectItems填充。 f:selectItems需要输入List<>,因此我们需要在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。当然这不起作用,因为该方法不存在,但它只是一个例子。 ; - )