14 Replies Latest reply on Mar 12, 2008 10:18 PM by pete007

    Removing the last char from a string in EL

      value="#{empty keyword ? '' : keyword.subString(0,keyword.length()-1)}"



      doesnt work.


      Im working on a keyboard for some pages, adding characters is working fine with:


       .. <f:param name="#{keyname}" value="#{keyword}A"/>A .. 



      But now I have to do the reverse thing for the back-button.


      Can this be done in expression language?
      If not, what else could I do?

        • 1. Re: Removing the last char from a string in EL
          matt.drees

          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

            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:


            Jakarta String Taglib


            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

              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

                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. :D


                Instead 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

                  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

                    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

                      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

                        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

                          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

                            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

                              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

                                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

                                  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

                                    And I think, you are right! :) .. after removing the camel case it worked as expected, thank you!