Announcement Announcement Module
Collapse
No announcement yet.
prepopulating form? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • prepopulating form?

    How do you prepopulate a form so that if the user has an error on the page when it redisplays the form for them the stuff they input is there as well as the stuff my code put there (e.g., the items in a SELECT list).

    I've got the basic form working where it's remembering what the user input or selected but I can't figure out how to generate the SELECT list.

    Thanks for the newbie help.

  • #2
    Am guessing here, but typically, a SELECT list would get it's items from data you publish in the referenceData method.

    e.g.

    refData.put("listItems", myListItems);

    where myListItems might be a List<LabelValueBean> or something.

    Your command object would contain a property that stored which item in the SELECT was selected.

    You'd then need to use JSTL or something to loop through your "listItems" request attribute, drawing the select, and checking against each option whether you needed to add the HTML "selected" attribute.

    e.g.

    <spring:bind path="command.mySelectedId">
    <c:forEach items="listItems" var="item">
    // draw each <option> here, checking if
    // command.mySelectedId matches item.value
    </c:forEach>

    <%-- show possible field errors --%>

    <c:forEach var="error" items="${status.errorMessages}">
    <div class="fieldError">
    <spring:message text="${error}"/>
    </div>
    </c:forEach>

    </spring:bind>

    We created various JSP pages like Select.jsp, Checkbox.jsp, etc, which makes it about a million times faster to put a JSP page together.

    Comment


    • #3
      Ok, great. Thanks for getting me pointed in the right direction.

      Comment


      • #4
        And use a tag library. If you don't you'll wish you did after you do it about a million times by hand. The Jakarta one is okay, but a bit dated. It's pretty easy to write your own. I just wrote one that takes a list of object and uses OGNL to find the option values and labels:

        Code:
        <eha&#58;select
                name="$&#123;status.expression&#125;"
                value="$&#123;status.value&#125;"
                items="$&#123;people&#125;"
                valuePath="id"
                titlePath="address.street"/>

        Comment


        • #5
          Here I am struggling to learn Spring and now you want me to learn how to write custom tags? :wink:

          But that does look slick. For my tables I will be using the displaytag library.

          I got it to work in the most minimal way as a test; now I have to do it "for real" and get the SELECT items from a database.

          What was confusing me is that in the .jsp this stuff is wrapped in a spring:bind and I was thinking that it was tied to the command object. Now that I see that it's not it seems almost too easy. Woo hoo!

          Comment

          Working...
          X