Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert string to lowercase for property binding within xhtml?

How could I always convert the property username to lower case?

This does not work:

<h:inputText value="#{userService.user.username.toLowerCase()}" />

as target gets "unreachable" by this. How could I else do this?

like image 617
membersound Avatar asked Oct 30 '12 08:10

membersound


People also ask

How do I convert a string to lowercase in HTML?

The toLowerCase() method returns the value of the string converted to lower case. toLowerCase() does not affect the value of the string str itself.

How do you convert strings to lowercase?

The toLowerCase() method converts a string to lower case letters. Note: The toUpperCase() method converts a string to upper case letters.

How do you convert a string to lowercase in node?

The toLowerCase() method converts a string to lowercase letters. The toLowerCase() method does not change the original string.

How do I convert a string to lowercase in TypeScript?

The toLowerCase() is an inbuilt function in TypeScript which is used to convert the characters within a string to lowercase.


1 Answers

<h:inputText value="#{userService.user.username.toLowerCase()}" />

You can't perform a "set" operation on the given EL expression, while this is mandatory in order to update the model value with the submitted value. The toLowerCase() method expression doesn't have a corresponding setter method.

Technically, you should be creating a Converter for the job. Here's a kickoff example:

@FacesConverter("toLowerCaseConverter")
public class ToLowerCaseConverter implements Converter {

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object modelValue) {
        if (!(modelValue instanceof String)) { 
            return null; // Or throw ConverterException.
        }

        return ((String) modelValue).toLowerCase();
    }

    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String submittedValue) {
        if (submittedValue == null) { 
            return null;
        }

        return submittedValue.toLowerCase();
    }

}

Use it as follows:

<h:inputText value="#{userService.user.username}" converter="toLowerCaseConverter" />

You should preferably not clutter the model with conversion strategies.

like image 73
BalusC Avatar answered Sep 30 '22 09:09

BalusC