In my JSF 2.2 application, when I create JSF form, the problem is that I can put spaces in the beginning of <h:inputText>
when I insert it from my form and it will return these spaces with my value that I inserted in it. So I should handle each value with mystring.tirm()
to void the spaces. Can I use any something else to return this value without spaces? I know about converter and JavaScript, so can you give me another option to use? I don't want to use any converter and JavaScript.
I don't want to use any converter and JavaScript
There's no magic here. You have to write code to do the job. I gather that your concern is more that you don't want to repeat the same conversion job over every single input field.
In that case, just register a converter specifically on String.class
via the forClass
attribute of the @FacesConverter
like below.
@FacesConverter(forClass=String.class)
public class TrimConverter implements Converter {
@Override
public Object getAsObject(FacesContext context, UIComponent component, String submittedValue) {
String trimmed = (submittedValue != null) ? submittedValue.trim() : null;
return (trimmed == null || trimmed.isEmpty()) ? null : trimmed;
}
@Override
public String getAsString(FacesContext context, UIComponent component, Object modelValue) {
return (modelValue != null) ? modelValue.toString() : "";
}
}
This way you don't need to declare the converter in every single input field. It will get applied transparently on every String
model value which doesn't already have an explicit converter registered.
The JavaScript way is not recommended as it runs entirely at the client side and endusers can easily disable and manipulate JavaScript. The JSF converter runs at server side and its outcome is not manipulatable by the enduser.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With