-
1. Re: Removing the last char from a string in EL
matt.drees Mar 4, 2008 4:58 AM (in response to pete007)You might want to create & use an EL function.
If you're using facelets, you can look at Rick Hightower's article for an example of EL functions. -
2. Re: Removing the last char from a string in EL
pete007 Mar 4, 2008 7:32 AM (in response to pete007)So, it cant be done by simply changing some chars?
After I read the article, I came to the conclusion that someone must have done this already and then I found this site:
Seems pretty nice and they already have a chop tag, that removes the last char, just like I need it.
I will try to use it in the evening, so please warn me, if this is no good idea. (compatibility or sth)
Thanks for the infos, Peter
-
3. Re: Removing the last char from a string in EL
kariem Mar 4, 2008 10:43 AM (in response to pete007)I think this should be done in a Converter in JSF. With seam this should not be a problem with the great support annotations.
The first implementation I would think of:
@Name("your.package.name.StringAddAndRemoveCharacter") @org.jboss.seam.annotations.faces.Converter(id = "characterSuffix") public class StringAddAndRemoveCharacter implements Converter { /** @see Converter#getAsObject(FacesContext, UIComponent, String) */ public Object getAsObject(FacesContext ctx, UIComponent comp, String s) { return s == null ? s : s + 'A'; } /** @see Converter#getAsString(FacesContext, UIComponent, Object) */ public String getAsString(FacesContext ctx, UIComponent comp, Object o) { if (o == null) { return null; } String s = o.toString(); return s.substring(0, s.length() - 1); } }
Of course this only works, if you don't have too many different characters to add.
-
4. Re: Removing the last char from a string in EL
pete007 Mar 4, 2008 2:36 PM (in response to pete007)What do you mean by too many character?
It's a full uppercase keyboard with [A-Z0-9.-ÄÖÜß] for text input including space, backspace and a joker sign. :DInstead of the character like above I am using a linked image showing the char.
I already defined everything and it is working, only the backspace functionality is making trouble.
You mean, if I put this in a File StringChopper.java, I can use it in the form?
@Name("de.me.gdc.StringChopper") @org.jboss.seam.annotations.faces.Converter(id = "chopper") public class StringChopper implements Converter { /** @see Converter#getAsString(FacesContext, UIComponent, Object) */ public String getAsString(FacesContext ctx, UIComponent comp, Object o) { if (o == null) { return null; } String s = o.toString(); return s.substring(0, s.length() - 1); } }
How would I call it exactly?
value="#{empty keyword ? '' : chopper(keyword)}"
Sorry, Ive got no java,seam,jboss by the hand to test it.
-
5. Re: Removing the last char from a string in EL
kariem Mar 4, 2008 3:26 PM (in response to pete007)Sorry, I think I have misinterpreted your use case. A converter is usually used to do custom data conversion. I just searched for a tutorial on converters and found something here. In your case, you actually need to add or remove characters from a single field.
Please forget my previous advice.
Could you perhaps elaborate on your current design? This would make it easier for others to answer.
Is your <f:param … enclosed in a h:commandButton or s:link? I'd say you could just call an action that removes the last character from an injected context variable, but it really depends on how you add characters.
-
6. Re: Removing the last char from a string in EL
keithnaas Mar 4, 2008 4:25 PM (in response to pete007)Peter, I'm not entirely sure of the use case, but would it make sense to create an Enum that maps the possible keynames and keywords? You can then use an EnumConverter to convert stuff.
-
7. Re: Removing the last char from a string in EL
jimk1723 Mar 4, 2008 6:05 PM (in response to pete007)You want chop the last character from a string in EL? Jakarta Commons Lang's StringUtils has a chop() method.
You can turn this - and any static method really - into a JSF function with a custom tag lib, e.g. META-INF/custom.taglib.xml:
<?xml version="1.0"?> <!DOCTYPE facelet-taglib PUBLIC "-//Sun Microsystems, Inc.//DTD Facelet Taglib 1.0//EN" "facelet-taglib_1_0.dtd"> <facelet-taglib> <namespace>http://org.apache.commons.lang/StringUtils</namespace> <function> <function-name>chop</function-name> <function-class>org.apache.commons.lang.StringUtils</function-class> <function-signature>java.lang.String chop(java.lang.String)</function-signature> </function> </facelet-taglib>
You may need to alter your build.xml to ensure the custom.taglib.xml finds its way to your WAR's META-INF folder.
And use it as such:
value="#{stringutils:chop(keyword)}"
Be sure to bind the stringutils prefix to http://org.apache.commons.lang/StringUtils - add this xmlns attribute to the root element of the page:
xmlns:stringutils="http://org.apache.commons.lang/StringUtils"
-
8. Re: Removing the last char from a string in EL
pete007 Mar 4, 2008 7:58 PM (in response to pete007)Hi all,
I am using the paramters keyname and keyword depending on the form where I want to include the keyboard.
It will be used on a touch screen terminal without a hardware keyboard. I use it to filter the elements shown in a list above the keyboard.
On the persons form keyname is lastname, on the rooms form it is roomnumber, etc.
Yes, it is used in a s:link + f:param + h:graphicsImage ...
the context variable is always another: person.lastname, room.roomnumber, ...Using an Enum is not that easy, I even got placeHolders to indent the keys half width in each row, might be difficult to map?
I will try the custom-taglib when I am at home.
Thanks so far, Pete
-
9. Re: Removing the last char from a string in EL
pete007 Mar 6, 2008 12:26 PM (in response to pete007)Just for the indexer, here is the error message you get, when you forget to copy commons-util.jar from the project/lib to the server/default/deploy/lib:
/layout/keyboard.xhtml @21,76 value="#{stringutils:chop(keyword)}" Function 'stringutils:chop' not found
Despite this the installation was very simple:
- Copy-pasted the above xml definition in a new file in project/lib/META-INF.
- Added the file to project/build.xml, target jar, add an include
- Bind Namespace in the form
- Use function
Works excellent, cropping one char after the other, thank you very much!
-
10. Re: Removing the last char from a string in EL
gavin.king Mar 7, 2008 3:15 AM (in response to pete007)Are you sure that:
#{empty keyword ? '' : keyword.substring(0,keyword.length()-1)}
Doesn't work? I think you just got the method name wrong.
-
11. Re: Removing the last char from a string in EL
gavin.king Mar 7, 2008 3:18 AM (in response to pete007)Note that there is also a built-in fn:substringAfter() function (at least in JSP, not 100% sure if it is there in Facelets).
You can see a list of all built-in EL functions here.
-
12. Re: Removing the last char from a string in EL
damianharvey.damianharvey.gmail.com Mar 7, 2008 10:50 AM (in response to pete007)Just out of interest, is there anything particularly wrong with writing your own Bean that does this?
eg:
@Name("stringUtility") @BypassInterceptors public class StringUtility { public String backspace(String in) { return in.substring(0, in.length() -1); } }
<a:commandButton value="Backspace" action="#{stringUtility.backspace(keyword)}" ajaxSingle="true" reRender="keyword"/>
I use this StringUtility to do things like padding, converting line breaks to <br/> etc. What advantage does an EL function give me?
Cheers,
Damian.
-
13. Re: Removing the last char from a string in EL
keithnaas Mar 7, 2008 4:10 PM (in response to pete007)Having done any testing but there is likely a performance difference between using the JavaBean approach and JSF function.
Also, if you're concerned about standards, the JavaBean method is a Seam extension to EL.
And finally, it all depends on what kind of link it is. See Christian's comments on GET/POST/DELETE
-
14. Re: Removing the last char from a string in EL
pete007 Mar 12, 2008 10:18 PM (in response to pete007)And I think, you are right! :) .. after removing the camel case it worked as expected, thank you!