10

How can I convert a String in h:outputText? Here is the code of h:outputText:

<h:outputText value="#{item.label} : " />

I tried using this,

<s:convertStringUtils format="capitalize" trim="true"/>

But it gives me error : "no tag was defined for name: convertStringUtils"

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
user1555524
  • 195
  • 1
  • 2
  • 9

4 Answers4

22

There are several ways.

  1. Use CSS text-transform: capitalize property.

    <h:outputText value="#{bean.text}" styleClass="capitalized" />
    

    with

    .capitalized {
        text-transform: capitalize;
    }
    
  2. Create a custom Converter.

    <h:outputText value="#{bean.text}" converter="capitalizeConverter" />
    

    with

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object modelValue) {
        if (modelValue == null || ((String) modelValue).isEmpty()) {
            return null;
        }
    
        String string = (String) modelValue;
        return new StringBuilder()
            .append(Character.toTitleCase(string.charAt(0)))
            .append(string.substring(1))
            .toString();
    }
    
  3. Use OmniFaces' of:capitalize() function.

    <html ... xmlns:of="http://omnifaces.org/ui">
    ...
    <h:outputText value="#{of:capitalize(bean.text)}" />
    

The <s:convertStringUtils> which you're trying is not from Seam. It's from MyFaces Sandbox.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • I do not want to convert to uppercase, i want to make capitalized. But this (number 5) can be applied to make it capitalized as well. Thanks for you efforts. – user1555524 Nov 17 '12 at 02:10
  • Oh right, fixed answer :) CSS one is the easiest IMO. It's purely presentational, right? – BalusC Nov 17 '12 at 02:20
  • Upon implementation of css option, I noticed that if the string contained '/' or '-'. Then the text would not transform to uppercase. – Eric Oct 20 '16 at 21:34
1

The following works with JSF 1.2 and Seam 2.x. It might work without Seam, but I can't recall exactly how Seam extends EL in Java EE 5.

<h:outputText value="#{item.label.toUpperCase()} : " />

<!-- If your string could be null -->
<h:outputText value="#{(item.label != null ? item.label.toUpperCase() : '')} : " />
Arjan Tijms
  • 37,782
  • 12
  • 108
  • 140
S Balough
  • 183
  • 4
  • 1
    This answer is incorrect in 2 ways: 1) capitalizing is definitely not the same as uppercasing, capitalizing is uppercasing only the 1st character. 2) EL is null-safe, so that nullcheck makes no sense, you just directly use `#{item.label.toUpperCase()}`. – BalusC Dec 10 '12 at 02:39
  • Good point about unnecessary null checking for property chains in EL; I wasn't aware of this. I see that this evaluation behavior is specified in the [EL 2.2 spec](http://download.oracle.com/otn-pub/jcp/jsp-2.1-fr-eval-spec-oth-JSpec/jsp-2_1-fr-spec-el.pdf?AuthParam=1355113088_64a7284fa52e13380c220bc6d64324f4). – S Balough Dec 10 '12 at 04:26
0

create a getter method in your data bean

public String getCapitalizeName(){
  return StringUtils.capitalize(getName());
}

and on xhtml

<houtputText value="#{yourDataBean.capitalizeName}"/>
jmj
  • 237,923
  • 42
  • 401
  • 438
0

As @BalusC said you can use text-transform: capitalize;. But it converts the first letter of each word in your sentence to uppercase. If your requirement is that, that is the best answer because
1. It is easier
2. text-transform: capitalize; is supported by all major browsers.

However if you want to capitalize only the very first letter of the sentence you can do something like this.

public String getLabel() {
  if(label != null && !label.isEmpty()) {
    return Character.toUpperCase(label.charAt(0)) + label.substring(1);
  }
  return label;
}

I don't think that JBoss Seam has a <s:convertStringUtils> tag. As I think such a tag is available in Apache MyFaces. Don't know much about that.

prageeth
  • 7,159
  • 7
  • 44
  • 72