gpt4 book ai didi

java - <表格 :select spring mvc shows the object as a string

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:45:45 26 4
gpt4 key购买 nike

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

上面一点我有一个组合框,我以相同的方式传递,但在 JSTL 中不合形式和位置。

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

这是代码和图像,因此他们可以帮助我。

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>

结果

SpringSelect

最佳答案

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

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

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

关于java - <表格 :select spring mvc shows the object as a string,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26893657/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com