如何使用 JSTL/CSS 将首字母大写?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5896953/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to capitalize first letter with JSTL/CSS?
提问by stivlo
I am coding a JSP/JSTL application. I would like to style a link with the first letter uppercase and the rest lowercase. For example "my LINK" would become "My Link".
我正在编写一个 JSP/JSTL 应用程序。我想用第一个字母大写和其余小写字母来设计一个链接。例如,“我的链接”将变成“我的链接”。
I saw that in CSS I can do:
我在 CSS 中看到我可以这样做:
<a href="..." style="text-transform: capitalize">${linkName}</a>
Which works only when ${linkName} is all lower case, but doesn't work as I want when is uppercase for instance if it contains "MY LINK" will be still displayed all uppercase.
仅当 ${linkName} 全部为小写时才有效,但在大写时不起作用,例如如果它包含“MY LINK”仍将全部显示为大写。
I was wondering what is the best way to solve this problem, for instance it could be to use JSTL to convert ${linkName} to lower case.
我想知道解决这个问题的最佳方法是什么,例如可能是使用 JSTL 将 ${linkName} 转换为小写。
Anyone knows how to do that? Or any alternative way?
有谁知道怎么做?或者有什么替代方法?
Thanks in advance!
提前致谢!
回答by BalusC
You can use JSTLfunctions fn:toLowerCase()
to lowercase a string.
您可以使用JSTL函数fn:toLowerCase()
来小写字符串。
So, this should do
所以,这应该做
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
...
<a href="..." style="text-transform: capitalize">${fn:toLowerCase(linkName)}</a>
回答by Miguel Angel Fernandez
If you do not want to use CSS and only use JSTL, this solution have a bit extreme:
如果不想用CSS,只用JSTL,这个方案有点极端:
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<a href="..." >
${fn:toUpperCase(fn:substring(linkName, 0, 1))}${fn:toLowerCase(fn:substring(linkName, 1,fn:length(linkName)))}
</a>
回答by WoodenKitty
Apache Commons offers libraries to do lots of common useful tasks. WordUtils can help you here.
Apache Commons 提供了一些库来完成许多常见的有用任务。WordUtils 可以在这里为您提供帮助。
WordUtils.capitalizeFully("aaa BBB cCc");
Would output...
会输出...
Aaa Bbb Ccc
Aaa Bbb Ccc
Reference for WordUtils: http://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/text/WordUtils.html#capitalizeFully%28java.lang.String%29
WordUtils 参考:http://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/text/WordUtils.html#capitalizeFully%28java.lang.String%29
On my web app I added a wrapper for this in my TLD file...
在我的网络应用程序中,我在 TLD 文件中为此添加了一个包装器...
<function>
<name>capitalize</name>
<function-class>org.apache.commons.lang3.text.WordUtils</function-class>
<function-signature>java.lang.String capitalizeFully(java.lang.String)</function-signature>
</function>
So now I can do this...
所以现在我可以做到这一点......
<p>Hello ${blah:capitalize(firstName)}</p>
I will leave you to read up on custom TLD files though, since other people explain it better.
不过,我会让你阅读自定义 TLD 文件,因为其他人会更好地解释它。
回答by Shinto Anto
PFB the below code which would convert "i AM god" to "I Am God"
PFB 下面的代码会将“我是上帝”转换为“我是上帝”
<c:forEach var="word" items="${fn:split(fn:toLowerCase(stringToBeConverted),' ')}">
<c:set var="formattedText" value="${formattedText} ${fn:toUpperCase(fn:substring(word,0,1))}${fn:toLowerCase(fn:substring(word,1,fn:length(word)))}" />
</c:forEach>