简体   繁体   English

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

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

I am trying to create a form with multiple objects being passed. 我正在尝试创建带有多个对象的表单。 I am trying to create a form where you can find ' product ' and ' customer ' and add them to order. 我正在尝试创建一个表单,您可以在其中找到“ product ”和“ customer ”并将其添加到订单中。 I have 2 model classes Customer and Product . 我有2个模型类CustomerProduct Customer has phoneNo variable, Product has name variable. CustomerphoneNo变量, Productname变量。 I found that easiest way to pass multiple objects is to add them to list and pass List to a View . 我发现传递多个对象的最简单方法是将它们添加到list并将List传递给View That's what I'm doing, but accessing those model objects is a different story. 这就是我正在做的事情,但是访问那些模型对象却是另一回事。

Controller class: 控制器类:

 @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);
    }
    }

Page: 页:

<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>
....

To refer the attributes Product.name and Customer.phoneNo in your model, you have to refer the names you give in the model plus the attribute name ( that must have a getter in the class!!! ) 要在模型中引用属性Product.nameCustomer.phoneNo ,必须引用在模型中提供的名称以及属性名称( 该类中必须具有getter !!!

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

*The attributes in the classes MUST have getters and setters if you want to manipulate them in the view. *如果要在视图中操作它们,则类中的属性必须具有getter和setter。 The naming must be: 命名必须是:

private int attributeName
public  int getAttributeName()

BUT : as long as the getter method in the class is like your variable name in the view. 但是 :只要类中的getter方法就像视图中的变量名一样。 You can have a getMyCalculation() method WITHOUT myCalculation attribute in order to calculate totals, averages, formatting dates, or whatever you need to show in the view but not store in the model. 您可以使用不带 myCalculation属性的getMyCalculation()方法,以计算总计,平均值,格式化日期或需要在视图中显示但不存储在模型中的任何内容。

Also, for various Products or Customers use a list: 另外,对于各种ProductsCustomers使用以下列表:

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

And iterate with a <for:earch> 并使用<for:earch>进行迭代

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

As far as I understood your question you can't access model object in this way. 据我了解您的问题,您不能以这种方式访问​​模型对象。 Please try using spring placeholders ${command.get('customer').phoneNo} . 请尝试使用Spring占位符${command.get('customer').phoneNo}

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

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