繁体   English   中英

Spring MVC-从JSP中的列表访问表单的对象

[英]Spring MVC - accessing objects from a list in JSP for a form

我正在尝试创建带有多个对象的表单。 我正在尝试创建一个表单,您可以在其中找到“ product ”和“ customer ”并将其添加到订单中。 我有2个模型类CustomerProduct CustomerphoneNo变量, Productname变量。 我发现传递多个对象的最简单方法是将它们添加到list并将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>
....

要在模型中引用属性Product.nameCustomer.phoneNo ,必须引用在模型中提供的名称以及属性名称( 该类中必须具有getter !!!

<tr><form:input path="${customer.phoneNo}"></form:input></tr>
<tr><form:input path="${product.name}"></form:input></tr>

*如果要在视图中操作它们,则类中的属性必须具有getter和setter。 命名必须是:

private int attributeName
public  int getAttributeName()

但是 :只要类中的getter方法就像视图中的变量名一样。 您可以使用不带 myCalculation属性的getMyCalculation()方法,以计算总计,平均值,格式化日期或需要在视图中显示但不存储在模型中的任何内容。

另外,对于各种ProductsCustomers使用以下列表:

List<Customer> customers = // fill the list!!!
model.put("customers", customers);

并使用<for:earch>进行迭代

<c:forEach items="${customers}" var="customer">     
    <c:out value="${customer.phoneNo}"/>
</c:forEach>

据我了解您的问题,您不能以这种方式访问​​模型对象。 请尝试使用Spring占位符${command.get('customer').phoneNo}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM