4

I just can't see it. I want to do the equivalent of the following:

listValue="%{capitalize(remoteUserName)}"

inside an s:select tag.

According to the struts documentation http://struts.apache.org/2.0.11.2/struts2-core/apidocs/com/opensymphony/xwork2/inject/util/Strings.html there exists a capitalize function. I have tried both the above and Strings.capitalize to try to capitalize remoteUserName.

Wandering through what is left of OGNL documentation at http://incubator.apache.org/ognl/, I don't see a way immediately to capitalize in this way.

So what then is the syntax to capitalize when using struts 2 tags?

EDIT:

I realize that the idea I put forth was to capitalize just the first letter of the word. Really, I would like each character in the word capitalized.

demongolem
  • 9,474
  • 36
  • 90
  • 105

1 Answers1

5

Here is an example of using com.opensymphony.xwork2.inject.util.Strings (has been tested)

<s:property value="@com.opensymphony.xwork2.inject.util.Strings@capitalize(myString)"/>

This requires that static method invocation be enabled, to do that simply add,

<struts>
    <constant name="struts.ognl.allowStaticMethodAccess" value="true"/> 
</struts>

into struts.xml

Edit: Just so others know (you probably already do) you can use any of the methods of java.lang.String ie: myString.toUpperCase() is a valid expression and you could use a regular expression and the java.lang.String methods replaceFirst or replaceAll to achieve the desired result.

If com.opensymphony.xwork2.inject.util.Strings capitalize method does not meet your needs this question covers other methods which might be useful: How to capitalize the first character of each word in a string

Community
  • 1
  • 1
Quaternion
  • 10,380
  • 6
  • 51
  • 102
  • `` is exactly the type of solution I was looking for. I already had allowStaticMethodAccess inside of my struts.xml file. However, I misstated what I was looking for. Your fix does what is advertised, however I really need the entire word capitalized. That is my mistake in communication. – demongolem Jun 29 '11 at 18:49
  • That is much easier, you don't need staticMethodAccess, just write myString.toUppercase() – Quaternion Jun 29 '11 at 18:51
  • Change your comment to myString.toUpperCase() and I can accept your solution :) Thanks. – demongolem Jun 29 '11 at 18:58