Anup Nadahalli
Anup Nadahalli

Reputation: 23

Splitting a string in the value attribute of Struts2 Iterator tag

I am stuck at one point in my project and I need some help. Below is my requirement.

I have a string(for ex: exampleString) which I am storing in session as follows:

request.getSession().setAttribute("exampleString",exampleString);

The exampleString will be like abc|def|ghi|jkl

Now in JSP I need to split it on "|" for using it in the Struts2 Iterator tag. For this I have written code as below:

<s:iterator var='item' value='#session.exampleString.split("|")'>
Remaining Code
</s:iterator>

Mow my problem is if I check the 'item' , the exampleString is not splitting up and the values that I am getting is a b c | d e f | g h i | j k l

I have also tried as follows:

<s:iterator var='item' value='#session.exampleString.split("\\|")'>
Remaining Code
</s:iterator>

The above code doesn't split the string at all.

I need to know where I am going wrong.

PS: This is my first post in this forum. Please let me know if I have done any mistakes while posting.

Upvotes: 2

Views: 2010

Answers (1)

Aleksandr M
Aleksandr M

Reputation: 24396

You need to escape \ as well so use:

<s:iterator var='item' value='#session.exampleString.split("\\\|")'>
  Remaining Code
</s:iterator>

Upvotes: 2

Related Questions