<...">

Struts 2 if: compare enum

I have 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 that java code is behind categories ...

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

Thus, the IF statement does not work; it never evaluates to true. I tried to get away from charaters, etc., but without success.

I would rather make a callback to the Action class with a value of "category" and decide what to do. eg.

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

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

So can anyone help me figure out how to pass the value backwards or make it work with the Struts IF tag with an enumeration.

I already read this: it doesn’t help much here, but I still link to it:

Struts 2: why if if tag doesn't evaluate single char string

can someone help me?

Jeff Porter

+4
source share
2 answers

It seems that I did not look at the CategoryEnum class (only if .class was not attached .java).

The CategoryEnum class, although this is an enumeration, the values ​​are their own class, therefore, therefore, calling toString () on it will allow me to compare the value inside each of them.

 <s:iterator value="categories" id="category" status="iteratorStatus"> <s:if test='#category.toString() == "M" '> snip </s:if> 
+9
source

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

Can you use real Java 1.5 enums or maby create in your action a new list of String values ​​before the page is displayed?

+1
source

All Articles