• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Convert Long to String inside JSF el expression

 
Hatem Alimam
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 }" />
 
Rohit Boorugu
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 17989
47
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Hatem Alimam
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 17989
47
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic