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>
Roman C
  • 49,761
  • 33
  • 66
  • 176
kosmičák
  • 1,043
  • 1
  • 17
  • 41

2 Answers2

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="@package.ENUM@enumvalue.toString() == 'some_enum_as_string'">
Aleksandr M
  • 24,264
  • 12
  • 69
  • 143
4

Got it, this works for me:

<s:if test="%{company.affiliateId != @com.foo.bar.Affiliates@XZY.asBigDecimal() }">
kosmičák
  • 1,043
  • 1
  • 17
  • 41
  • 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; } – kosmičák Nov 14 '12 at 10:33