将Enum值作为参数从JSF传递

Tha*_*ham 19 java jsf enums el

我正在尝试将现有代码迁移到使用Enum,由于我缺乏Enum的经验,我遇到了一些问题.首先,这是我的结构.在我的EJB实体中,我有一个枚举类(不确定它是否甚至是一个类).

public enum Type {
    PROFILE_COMMENT,
    GROUP_COMMENT
} 
Run Code Online (Sandbox Code Playgroud)

在我的托管bean myBean.java,我有

@ManagedBean(name="myBean")
@SessionScoped
public class myBean {

    private Type type;

    public myBean() {
    }

    public Type getType() {
        return type;
    }

    public void setType(Type type) {
        this.type = type;
    }

    public void Test(Type t){
        System.out.println(t);
    }

}
Run Code Online (Sandbox Code Playgroud)

然后在我的JSF,

<h:commandButton value="Test" action="#{myBean.Test(myBean.type.PROFILE_COMMENT)}" />
Run Code Online (Sandbox Code Playgroud)

java.lang.ClassNotFoundException:Type的不是课

Type在EJB中的原因是我可以为我的Entity创建一个枚举类型,所以我的查询看起来像这样

select c from X c where c.type = Type.PROFILE_COMMENT
Run Code Online (Sandbox Code Playgroud)

Bal*_*usC 41

您无法访问EL中的枚举.然而,JSF内置了EL的枚举转换器.您可以将枚举名称用作字符串.

<h:commandButton value="Test" action="#{myBean.Test('PROFILE_COMMENT')}" />
Run Code Online (Sandbox Code Playgroud)