Announcement Announcement Module
No announcement yet.
prepopulating form? Page Title Module
Move Remove Collapse
Conversation Detail Module
  • 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.


    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.


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

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

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


    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.


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


      • #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:



        • #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!