2 Replies Latest reply on Mar 18, 2009 10:55 AM by vdweij

    Access Maps with Enum keys in facelet

    vdweij

      I was facing a problem where I had to display values of a Map with Enum keys. The Map is unordered so I could not simply iterate over it. For the sake of clarity I minimized the classed in order to demonstrate the case.


      Here is the enum class:


      public enum MyType{
           TYPE1 ,
           TYPE2
      }




      Here is the bean:


      @name('myBean')
      public class MyBean {
           public Map<MyType,Date> importDates;
      
      
           /* the class also contains the getter 'n setter 
              and some initialisation code for the
              importDates property */
      }



      My first attempt was unsuccessfully and yielded no output:


      <h:outputText value="Date of type 1: #myBean.importDates['TYPE1']">
           <s:convertDateTime type="both" dateStyle="full"/>
      </h:outputText>
      <h:outputText value="Date of type 2: #myBean.importDates['TYPE2']">
           <s:convertDateTime type="both" dateStyle="full"/>
      </h:outputText>



      This is of course pretty logical since the key is an Enum an not a String. I worked around this by looping through the key set of the importDates Map and making them available as a parameter (note the absence of single quotes).


      <c:forEach var="type" items="#{myBean.importDates.keySet()}">
           <ui:param name="#{type}" value="#{type}"/>
      </c:forEach>
      
      <h:outputText value="Date of type 1: #myBean.importDates[TYPE1]">
           <s:convertDateTime type="both" dateStyle="full"/>
      </h:outputText>
      <h:outputText value="Date of type 2: #myBean.importDates[TYPE2]">
           <s:convertDateTime type="both" dateStyle="full"/>
      </h:outputText>



      This gives me what I want but appears somewhat clumsy. Does someone know of a better approach? I know of the convertEnum tag but I couldn't fit that in.

        • 1. Re: Access Maps with Enum keys in facelet
          vdweij

          In components.xml I need to access the same property


          <framework:entity-query name="myEQ" ejbql="SELECT MyEntity m WHERE m.type = 'TYPE1'">
               <framework:restrictions>
                    <value>m.lastImportDate = #{myBean.importDates['TYPE1]'}</value>      
               </framework:restrictions>
          </framework:entity-query>



          and that does work! Now a String is converted to a Enum key. This looks odd when the same syntax fails in facelets.

          • 2. Re: Access Maps with Enum keys in facelet
            vdweij

            Crap... I noticed I forgot the braces in my example (the do exist in the code). The need to be in the outputText tag.


            <c:forEach var="type" items="#{myBean.importDates.keySet()}">
                 <ui:param name="#{type}" value="#{type}"/>
            </c:forEach>
            
            <h:outputText value="Date of type 1: #{myBean.importDates[TYPE1]}">
                 <s:convertDateTime type="both" dateStyle="full"/>
            </h:outputText>
            <h:outputText value="Date of type 2: #{myBean.importDates[TYPE2]}">
                 <s:convertDateTime type="both" dateStyle="full"/>
            </h:outputText>