struts2: enum in IF
Asked Answered
L

2

8

How does the following Java condition translate into s:if test="..." in struts2?

if(company.getAffiliateId().asInt() != com.foo.bar.Affiliates.XYZ.asInt()){
 // do something
}

company.getAffiliateId() returns BigDecimal

com.foo.bar.Affiliates is an enum

This doesn't work:

<s:if test="%{company.affiliateId.asInt() != com.foo.bar.Affiliates.XYZ.asInt() }">
   alert("do something");
</s:if>
Lycaonia answered 14/11, 2012 at 9:55 Comment(0)
H
9

Use toString method to compare enums.

<s:if test="ENUM.toString() == 'some_enum_as_string'">

And if you want to use enums in JSP

<s:if test="@[email protected]() == 'some_enum_as_string'">
Harbinger answered 14/11, 2012 at 10:19 Comment(0)
L
4

Got it, this works for me:

<s:if test="%{company.affiliateId != @[email protected]() }">
Lycaonia answered 14/11, 2012 at 10:25 Comment(1)
I think the enum class was generated based on a database table. There are no static methods in it: private int id; private Affiliates(int id) { this.id = id; } public BigDecimal asBigDecimal() { return BigDecimal.valueOf(id); } public int asInt() { return id; }Brodench

© 2022 - 2024 — McMap. All rights reserved.