7

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?

membersound
  • 81,582
  • 193
  • 585
  • 1,120

3 Answers3

8
<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.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Which converter is extended here? `import org.apache.myfaces.config.element.Converter;` would not work here as it would require a lot of override methods like `getConverterClass` `getConverterId` `getForClass` – membersound Oct 30 '12 at 09:39
  • Just the standard JSF converter. Have you never created one? It's the [`javax.faces.convert.Converter`](http://docs.oracle.com/javaee/6/api/javax/faces/convert/Converter.html). – BalusC Oct 30 '12 at 09:41
  • Ok we have to use *implements* here instead extends. Then it works, additionaly changing the *void* to String or Object. – membersound Oct 30 '12 at 09:42
  • Oops, sorry for that. Didn't had enough coffee at 05:00 am. Answer is been fixed. I should have used an IDE instead of the SO message editor. – BalusC Oct 30 '12 at 09:43
  • no problem, anyway this is probably the best solution for lower case. It worked of course with the conversation within the setter, but as you said cluttering the model should be avoided. – membersound Oct 30 '12 at 09:44
  • One more suggestion to have a complete example here: `if (!(value instanceof String)) { return ""; }` as we cannot return an object if method is a String. – membersound Oct 30 '12 at 09:47
  • @ᴠɪɴᴄᴇɴᴛ: I usually don't add imports unless there's ambiguity. Just let IDE autocomplete imports and verify if it compiles. Moreover, the `Converter` link in my answers points to the javadoc. – BalusC Jan 12 '16 at 20:51
  • @ᴠɪɴᴄᴇɴᴛ: missed that due to the double comment, I only saw last one. – BalusC Jan 13 '16 at 18:40
4

You can use jstl function for this

include: <%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>

and use < h:inputText value="${fn:toLowerCase(string)}"/>

Or you can make username in lowercase in java by .toLowerCase().

Kishor Sharma
  • 599
  • 8
  • 15
1

This way should work. As soon as you set the name, the name is lowecased.

String name;

public void setName(String name){
 this.name = name.toLowerCase();
}
Averroes
  • 4,168
  • 6
  • 50
  • 63