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.
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.
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>
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.
I don't understand where did the JVM find the order variable?
you have to show me more codes.
Is your controller a subclass of AbstractFormController (or one of its subclasses: SimpleFormController or AbstractWizardFormController)?
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
My form is a subclass of SimpleFormController
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)?
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>
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 :-)
Hope this helps.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>
Jettro Coenradie
http://www.gridshore.nl