wood burning stoves 2.0*
The moose likes JSF and the fly likes Convert Long to String inside JSF el expression Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » JSF
Bookmark "Convert Long to String inside JSF el expression" Watch "Convert Long to String inside JSF el expression" New topic

Convert Long to String inside JSF el expression

Hatem Alimam
Ranch Hand

Joined: Dec 14, 2008
Posts: 30
Hi ranchers..
as the title says, i have a .jsf page with a form
binded to an element in the backing bean..
now the object in the backing bean returns some values as Long objects
whereas i want it as String inside the form elements.
any way to convert it directly inside the el expression
something like this:

<h:inputText value="#{ convert long to string directly }" />

SCJP 5.0, SCWCD 1.4, SCBCD 3.0 with 100 score
Rohit Boorugu

Joined: Oct 06, 2009
Posts: 14
Hi Hatem, Its not a good idea to convert anything in an EL expression. It can have some issues with getters and setters if you do that. I would suggest you to do that in the backing bean like setLongvalue(getLongvalue.toString). why do you need to convert it in an EL expression? any specific reason for that.

Hope this helps
Hatem Alimam
Ranch Hand

Joined: Dec 14, 2008
Posts: 30
Hi Rohit ,

The reason of doing this is that I have a selectOneChoice which the value of it is a String and in my Bean the value is Long ...
So in this case if I give the value of the selectOneChoice directly from my bean class which it is a Long value , it don't understand it so it doesn't assign it to the right selected item.

so in this case I have to put a converter to it in other words a Holder, Inside the holder I do the conversion and assign these values to the selectOneChoice instead of assigning it directly from the bean classe. This is the first issue , The second is when I get the values back I have to reconvert them to Long so they fit in my bean for a DB process.

So I thought if there's any way to convert them to a String in the EL expression so I can drop one issue, Which it is the first one.

I hope I describe it clearly .

Thanks .
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15959

I think you're making things overcomplicated. On the displayed page (form), everything is a string. It's all generated as HTML and HTML is a text format. The EL converters handle converting bean properties to and from the text that's used on the form. Usually that's going to be the equivalent of invoking the String.valueOf and construct-from-string methods of basic Java.

Sometimes the standard rules don't have a way to handle that kind of stuff. For example, for specialized formatting of dates and times. That's when you'd add a converter to assist in the process. For primitive types that's not an issue.

Customer surveys are for companies who didn't pay proper attention to begin with.
Hatem Alimam
Ranch Hand

Joined: Dec 14, 2008
Posts: 30
aha that's right Tim,

But I think that the ADF <af:selectOneChoice> has another behavior then the plan Html ....

I am wondering why the values are not displayed correctly(put a certain selected item based on the value), When I convert my Long value to a String in the backing bean everything works will (put the converted Long in <af:selectOneChoice> value ).

Note : the selectItem Vector, the values in it are String . ----> the selectItem Vector is assigned to the <f:selectItems> value inside the <af:selectOneChoice>.
So I think this is the reason .

after all I don't have a solution but to do the conversion .

Best Regards.
And thanks all.

By the way Tim I like you truly signature .
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15959

It doesn't matter. ADF is translated to HTML before it's sent to your browser. Browsers don't understand JSF, only HTML.

Are you setting up the selectitems like this:

Or, if you're not using autoboxing: "new SelectItem(new Long(1), "One");"

That should work if your target value is like this:

Hatem Alimam
Ranch Hand

Joined: Dec 14, 2008
Posts: 30
The thing is I can't change my selectItem Array or Vector for some reasons (the values in the selectItem Array in my case are Strings) ....
But I found a convincing solution ...
I have defined in the backing bean an emptyString = ""

and in the value of the selectOneChoice I did this

the values are displayed but unfortunately the selectOneChoice is changed to outText

anyway i'll keep in touch for this issue for the benefit of the others that they would have this issue .

Thanks Tim
I agree. Here's the link: http://aspose.com/file-tools
subject: Convert Long to String inside JSF el expression
Similar Threads
JSF binding conversion
Violation of Separation of Concerns
Drag n' drop RichFaces panel
JSF Portlet and instance variables
How to pass parameter to JSF backing Bean