【问题标题】:<form:select spring mvc shows the object as a string<form:select spring mvc 将对象显示为字符串
【发布时间】:2015-01-09 16:47:39
【问题描述】:

大家好,我正在使用 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";
}

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>

结果

【问题讨论】:

  • 我认为您缺少嵌套在 &lt;form:select...&gt; 标签内的选项 &lt;form:options items="${listaSupplieres}" itemValue="nameOfValue" itemLabel="nameOfLabel"/&gt;
  • 哦,我刚刚注意到一个错字。在表单上你有 ${listaSupplieres} 但在控制器上你有 listSupplier 作为属性名称 - 这是故意的吗?

标签: java spring spring-mvc jakarta-ee


【解决方案1】:

您没有指定用于选项的内容。试试这个:

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

请注意items 名称已更改为与@Controller 给出的名称匹配

【讨论】:

  • 这可能是最好的办法。
猜你喜欢
  • 1970-01-01
  • 2013-04-02
  • 1970-01-01
  • 1970-01-01
  • 2013-09-18
  • 2017-09-13
  • 1970-01-01
  • 1970-01-01
  • 2020-04-26
相关资源
最近更新 更多