我正在尝试创建一个传递多个对象的窗体。我正在尝试创建一个窗体,您可以在其中找到' product
'和' customer
'并将它们添加到订单中。我有两个模型课 Customer
以及 Product
. Customer
有 phoneNo
变量, Product
有 name
变量。我发现传递多个对象的最简单方法是将它们添加到列表并传递 List
到 View
. 这就是我要做的,但是访问那些模型对象是另一回事。
控制器类:
@Controller
public class OrderController {
@RequestMapping(value="/create_order", method= RequestMethod.GET)
public ModelAndView create_order() {
Map<String, Object> model = new HashMap<String, Object>();
model.put("customer", new Customer());
model.put("product", new Product());
return new ModelAndView("create_order", "command", model);
}
}
页码:
<form:form method="POST" action="/Persistence_Web/order_confirmation" commandName="model">
<table>
<!-- I tried few different ways of writing it and all fail -->
<tr><form:label path="model[0].phoneNo">Customer phone number</form:label></tr>
<tr><form:input path="model[0].phoneNo"></form:input></tr>
....
2条答案
按热度按时间r8xiu3jd1#
引用属性
Product.name
以及Customer.phoneNo
在您的模型中,您必须引用您在模型中提供的名称加上属性名称(类中必须有getter!!!)但是:只要类中的getter方法与视图中的变量名相似。你可以喝一杯
getMyCalculation()
方法没有myCalculation
属性,以便计算总计、平均值、格式化日期或需要在视图中显示但不存储在模型中的任何内容。另外,对于各种
Products
或者Customers
使用列表:用一个
<for:earch>
```<c:forEach items="${customers}" var="customer">
<c:out value="${customer.phoneNo}"/>
</c:forEach>
elcex8rz2#
据我所知,您的问题是不能以这种方式访问模型对象的。请尝试使用spring占位符
${command.get('customer').phoneNo}
.