Announcement Announcement Module
Collapse
No announcement yet.
bind a dynamic drop-down list Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • 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
    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>

    Comment


    • #3
      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.

      Comment


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

        you have to show me more codes.

        Comment


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

          Comment


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

            Comment


            • #7
              My form is a subclass of SimpleFormController

              Comment


              • #8
                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)?

                Comment


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

                  Comment


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

                    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.

                    Comment


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

                      Comment

                      Working...
                      X