Results 1 to 5 of 5

Thread: prepopulating form?

  1. #1
    Join Date
    May 2005
    Location
    California, US
    Posts
    735

    Default 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. #2
    Join Date
    Aug 2004
    Location
    Sydney
    Posts
    503

    Default

    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.

  3. #3
    Join Date
    May 2005
    Location
    California, US
    Posts
    735

    Default

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

  4. #4

    Default

    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"/>

  5. #5
    Join Date
    May 2005
    Location
    California, US
    Posts
    735

    Default

    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!

Similar Threads

  1. Replies: 3
    Last Post: Jun 8th, 2010, 03:27 AM
  2. Replies: 6
    Last Post: Sep 22nd, 2006, 09:08 AM
  3. Replies: 9
    Last Post: May 4th, 2006, 09:53 AM
  4. Replies: 1
    Last Post: Aug 30th, 2005, 10:51 PM
  5. Replies: 0
    Last Post: Jun 10th, 2005, 08:22 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •