124697
124697

Reputation: 21893

How to format output in JSTL

I've got a span like this

<span>${bean.name}</span>

and it returns something like John Brown

how can i format it so it shows like BROWN, John in jstl?

Upvotes: 1

Views: 888

Answers (1)

BalusC
BalusC

Reputation: 1108732

So, to the point, you want to apply the following modifications on the String:

  1. Split in 2 parts on whitespace (what to do if there are more whitespaces?)
  2. Show the 2nd part of the split in uppercased flavor.
  3. Show a comma and then a space.
  4. Show the 1st part of the split.

This is all doable with JSTL functions.

  1. <c:set var="parts" value="${fn:split(bean.name, ' ')}" />
  2. ${fn:toUpperCase(parts[1])}
  3. ,
  4. ${parts[0]}

Summarized:

<c:set var="parts" value="${fn:split(bean.name, ' ')}" />
${fn:toUpperCase(parts[1])}, ${parts[0]}

You've only another problem when the name contains more than one space.

Upvotes: 2

Related Questions