2 Replies Latest reply on May 2, 2009 2:03 PM by aditya.bora

    substring in El Expression

      Hi to all

      I am creating a datatable whigh shows data related to a department table. In database, department table has got a field called depdesc(size:256) which stores description of the department. Now, when I enter department description of size 256 without any space, data column is not able to maintain the defined width of the column. It gets expand.
      To overcome this issue what I am thinking of displaying only the first 40 characters form the description field. Now my question is, Is this possible to use substring in El Expression for data column?
      Something like:

      <rich:column id="dep3" sortable="true" label="Description" filterEvent="onkeyup" style="width:150px;" sortBy="#{ecEntity.depDesc}" filterBy="#{ecEntity.depDesc}">

        <f:facet id="dep3_ecfchdr" name="header">
         <span id="dep3_echdrtxt" style="width:150px;">Description</span>
        </f:facet>

        <h:outputText value="#{substring(ecEntity.depDesc,0,40)}"/>
      </rich:column>


      Thankx in advance
      Aditya(Eddie)
        • 1. Re: substring in El Expression
          niox.nikospara.yahoo.com

          Hi,


          You need to define a new EL function, easy with facelets.


          1) Create a file eg my.taglib.xml (replace my, but keep the .taglib.xml suffix) in WEB-INF with contents:


          <?xml version="1.0"?>
          <!DOCTYPE facelet-taglib PUBLIC
               "-//Sun Microsystems, Inc.//DTD Facelet Taglib 1.0//EN"
               "https://facelets.dev.java.net/source/browse/*checkout*/facelets/src/etc/facelet-taglib_1_0.dtd"
          >
          <facelet-taglib>
               <namespace>http://www.my.net/some/arbitrary/namespace</namespace>
               
               <function>
                    <function-name>substring</function-name>
                    <function-class>my.util.Util</function-class>
                    <function-signature>java.lang.String substring(java.lang.String,int,int)</function-signature>
               </function>
          </facelet-taglib>
          



          2) Create the class as follows:


          package my.util;
          
          public class Util {
            public static String substring(String orig, int start, int length) {
              return orig.substring(start,length);
            }
          }
          



          3) Finally make Facelets aware of your library; in web.xml:


               <context-param>
                    <param-name>facelets.LIBRARIES</param-name>
                    <param-value>/WEB-INF/my.taglib.xml</param-value>
               </context-param>
          



          4) Use it in your pages:


          <ui:composition ... xmlns:my="http://www.my.net/some/arbitrary/namespace">
            ...
            <h:outputText value="#{my:substring(ecEntity.depDesc,0,40)}"/>
            ...
          </ui:composition>
          

          • 2. Re: substring in El Expression

            Thankx Nikos. It really works.


            Regards
            Aditya(Eddie)