6 Replies Latest reply on Jan 17, 2007 6:06 AM by Marco Huber

    Seam selectItems ignores label tag

    Marco Huber Newbie

      Hello,

      I tried to figure out how the selectItems component from seam works, but I wasn't very successful yet.

      I tried out the examples but I can't figure out how I can transfer this to my application.

      Here my scenario:

      I have a List of Languages, that I want to display my web gui in a h:selectOneMenu.
      I have extend my jsf, wrote a Converter, make a backing bean. The value of the option is set correct through the Converter, but for the description I get always the result of the toString() method. It seems that the label tag is ignored. I can write what ever I want and get always the toString()-result.
      Maybe I have configured wrong.

      Here some code fragements:

      The jsp:

      
      <h:selectOneMenu value="#{user.language}" converter="myLanguageConverter">
       <si:selectItems
       value="#{languages}" var="language" label="#{language.isocode}"/>
       </h:selectOneMenu>
      


      The backing bean:
      public class EditUserInformationActionBean implements EditUserInformationAction, Serializable {
      
       @SelectItems
       private List<Language> languages;
      
      
       @Factory("languages")
       public void initLanguages() {
       languages = ItemHelper.initLanguage();
       }
      
       ...
      
      }
      


      The Converter:
      public class LanguageConverter extends SelectItemsConverter {
      
       public LanguageConverter() {
       }
      
       public Object getAsObject(FacesContext context, UIComponent component, String value) throws ConverterException {
       throw new ConverterException("Method not supported");
       }
      
       public String getAsString(FacesContext context, UIComponent component, Object value) throws ConverterException {
       if (null == value) {
       return "";
       }
       if (value instanceof Language) {
       Language l = (Language) value;
       return formatLanguageString(l);
       }
       return "";
       }
      
      


      The language class:
      public class Language implements Serializable {
      
       @Id
       @Column(name = "isocode", nullable = false)
       private String isocode;
      
       /** Creates a new instance of Language */
       protected Language() {
       }
      
       // getter/setter, toString, equals etc.
      




      The examples works fine. But I think there is an litte error, too. The discount drop-down-field has the descriptions "tenPercent", ""twentyPercent" and "twentyFivePercent", but I would expected the values from the message.properties (10%, 20% and 25%)

      I hope someone could give me a hint what's wrong.

      My System:
      - WinXP Pro
      - JBoss 4.0.5.GA
      - Seam 1.1.0.GA

      Tried:
      - selectitems-1.1.1beta4
      - selectitems-1.1.0rc2


      Regards
      Marco

        • 1. Re: Seam selectItems ignores label tag
          Pete Muir Master

          It seems to me like there is some confusion going on.

          1) use the latest build from the wiki page (1.1.1beta4)
          2) Don't use @SelectItems (it shouldn't even be in the latest jar)
          3) If language is an EJB3 entity (it looks to me like it is) then you don't need to write your own converter, the default one should be fine.

          Thanks for the bug report on the example, I'll fix that in the next release (a typo on my part).

          • 2. Re: Seam selectItems ignores label tag
            Marco Huber Newbie

            Thank you for your answer petemuir.

            I have removed the selectitems-1.1.0rc2.jar and the @SelectItems annotation from my ActionBean and now it works. Also works the default converter, so I can remove my class, too.

            I have a question to the labeling of the entry:
            As I see, I can use every attribute of the java bean defined in the var tag, but is it possible to access a localized message bundle, depending on a value from the corresponding entity.

            What I have in mind is something like this:
            I have this list of language objects, which I want to display in english or german or french etc. depending on the Locale.
            So I like to configure a set of keys in my message properties like languagename + isocode from the language entity.

            Does the Seam selectItem support multi language labelling?

            Marco

            • 3. Re: Seam selectItems ignores label tag
              Pete Muir Master

              Well, you can easily look up in the message bundle a message using an EL value binding as the key:

              <si:selectItems value="#{foo}" var="bar" label="#{messages[bar.label]}" />


              Is that what you are after? Otherwise you could always process the List before it reaches the page in the @Factory method.

              • 4. Re: Seam selectItems ignores label tag
                Marco Huber Newbie

                Ok, I tried it and it works. But it wasn't exact that what I really had in mind.

                I try to explain it a little more:

                With your example I can only lookup for message keys that has the same name as the value of the attribute.

                If I have languages with the iso codes 'EN', 'DE' or 'FR' the EL expression will look for messages keys with the names of the codes. So the keys have to be the value of the attribute:

                DE = german
                EN = english
                FR = french


                But if I have also countries like 'DE' or 'FR', the message key referres for this both also to the language name.

                So I would put a prefix to the message keys and access them from the UI component. Here an snip of a resource bundle:
                languagename.DE = german
                languagename.EN = english
                languagename.FR = french
                country.DE = Germany
                country.FR = France
                country.UK = United Kingdom
                


                So I would write something like 'languagename.' + language.isocode or 'country.' + country.isocode in a tag.

                Is this possible?

                Regards
                Marco


                • 5. Re: Seam selectItems ignores label tag
                  Pete Muir Master

                  As long as you provide a valid expression to the label attribute it will work. I haven't tried your exact situation but I think #{messages['languagename.'language.isocode]} probably would work (n.b. strings are automatically concatenated with no operator in EL)

                  • 6. Re: Seam selectItems ignores label tag
                    Marco Huber Newbie

                    I tried your suggestion, but it seems that #{messages['languagename.'language.isocode]} not works. I always get an exception:

                    Caused by: javax.el.ELException: Error Parsing: #{messages['languagename.'language.isocode]}
                     at com.sun.el.lang.ExpressionBuilder.createNodeInternal(ExpressionBuilder.java:140)
                     at com.sun.el.lang.ExpressionBuilder.build(ExpressionBuilder.java:157)
                     at com.sun.el.lang.ExpressionBuilder.createValueExpression(ExpressionBuilder.java:201)
                     at com.sun.el.ExpressionFactoryImpl.createValueExpression(ExpressionFactoryImpl.java:74)
                     at com.sun.facelets.tag.TagAttribute.getValueExpression(TagAttribute.java:256)
                     ... 84 more
                    Caused by: com.sun.el.parser.Pars
                    11:30:11,219 ERROR [STDERR] eException: Encountered "language" at line 1, column 27.
                    Was expecting one of:
                     "." ...
                     "[" ...
                     "]" ...
                     ">" ...
                     "gt" ...
                     "<" ...
                     "lt" ...
                     ">=" ...
                     "ge" ...
                     "<=" ...
                     "le" ...
                     "==" ...
                     "eq" ...
                     "!=" ...
                     "ne" ...
                     "&&" ...
                     "and" ...
                     "||" ...
                     "or" ...
                     "*" ...
                     "+" ...
                     "-" ...
                     "?" ...
                     "/" ...
                     "div" ...
                     "%" ...
                     "mod" ...
                    
                     at com.sun.el.parser.ELParser.generateParseException(ELParser.java:1651)


                    So I seached the internet an found out that EL cannot concat strings this way.

                    I found a solution, but I find is not very beautiful, but it works.
                    I defined the jstl functions in my header

                    xmlns:fn="http://java.sun.com/jsp/jstl/functions"


                    and could now use the replace function (a concat is not available) like followed:

                    <h:selectOneMenu value="#{user.language}">
                     <si:selectItems
                     value="#{languages}" var="language" label="#{messages[fn:replace('languagename.SUFFIX', 'SUFFIX', language.isocode)]}"/>
                     </h:selectOneMenu>


                    Maybe I'll try to write my own concat-functions.