Question

Is there any way to convert a string to Title case, using JSTL tags?

Thanks in Advance.

Was it helpful?

Solution

An alternative to transforming the string on the server is to let CSS do the work:

text-transform: capitalize

OTHER TIPS

An idea:

In a class, create a simple method that uses the WordUtils from Apache Commons Lang that will manipulate your String:

import org.apache.commons.lang.WordUtils;

...

public static String titleCase(String input){
   return WordUtils.capitalize(input);;
}

And now, create your own tag (in a function.tld) :

<?xml version="1.0" encoding="UTF-8" ?>
<taglib xmlns="http://java.sun.com/xml/ns/j2ee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
  version="2.0">
  <description>functions library</description>
  <display-name>functions</display-name>
  <tlib-version>1.1</tlib-version>
  <short-name>xfn</short-name>
  <uri>http://yourdomain/functions.tld</uri>
  <function>
    <description>
      Title case a String
    </description>
    <name>titleCase</name>
    <function-class>Functions</function-class>
    <function-signature>java.lang.String titleCase(java.lang.String)</function-signature>
    <example>
      ${xfn:titleCase(string)}
    </example>
  </function>
</taglib>

ps: I was quite inspired from this post to give my answer.

It's not too super hard in JSTL...

${fn:toUpperCase(fn:substring(user.firstName, 0, 1))}${fn:toLowerCase(fn:substring(user.firstName, 1, -1))}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top