将枚举与ActionParam一起使用 [英] Using a Enum with a ActionParam

查看:80
本文介绍了将枚举与ActionParam一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using the following piece of code in my JSF 2.0 with RichFaces 4.0. I have a managed bean that has an enum. Now i want to assign the value of the enum via an ActionParam. How can I do this? Here is the code:

<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值.我该怎么办?

Now I want to assign the value of the transactionType variable to an Enum value. How can I do this?

推荐答案

我不知道您想对变量做什么或如何显示它,所以这是一个通用示例.

I don't know what you want to do with the variable or how you want to display it, so here's a generic example.

首先,JSF页面必须能够在枚举上迭代"以发现可能的值.我以h:selectOneMenu为例,使用f:selectItems进行了填充. f:selectItems需要一个List<>作为输入,因此我们需要在TellerBean中创建一个方法:

First of all, the JSF page must be able to 'iterate' over the enum to discover the possible values. I'm using h:selectOneMenu as an example which is filled using f:selectItems. f:selectItems expects a List<> as input so we need to create a method in the 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页面为例:

Then for an example JSF page:

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

JSF页面应显示一个带有枚举值的下拉列表.单击标记为提交"的按钮时,它将在TellerBean中执行someMethod().当然,这是行不通的,因为该方法不存在,但这只是一个示例. ;-)

The JSF page should display a drop-down list with the values of the enum. When clicking the button labeled "Submit" it executes someMethod() in TellerBean. Of course this doesn't work because the method doesn't exist, but it's just an example. ;-)

这篇关于将枚举与ActionParam一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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