tags:

views:

567

answers:

2

Hello all,

I have a Struts 2 jsp with the following code...

<s:iterator value="categories" id="category" status="iteratorStatus">
<s:if test='#category == "M" '>  snip </s:if>

The problem is the java code behind categories is..

    private static final CategoryEnum[] PRIVATE_VALUES = {A,B,C,M   };
    public static final List<CategoryEnum> VALUES = Collections.unmodifiableList(Arrays.asList(PRIVATE_VALUES));
    public List<CategoryEnum> getCategories() {
    return CategoryEnum.VALUES;
}

So the IF statement does not work, it never evalutes to true. I've tried escaping the charaters etc, but with no success.

I'd prefer to make a call back to the Action class with the 'category' value and have that decied what to do. e.g.

<s:if test='renderCategory(#category)>  snip </s:if>

but I don't know how to pass the #category back to the action.

So can anyone help either me work out how to pass the value back or to make the Struts IF tag work with an enum.

I've already read this: which isn't much help here, but I'll reference iy anyway:

Struts 2: Why won't the 'if' tag evaluate a one char string

can anyone help me please?

Jeff Porter

+1  A: 

It is not clear to me from your example what is the PRIVATE_VALUES array? it looks like A,B,C and M are instances of CategoryEnum. In that case, you are comparing a CaterogyEnum to a string in your OGNL expression, so that's why it is failing.

Can you use real Java 1.5 enums, or maby create on your action a new list of String values, before the page renders?

Yoni
yep, PRIVATE_VALUES is an array of CaterogyEnum.I'll give you a +1 for making me look more closly at the CaterogyEnum instances.
jeff porter
+2  A: 

It seem that I didn't look at the CategoryEnum class (only had the .class attached not the .java).

CategoryEnum class although it is a enum, the values are their own class, so hence calling toString() on it will allow me to compare the value inside each one.

<s:iterator value="categories" id="category" status="iteratorStatus">
<s:if test='#category.toString() == "M" '>  snip </s:if>
jeff porter
just what I was looking for! Thanks a lot...
Nischal