converting strings to Title case in JSTL

こ雲淡風輕ζ 提交于 2019-12-22 03:53:01

问题


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

Thanks in Advance.


回答1:


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

text-transform: capitalize



回答2:


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.




回答3:


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

${fn:toUpperCase(fn:substring(user.firstName, 0, 1))}${fn:toLowerCase(fn:substring(user.firstName, 1, -1))}


来源:https://stackoverflow.com/questions/482925/converting-strings-to-title-case-in-jstl

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!