Passing a Enum value as a parameter from JSF
Asked Answered
P

2

21

I am trying to migrate my existing code to using Enum and I run into some problems due to my lack experience with Enum. First of all here is my structures. In my EJB, alongs with Entity, I have a enum class (not sure if it even a class).

public enum Type {
    PROFILE_COMMENT,
    GROUP_COMMENT
} 

At my managed bean myBean.java, I have

@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);
    }

}

then at my JSF,

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

I got java.lang.ClassNotFoundException: saying Type is not a class

The reason I have Type in my EJB so that I can create an enumerated type for my Entity, so my query would look like this

select c from X c where c.type = Type.PROFILE_COMMENT
Postmeridian answered 12/10, 2010 at 16:24 Comment(0)
C
45

You can't access enums like that in EL. JSF has however builtin enum converters for EL. You can just use the enum name as string.

<h:commandButton value="Test" action="#{myBean.Test('PROFILE_COMMENT')}" />
Cotta answered 12/10, 2010 at 17:42 Comment(2)
@BalusC: Although this worked for me while using glassfish but after migrating to tomcat 7 this failed, could you point out why ?Byrnie
here is the link to details: #9534630Byrnie
A
0

In my case that helped me.

Simple compare enum to its value. EL recognize it and also check if that value exists while validating xhtml.

<c:if test="#{requestManager.selectedRequestType == 'ItemCreate' or requestManager.selectedRequestType == 'ItemChange'}"></c:if>
Abbacy answered 27/4, 2016 at 16:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.