11

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

Thanks in Advance.

  • Great question, and yet another short-coming of JSTL. Title case is a very basic display need. These answers are all good and valid, but I find it insulting that the creators of JSTL failed to include this functionality directly. – Xtopher Jul 12 '18 at 15:30

3 Answers3

16

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

text-transform: capitalize
McDowell
  • 107,573
  • 31
  • 204
  • 267
  • cool. It helped, since I needed the Title Case only for presentation purpose, CSS would be a better solution. Thanks. –  Jan 28 '09 at 06:04
  • Why always looking for complex answer? Your solution is really the best one! – Romain Linsolas Jan 28 '09 at 08:30
  • 3
    In a way, this isn't a very good answer to "how can I do it in JSTL". I like romaintaz's explanation, since it better covers how someone could do it in JSTL. – Nick Spacek Oct 12 '11 at 12:00
  • @Nick - I think that's a fair comment but it does no harm to read between the lines occasionally and suggest alternatives. – McDowell Oct 12 '11 at 12:49
  • 3
    This only works with certain inputs with CSS. For example if my input Data is ALL UPPERCASE, capitalize will not work, it will leave my text as is in it's uppercase format. EX: someone enters last name in all caps. I want to display this name with proper/title case, then no go using CSS, the name will be displayed all caps. – blo0p3r May 02 '12 at 15:39
  • This one works for all upper case too. .m_name{ text-transform:lowercase; } .m_name:first-letter{ text-transform:uppercase; } – iBabur Mar 21 '18 at 17:38
8

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

${fn:toUpperCase(fn:substring(user.firstName, 0, 1))}${fn:toLowerCase(fn:substring(user.firstName, 1, -1))}
Paul D. Waite
  • 96,640
  • 56
  • 199
  • 270
jonnad
  • 91
  • 1
  • 1
8

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.

Community
  • 1
  • 1
Romain Linsolas
  • 79,475
  • 49
  • 202
  • 273