views:

538

answers:

3

HI,

I have an input field in which I want to trim any leading/trailing whitespaces. We are using JSF and binding the input field to a backing bean in the jsp using:

<h:inputText id="inputSN" value="#{regBean.inputSN}" maxlength="10"/>

My question is that besides validation can this be done in the jsp? I know we can also do this using the trim() java function in the Handler, but just wondering if there is a more elegant way to achieve this in JSF.

Thanks.

+1  A: 

You could use a Converter (tutorial).

McDowell
And register it with `converter-for-class` for `java.lang.String` so that you don't need to define it on every `UIInput` component.
BalusC
what is this converter-for-class? sounds cool
Martlark
@Martlark - it is an element used in `faces-config.xml`. From spec: _/faces-config/converter -- Create or replace a converter id/converter class or target class/converter class pair with the Application instance for this web application._ Also: _The “converter-for-class” element represents the fully qualified class name for which a Converter class will be registered._ See the JSF specification for more.
McDowell
A: 

I resolved this by just using the trim() function in the handler before doing any processing. it just seemed like the most straight forward way of doing this.

msharma
You should mark this as accepted answer, if this is what you feel is the correct answer.
Shervin
A: 

I answered a similar question here

Basically you can either create your own component that is a copy of inputText which automatically trims, or you can extend the inputText and add trim attribute that trims if true.

Shervin