Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

<form:select spring mvc shows the object as a string

Hi all I am using spring mvc and I have a form where I have to use a combo box in step a list of objects driver to be loaded into the combo box but it turns out I show it as a string and not as the object .

a little above I have a combo box where I pass in the same way but is out of form and position with JSTL.

what I need is that the first two attributes of my object is loaded for the combo box

Here is the code and images so they can help me.

Controller

@RequestMapping(value="products.htm", method=RequestMethod.GET)
public String homeSuppliers(@RequestParam(required=false) String state, ModelMap model){

    try {

        if (state != null) {
            model.addAttribute("state", state);
        }

        List<ProveedoresDTO> listSupplier = supplierService.getAllSuppliersDTO();

        List<ProductosDTO> listProducts = productService.getAllProductsDTO();

        model.addAttribute("listProducts",listProducts);

        model.addAttribute("listSupplier",listSupplier);

        model.addAttribute("productAtt", new ProductsDTO());

    } catch (Exception e) {
        model.addAttribute("msg",e.getMessage());
    }

    return "productsView/products";
}

JSP

<select id="comboProducts" onchange="BuscaProductPorId()">
    <option value="0"></option>
    <c:forEach items="${listProducts}" var="product">
        <option value="${product.productID}">${product.productName}</option>
    </c:forEach>
</select>
<h1>${msg}</h1>

<form:form commandName="productAtt" action="crearProduct" method="get"
    id="formSend">
    <fieldset>
        <legend>Product</legend>
        <form:hidden path="productID" />
        <table>
            <tr>
                <td><form:label path="productName">Nombre Product</form:label></td>
                <td>:</td>
                <td><form:input path="productName" /></td>
                <td><form:errors path="productName" /></td>
            </tr>
            <tr>
                <td><form:label path="supplierID">Select Supplier</form:label></td> 
                <td>:</td>
                <td><form:select path="supplierID" multiple="false" items="${listSupplier}"></form:select></td> 
                <td><form:errors path="supplierID" /></td> 
            </tr> 
            <tr>
                <td><form:label path="quantityPerUnit">Cantidad por Unidad</form:label></td>
                <td>:</td>
                <td><form:input path="quantityPerUnit" /></td>
                <td><form:errors path="quantityPerUnit" /></td>
            </tr>
            <tr>
                <td><form:label path="unitPrice">Precio Unitario</form:label></td>
                <td>:</td>
                <td><form:input path="unitPrice" /></td>
                <td><form:errors path="unitPrice" /></td>
            </tr>
            <tr>
                <td><form:label path="unitsInStock">Unidades en Stock</form:label></td>
                <td>:</td>
                <td><form:input path="unitsInStock" /></td>
                <td><form:errors path="unitsInStock" /></td>
            </tr>
            <tr>
                <td><form:label path="unitsOnOrder">Unidades en Orden</form:label></td>
                <td>:</td>
                <td><form:input path="unitsOnOrder" /></td>
                <td><form:errors path="unitsOnOrder" /></td>
            </tr>
            <tr>
                <td><form:label path="reorderLevel">Nivel de Orden</form:label></td>
                <td>:</td>
                <td><form:input path="reorderLevel" /></td>
                <td><form:errors path="reorderLevel" /></td>
            </tr>
            <tr>
                <td><form:label path="discontinued">Descontinuado</form:label></td>
                <td>:</td>
                <td><form:input path="discontinued" /></td>
                <td><form:errors path="discontinued" /></td>
            </tr>
            <tr>
                <td><br></td>
            </tr>
            <tr>
                <td><input type="submit" value="Crear Product" name="crea"
                    id="crea"></td>
                <td><input type="button" onclick="formReset()"
                    value="Limpiar Campos" /></td>
            </tr>
        </table>
    </fieldset>
</form:form>

Result

SpringSelect

like image 636
Elias Vargas Avatar asked Dec 04 '25 17:12

Elias Vargas


1 Answers

You are not specifying what to use for your options. Try this:

<form:select path="supplierID" multiple="false">
      <form:options items="${listSupplier}" itemValue="SupplierID" itemLabel="CompanyName"/>
 </form:select>

Please note the items name has been changed to match the one given by the @Controller

like image 134
blurfus Avatar answered Dec 06 '25 07:12

blurfus