Announcement Announcement Module
Collapse
No announcement yet.
Spring Select List Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring Select List

    Can someone tell if the below select is configured correctly.Its loads the multiselect list but when i try to submit the form it complains about type mismatch.

    <form:form modelAttribute="user" method="post" action="whatever">
    <form:select path="CustomObjList">
    <c:forEach items="${numList}" var="temp">
    <option value="${temp.number}">${temp.number}</option>
    </c:forEach>
    </form:select>
    </form:form>

    Class User
    {
    List<CustomObject> CustomObjList;
    }

    Class CustomObject
    {
    Object A;

    Object B ;
    }

    Class B
    {
    String number;
    }

    I want to loop thru the select to be able to display attributes in class B (number)
    In my spring webflow i have the following:

    //within view state//
    <on-render>
    <evaluate expression="service.generateNumbers(user)" result="viewScope.numList"></evaluate>
    </on-render>

  • #2
    Hello

    Some observations

    1) use code tags, is more readable for us
    2) when you declare a variable in Java in general, is very recommendable that the first letter must be lower case

    Code:
    <form:form modelAttribute="user" method="post" action="whatever">
    <form:select path="customObjList">
    <c:forEach items="${numList}" var="temp">
    <option value="${temp.number}">${temp.number}</option>
    </c:forEach>
    </form:select>
    </form:form>
    
    Class User
    {
    List<CustomObject> customObjList;
    }
    Can someone tell if the below select is configured correctly.Its loads the multiselect list but when i try to submit the form it complains about type mismatch.
    Post the complete error stack trace

    Now this is a classic problem about HTML Select + Form when other custom class type is involved here

    To gain time read about Converters and Formatters, it is mentioned on the Spring Core Documentation

    //within view state//
    <on-render>
    <evaluate expression="service.generateNumbers(user)" result="viewScope.numList"></evaluate>
    </on-render>
    Seems well, since it is used before to show the view, furthermore your problem is you try to post the form, therefore it means the collection was correctly created

    HTH

    Comment


    • #3
      Hello,

      There is something that confuses me regarding storing a multiple select input to a comma separated string. This conversion is done by default in an ordinary MVC spring app (I guess due to CollectionToStringConverter or something similar), but returns a typeMismatch error in a web flow. I've figured that a multiple select returns a list and because of my path field being declared as a String I get a typeMismatch. This still doesn't explain why I sometimes get an error and sometimes not. Setting CollectionToStringConverter as a converter for my field in the web flow file didn't work neither.

      I'm sure I'm missing something, but I can't figure out what. Could you please give me a hint?

      Comment


      • #4
        Andreea

        Read documentation about Converters and Formatters, it is mentioned on the Spring Core Documentation

        you need a little extra configuration if you want it use for SWF, on SWF documentations are some indications

        Comment


        • #5
          Thanks for your reply. I've read SWF Reference Guide and from there I found out that I could install my own converter or override a default one. It's not big deal to write my own CollectionToString converter by following the indications, but I was wondering if there is a way to use "org.springframework.core.convert.support.Collecti onToStringConverter". I can't extend DefaultConversionService and use addConverter(new org.springframework.core.convert.support.Collectio nToStringConverter) because this class is not public.

          Comment


          • #6
            Hello andreea

            Thanks for your reply.
            You're welcome

            I've read SWF Reference Guide and from there I found out that I could install my own converter or override a default one.
            Since I work and do my own experiments with Spring MVC and Spring Web Flow, I only wrote one Formatter and work both projects, on my configuration I only do reference to the *converter/formatter* register for Spring MVC before

            I should had wrote a tutorial about this, after to spend 2/3 days making this integration

            It's not big deal to write my own CollectionToString converter by following the indications
            I am not using the CollectionToString

            Recall, I am using Formatters and you COnverter, when I tried use converters for Spring MVC and SWF I couldnt, but with Formatter all goes well, and I have practically the same behavior, for example has a select list of objects (City class) for the Country Class

            Code:
            public class Country{
            
                 private City city
            where from the all the select list I only need a city

            If you could post some code, would be nice, but I going to see your code in the night, I am busy right now with other things

            Comment


            • #7
              I really appreciate your goodwill and I also consider time valuable, so I will first try using Formatter and if still have problems ask again.

              To gain some time, I'll describe my need. When I first read the type Conversion and Formatter documentation I decided the best choice for my case was Formatter. But after more research, I found this phrase " In fact in the end Formatters are reigstered as GenericConverter types with Spring's GenericConversionService making them equal to any other converter." on http://static.springsource.org/sprin...l/ch05s07.html. I decided to use Converter after all, discovered org.springframework.core.convert.support package and tried to make use of it in the following context (by adding converter="org.springframework...CollectionToStrin gConverter" in binding tag):

              Code:
              public class Computer {
              ...
              private String type;
              ...} web-flow file: <view-state id="..." model="computer">
              <binder>
              <binding property="type"/>
              </binder>
              <transition on="..." to="selectType" bind="true"/>
              </view-state> selectType.jsp <form:form modelAttribute="computer">
              <form:select path="type" multiple="true" items="...list from model..."/>
              ... </form:form>
              This gives me a typeMismatch error when I try submitting the form in selectType.jsp (after selecting more than one item from items list, but works fine when selecting only one item). After adding converter in binding tag and define a conversionService bean with class GenericConversionService, I get a strange Exception.

              Thanks,
              Andreea

              Comment


              • #8
                Hello andreea

                I really appreciate your goodwill and I also consider time valuable
                You're welcome, I recall when I started in this forum and valuable help I have received, and I have met important friends like Karl and Marten, now I am returning the favor

                so I will first try using Formatter and if still have problems ask again.
                Let me know your advance

                To gain some time, I'll describe my need. When I first read the type Conversion and Formatter documentation I decided the best choice for my case was Formatter.
                Good

                " In fact in the end Formatters are reigstered as GenericConverter types with Spring's GenericConversionService making them equal to any other converter." on http://static.springsource.org/sprin...l/ch05s07.html.
                I think this mean Spring Core underhood does such job internally

                I decided to use Converter after all, discovered org.springframework.core.convert.support package and tried to make use of it in the following context (by adding converter="org.springframework...CollectionToStrin gConverter" in binding tag):
                I couldnt integrate Converter + Spring MVC + SWF, therefore I doubt be useful for you later

                This gives me a typeMismatch error when I try submitting the form in selectType.jsp (after selecting more than one item from items list, but works fine when selecting only one item). After adding converter in binding tag and define a conversionService bean with class GenericConversionService, I get a strange Exception.
                You must consider that private String type; is not the same than private String[] type;, I would consider List<String> instead

                Best Regards

                Comment


                • #9
                  Hello, sorry for my late reply, but yesterday I was not able to login.

                  The problem with List<String> is that I needed "type" field to be a string. As converters didn't work and formatters didn't match (I needed parser method to receive a List<String> and return a comma separated String), I made a trick (unfortunately not as efficient as I wanted) and added another field, List<String> types. This way the end of the flow if followed by a method call which converts the list to a string. If any more professional (maybe with default conversion of list to string or anything else) idea cross your mind, please let me know.

                  Thanks for your help,
                  Andreea

                  Comment

                  Working...
                  X