3

I'm using the Struts 2 framework. I have an action class and a JSP page. The class has the following two methods:

public String foo(String s)
{
    return s + s;
}

public String bar(SomeEnumClass EnumType)
{
    switch(EnumType)
    {
         //Return a string depending on the type. 
    }
}

I can call the first method using this in my .JSP file:

<s:property value="%{foo('some string here')}"/>

and it will correctly return to my .JSP:

some string heresome string here

I'm now trying to figure out how to call the second method. How can I let Struts 2 know the "string" EnumTypeA should be typecasted to the corresponding EnumType?

<s:property value="%{bar('EnumTypeA')}"/>

Struts offers a lot of built-in type conversion, but string to enum doesn't appear to be one of them?

1 Answer 1

4

You can access enum from JSP using following notation:

<s:property value="bar(@package.SomeEnumClass@EnumTypeA)"/>
2
  • Worked perfectly! Just for reference for future readers: Do you need to add this line to struts.xml or not? <constant name="struts.ognl.allowStaticMethodAccess" value="true"/> Apr 9, 2014 at 17:33
  • For accessing enum you don't need to enable struts.ognl.allowStaticMethodAccess. Apr 9, 2014 at 17:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.