Aug 10

Struts 2 enum form value

If you want to use enums in jsps while employing the struts 2 framework, add the following in your xwork-conversion.properties file:

java.lang.Enum = com.opensymphony.xwork2.util.EnumTypeConverter

Aug 10

Struts 2 test enum

In my Struts 2 app I needed to check for an enum value in the jsp. Using the struts <s:if test=”"> tag this can be done :

<s:if test=’#role.toString()!=”PAID”‘></s:if>

Hope this helps out someone. What I’m doing is getting the string representation of the enum and comparing the same. I did not know we could do a toString() here, hence thought of blogging about it!

Sep 08

Generate dynamic id’s for struts html tags

Do you use struts tags to generate html content? If yes, then sooner or later you’ll come across a scenario where you would generate html elements with the same name but would need different id’s for each element. I faced a similar predicament recently. I had no other option but to use scriptlets to generate id’s for the html elements. However, I soon found out using scriptlets within the struts html tags isn’t really straight forward.

Here’s the code I tried in my first attempt:

<%int i=0;%>

//iteration logic here

<html:text property=”example” styleid=”example<%=i%>”>

Well, if you write the code as shown above, the html code generated would be :

<input type=”text” name=”example” id = “example<%=i%>”>

and not

<input type=”text” name=”example” id=”example0″>

To get the expected result, i.e. for the scriptlet to work inside the struts html tag, write as below:

<html:text property=”example” styleid=’<%=”example”+i%>’

That’s it. If any of you ever find out a way to generate different id’s for html elements with the same name using strtus tags exclusively then do let me know :)