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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29912971/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 16:07:05  来源:igfitidea点击:

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

javaspringjspspring-mvc

提问by Edgar.A

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. I have 2 model classes Customerand Product. Customerhas phoneNovariable, Producthas namevariable. I found that easiest way to pass multiple objects is to add them to list and pass Listto a View. That's what I'm doing, but accessing those model objects is a different story.

我正在尝试创建一个传递多个对象的表单。我正在尝试创建一个表单,您可以在其中找到“ product”和“ customer”并将它们添加到订单中。我有 2 个模型类CustomerProduct. CustomerphoneNo变,Productname变。我发现传递多个对象的最简单方法是将它们添加到列表并传递ListView. 这就是我正在做的事情,但访问这些模型对象是另一回事。

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

采纳答案by Jordi Castilla

To refer the attributes Product.nameand Customer.phoneNoin 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.name,并Customer.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. The naming must be:

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

private int attributeName
public  int getAttributeName()

BUT: as long as the getter method in the class is like your variable namein the view. You can have a getMyCalculation()method WITHOUTmyCalculationattribute in order to calculate totals, averages, formatting dates, or whatever you need to show in the view but not store in the model.

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

Also, for various Productsor Customersuse 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>

回答by androberz

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}