Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert String inside h:outputText into capitalize String?

Tags:

java

jsf

seam

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"

like image 263
user1555524 Avatar asked Nov 17 '12 01:11

user1555524


2 Answers

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.

like image 156
BalusC Avatar answered Oct 15 '22 09:10

BalusC


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() : '')} : " />
like image 37
S Balough Avatar answered Oct 15 '22 08:10

S Balough