<form:select spring mvc将对象显示为字符串

Eli*_*gas 3 java spring spring-mvc java-ee

大家好,我正在使用spring mvc,并且我有一个表单,在其中必须逐步使用组合框,才能将要加载到组合框的对象驱动程序列表显示出来,但事实证明,我将其显示为字符串而不是对象。

在上方一点,我有一个组合框,我以相同的方式通过,但在JSTL中格式和位置都不正确。

我需要的是为组合框加载对象的前两个属性

这是代码和图像,因此它们可以为我提供帮助。

控制者

@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";
}
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

结果

SpringSelect

blu*_*fus 5

您未指定要使用的选项。尝试这个:

<form:select path="supplierID" multiple="false">
      <form:options items="${listSupplier}" itemValue="SupplierID" itemLabel="CompanyName"/>
 </form:select>
Run Code Online (Sandbox Code Playgroud)

请注意,items名称已更改为与@Controller