Announcement Announcement Module
Collapse
No announcement yet.
Binding Data Back into DrowDownList Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Binding Data Back into DrowDownList

    Hi, I want to do something like comboBox (dropdownlist) in .net for the JSP
    Is That Can work?

    What i mean is,

    Initially there is a combobox which contains data from db, the default select is " Please Select One"

    Let Say A User . edit his own profile, the db should return a value into the dropdownlist, so i want the value become the "selected" value but other data still can show when the user when activate the dropdownlist.

    whan i can do is only bind value for a list, but i can make the value i want become selected in the list

    Looking Forward to your helps.
    sorry for my bad english.

  • #2
    Binding Data Back into Dropdown list

    Hi,

    This is what you can do.

    Before diplaying the JSP I fetch the required list (ex.100,101,102)from the database into Set playListIdSet in the controller and put it into the request scope.

    Once the control comes to the JSP, I iterate through the Set with the help of "c" tag.If the "id" of that record is null i.e. if the form is "Add" mode,then display all the values in the dropdown.
    -----------------------------xxx-------------------------------
    <c:forEach var="playListId" items="${playListIdSet}">
    <c:if test="${playList.id == null}">
    <option value ="${playListId}">${playListId}</option>
    </c:if>
    (All the values i.e. 100,101,102 will be display in the dropdown)
    -----------------------------xxx-------------------------------
    Now if the "id" of the record is NOT null i.e."Edit" mode then check the dropdown value with it's value in the POJO (my value from the database is 101) or form bean & make it SELECTED.
    In my case the value to be selected is suppose 101
    -----------------------------xxx-------------------------------
    <c:if test="${playList.id != null}">
    <c:if test="${playList.playlistId == playListId}">
    <option value ="${playListId}" SELECTED>${playListId}</option>
    </c:if>
    (the dropdown value selected will be 101 here!)
    -----------------------------xxx-------------------------------
    Once we set the value in the drop-down (the value of the record i.e.101), we need to put the rest of the values too (to display 100,102)
    -----------------------------xxx-------------------------------
    <c:if test="${playList.playlistId != playListId}">
    <option value ="${playListId}">${playListId}</option>
    </c:if>

    </c:if>
    </c:forEach>
    -----------------------------xxx-------------------------------
    Hope this could be helpful to you.

    Thank You,

    ~Ritesh Biltheria

    Comment


    • #3
      Thank You, Biltheria!

      it help me a lot.
      btw, i found a example in the spring 2.06 example [springtag].
      Inside there have use PropertyEditor and some SpringTag. Is that similiar with ur code?
      What I read is so many class to build for complete 1 persistent class.
      Is that build the View using spring tag better or just normal jtsl better?

      Comment


      • #4
        Binding Data Back into DropwDown List

        Hi xLancer,

        I am glad that my solution was useful to you.

        I did not use any spring tag in the illustration. It was simple JSTL.

        There are two different things we are talking about here.

        JSTL is used to avoid JSP Scriplet in the source code and Spring tags are used

        to bind fields to the form POJO.Ex spring:bind

        A Spring App can be built without using Spring Tags however if you wish to

        use Spring Validation then you will have to use Spring Bind tag.

        JSTL core tags "c" can be used to for condition (<c:if), iteration (<c:forEach), setting a variable in request (<c:set) etc.

        Hope that clears the doubt.

        Thank You,

        ~Ritesh Biltheria

        Comment


        • #5
          Thanks you again,

          I got another question but not about the dropdownlist but still the value binding problem.

          I have a Collection of CheckBox which all this data should get from database.
          emmm, Like when register to some shoping website, at the end there will giv ppl choose which is interest (Edu, Tech, Fashion, etc) then the web will auto email the related issus.

          I can bind the checkboxes to web, my question is, how i insert a collection of select checkboxes value into mvc? because i only want those "selected" checkbox insert into the database.

          regards,
          x£@nc3r

          Comment


          • #6
            Originally posted by ritesh_bil View Post
            A Spring App can be built without using Spring Tags however if you wish to use Spring Validation then you will have to use Spring Bind tag.
            That's not true. Spring's bind tags have nothing to do with validation and actually don't have that much advantage over plain JSTL. The path handling is easier and you can use PropertyEditors with them. A real advantage you get by using Spring's form tags especially with dropdowns since you don't need to do any if (value==selectedValue) then selected="selected" stuff.

            Joerg

            Comment


            • #7
              Binding Data into dropdown list

              I am kinda surprise, can we really use spring validation without using spring:bind tag? If yes, how the data would be bound!
              Moreover, how would we use error class to generate error messages when the form is rejects???
              Did the spring people build spring tag only for drop-down purpose?
              ~Ritesh Biltheria

              Comment


              • #8
                Originally posted by ritesh_bil View Post
                I am kinda surprise, can we really use spring validation without using spring:bind tag?
                It's completely independent from each other. The tags are just for providing access to the actual objects when rendering the page. When doing this for non-String fields and String.valueOf(..) is not appropriate, PropertyEditors come into play. The request-to-object binding happens indepently from the tags, it's just a question of correct request parameter names. Only afterwards the validators come into play.

                Originally posted by ritesh_bil View Post
                Moreover, how would we use error class to generate error messages when the form is rejects???
                It's a question of naming conventions.

                Originally posted by ritesh_bil View Post
                Did the spring people build spring tag only for drop-down purpose?
                No, since you also need to convert objects to strings or markup. As written above Spring tags allow to use PropertyEditors for doing the conversion. Plain JSTL has no such functionality, only oneway-process with the tags in the fmt namespace. But imagine formatting a date in a particular way. You have to parse it with the same DateFormat. With CustomDateEditor this is handled automatically. With JSTL you have to do it by hand and duplicate the format.

                Joerg

                Comment

                Working...
                X