Announcement Announcement Module
No announcement yet.
bind a dynamic drop-down list Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

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

    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>
    <SELECT name="typeId">
    <c:forEach var="type" items="${types}">
    <c:if test="${ ==}">
    <OPTION selected="<c:out value="${}"/>" value="<c:out value="${}"/>"><c:out value="${}"/></OPTION>
    <c:if test="${ !=}">
    <OPTION value="<c:out value="${}"/>"><c:out value="${}"/></OPTION>


    • #3

      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
        I don't understand where did the JVM find the order variable?

        you have to show me more codes.


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


          • #6
            You should have a look at your jsp page, what are the spring:bind tags around your <c:foreach ... /> tag?


            • #7
              My form is a subclass of SimpleFormController


              • #8

                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
                  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" });

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


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

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


                    • #11
                      my bad

                      stupid me. the error is actually on another part of the JSP code. the page works fine after I took out that piece. sorry about that. thanks again.