Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: bind a dynamic drop-down list

  1. #1
    Join Date
    Oct 2004
    Posts
    27

    Default bind a dynamic drop-down list

    I have an HTML form which has a drop-down list (indexed properties). The values of the list are retrieved from the database. How do I bind the list? Is there an example? Thanks.

  2. #2

    Default bind a list

    You should check the petclinic example that comes with the spring distribution.

    the form to add or edit a pet have a drop down list.
    that drop down list created with values from the TYPES table. the jsp is
    petForm.jsp

    is not complicated, to bind a select list is like to bind a input text.

    use the tag <spring:bind>

    examples :

    <spring:bind path="command.type">
    <FONT color="red">
    <B><c:out value="${status.errorMessage}"/></B>
    </FONT>
    <BR>
    <SELECT name="typeId">
    <c:forEach var="type" items="${types}">
    <c:if test="${command.type.id == type.id}">
    <OPTION selected="<c:out value="${command.type.id}"/>" value="<c:out value="${type.id}"/>"><c:out value="${type.name}"/></OPTION>
    </c:if>
    <c:if test="${command.type.id != type.id}">
    <OPTION value="<c:out value="${type.id}"/>"><c:out value="${type.name}"/></OPTION>
    </c:if>
    </c:forEach>
    </SELECT>
    </spring:bind>

  3. #3
    Join Date
    Oct 2004
    Posts
    27

    Default referenceData

    Thank you very much for pointing me to the example.

    In EditPetForm.jave, there is refereceData() method as
    protected Map referenceData(HttpServletRequest request) throws ServletException {
    Map refData = new HashMap();
    refData.put("types", getClinic().getPetTypes());
    return refData;
    }

    Then petForm.jsp uses the following code to get the list of types.
    <c:forEach var="type" items="${types}">

    I assume that this is how the "types" are set and retrieved. So I was trying the same way for a list of products (just product descriptions). I simply use
    refData.put("products", productManager.getProducts());
    in form controller and
    <c:forEach var="item" items="${products}">
    in JSP.

    However, I got the error message below:
    An error occurred while evaluating custom action attribute "items" with value "${order.products}": Unable to find a value for "products" in object of class "bus.Order" using operator "." (null)

    I ran debug and found the list of products were retrieved correctly in referenceData(). However, it was not bound to the JSP somehow.

    Thanks in advance.

  4. #4

    Default

    I don't understand where did the JVM find the order variable?

    you have to show me more codes.

  5. #5
    Join Date
    Oct 2005
    Location
    Boston, MA
    Posts
    2,844

    Default

    Is your controller a subclass of AbstractFormController (or one of its subclasses: SimpleFormController or AbstractWizardFormController)?

  6. #6
    Join Date
    Aug 2004
    Location
    The Netherlands
    Posts
    160

    Default

    You should have a look at your jsp page, what are the spring:bind tags around your <c:foreach ... /> tag?
    Jettro Coenradie
    http://www.gridshore.nl

  7. #7

    Default

    My form is a subclass of SimpleFormController

  8. #8
    Join Date
    Oct 2005
    Location
    Boston, MA
    Posts
    2,844

    Default

    zliu,

    Can you post your referenceData method and the handling method (or at least an excerpt of code from the controller where you return the ModelAndView object)?

  9. #9
    Join Date
    Oct 2004
    Posts
    27

    Default here is the code

    Thank you for your help. Here is the code. By the way, I wonder if I missed something special for AbstractWizardFormController. Similar code works fine for SimpleFormController.

    public class OrderFormController extends AbstractWizardFormController {
    ...

    public OrderFormController() {
    setPages(new String[] { "order", "order-confirm" });
    setCommandName("order");
    }

    protected Map referenceData(HttpServletRequest request, int page) {
    Map refData = new HashMap();
    refData.put("products", productManager.getProducts());
    // getProducts() returns a "List" of "Product"s.
    return refData;
    }

    ...
    }

    public class Order {
    private String product; // the type is "String", not "Product"
    private int amount;
    // getters and setters
    }

    public class Product {
    private int id;
    private String description;
    private double price;
    // getters and setters
    }

    // order.jsp
    <spring:bind path="order.product">
    <select name="product">
    <c:forEach var="entry" items="${products}">
    <option value="<c:out value="${entry.description}"/>"
    <c:if test="${entry.description == order.product}">selected="selected"</c:if>
    >
    <c:out value="${entry.description}"/>
    </option>
    </c:forEach>
    </select>
    </spring:bind>

  10. #10
    Join Date
    Aug 2004
    Location
    The Netherlands
    Posts
    160

    Default maybe this example helps

    I am not really sure if this helps, but this is one of the select boxes I have created within a wizard form. All the other parts seem oke, just the jsp has some difference. I just cannot point you what is wrong. Probably is something small :-)

    Code:
    <spring:nestedPath path="order.paymentMethod">
    	<spring:bind path="id">
    	        <select name="${status.expression}">
    	        	<option value=""></option>
    	        	<c:forEach items="${paymentMethods}"  var="payMethod">
    				<option value="${payMethod.id}"
    					<c:if test="${status.value == payMethod.id}">selected="selected"</c:if>
    				>${payMethod.name}</option>
    	        	</c:forEach>
    	        </select>
    	        <span class="fieldError">${status.errorMessage}</span>
    	</spring:bind>
    </spring:nestedPath>
    Hope this helps.
    Jettro Coenradie
    http://www.gridshore.nl

Posting Permissions

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