aspose file tools*
The moose likes JSF and the fly likes How to insert a char in a String in a JSF/Trinidad tag ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » JSF
Bookmark "How to insert a char in a String in a JSF/Trinidad tag ?" Watch "How to insert a char in a String in a JSF/Trinidad tag ?" New topic
Author

How to insert a char in a String in a JSF/Trinidad tag ?

Gabriele Giambartolomei
Greenhorn

Joined: Nov 17, 2011
Posts: 8
Hello guys,
I have this problem. I need to insert a char into a string (a space char to be precise). The string may start with a capitol letter and all other chars can be a letter or digits like "J16334067" or "M34782367 or "BA63879G4.
What I need to do is: if the first char is either an 'M' or 'J' or 'T' char and the second char is a digit (0 to 9) then I have to insert a space between the first char and the rest of the string. For example if the string is J16334067 I need to produce "J 16334067" (first char is a 'J', second char a digit).

But I need to do this within a tag in JSF/Trinidad like this:
<tr:commandLink id="treeNodeLink" text="#{node.nodeID == ((node.nodeID.startsWith('J') || node.nodeID.startsWith('M') || node.nodeID.startsWith('T')) && ((node.nodeID.charAt(1) >= 0x030) && (node.nodeID.charAt(1) <= 0x39))) ? 'node.nodeID.insert('1, ' ').toString()' : 'node.nodeID'}"

Here the node.nodeID is the string I need to elaborate, so what I am doing here is checking if the first character of node.nodeID is either an 'M' or 'T' or 'J' char and then check if the second char is a digit. If so, I need to insert a space after the first character otherwise just return the string as it is. I did it this way but it doesn't work.
Anyone has any ideas on how to di this ?

Many thanks in advance.
Gabriele



Rok Koncina
Greenhorn

Joined: Nov 30, 2011
Posts: 5
Hello,

I never worked with char in expression language, but I think you have some other mistakes that should give wrong result. Try with the following code:

My application also doesn't work with && and ||. I have to use and and or instead.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16303
    
  21

It is not good practice to code complex expressions in EL. The Model/View/Controller paradigm upon which JSF is based is predicated on the idea that the View won't contain logic. Otherwise, you get a maintenance headache because you have to constantly "treasure hunt" between model and view files to find where things are getting done - and after a while they may actually end up in conflict.

Plus, EL is a to code and debug when you attempt things like that.

It's better/cleaner to do the string manipulation in the backing bean and simply expose the result as a property.


Customer surveys are for companies who didn't pay proper attention to begin with.
Gabriele Giambartolomei
Greenhorn

Joined: Nov 17, 2011
Posts: 8
Hello,

Rok, many thanks for your reply, I will try this way and let you know if it works for me.

Tim, you are right, I totally agree with you: this should be done in the backend code and not within the JSF page, but unfortunately this is the way I have to do in this case, I can't change it :-/

Thanks.
Gabriele
Gabriele Giambartolomei
Greenhorn

Joined: Nov 17, 2011
Posts: 8
Hello Rok,

unfortunately if I do it the way you suggested I get the following error message:
<05-Dec-2011 14:21:50 o'clock GMT> <Error> <HTTP> <BEA-101017> <[weblogic.servlet.internal.WebAppServletContext@6bdde4 - appName: '_auto_generated_ear_', name: 'RA', context-path: '/RA'] Root cause of ServletException.
com.sun.facelets.FaceletException: Error Parsing //C:/data/workspace/RA/web/jsf-custom/components/interactionTree.jspx: Error Traced[line: 31] The value of attribute "text" associated with an element type "tr:commandLink" must not contain the '<' character.

I tried to replace the logic operators || and && with OR and AND but it doesn't make any difference.

Gabriele
Pete Nelson
Ranch Hand

Joined: Aug 30, 2010
Posts: 147

The error message is complaining about the ">" character. Try using "gt" (or "lt" for "<") instead, and see if you have any better luck.

The other suggestions are correct, though - this would likely be easier in a backing bean than to do it in EL (more reusable that way, too).


OCPJP
In preparing for battle I have always found that plans are useless, but planning is indispensable. -- Dwight D. Eisenhower
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16303
    
  21

If the objection is that you are forbidden to alter the original backing bean, you can construct an additional façade backing bean to process that expression and inject whatever you need into it from the primary backing bean. There's no limit on the type or number of beans that can back a JSF view (other than sanity, anyway!)

If you are simply forbidden from creating your own backing beans or otherwise following recommended best practices, update your résumé. Shops that are over-controlled and especially ones rigidly controlled by "experts" who are only experts in their own heads (and management's) are not healthy places to work.
Gabriele Giambartolomei
Greenhorn

Joined: Nov 17, 2011
Posts: 8
Tim, the second part of your comment is the one that applies to me at the moment...unfortunately ;-)

Thanks anyway.
Gabriele
Rok Koncina
Greenhorn

Joined: Nov 30, 2011
Posts: 5
Ok, I just tested it and the code works for me (except , because I don't have the same class ;) ).
Gabriele Giambartolomei
Greenhorn

Joined: Nov 17, 2011
Posts: 8
Hello Rok,

unfortunately the same code that works for you, does not work for me :-( When I run it I get an error parsing the expression: it complains about the round brackets in node.nodeID.startsWith('J') ! Weird...

To reduce complexity now I am just running this piece of code:

text="#{node.nodeID == node.nodeID.startsWith['J'] ? node.nodeID.insert[1,' '].toString() : node.nodeID}"

and when I run it I get en error parsing the expression with the comma in node.nodeID.insert[1,' '] but I don't know how I can replace that ?

Gabriele
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to insert a char in a String in a JSF/Trinidad tag ?