springmvc-从jsp中的一个表单列表访问对象

qlzsbp2j  于 2021-07-13  发布在  Java
关注(0)|答案(2)|浏览(193)

我正在尝试创建一个传递多个对象的窗体。我正在尝试创建一个窗体,您可以在其中找到' product '和' customer '并将它们添加到订单中。我有两个模型课 Customer 以及 Product . CustomerphoneNo 变量, Productname 变量。我发现传递多个对象的最简单方法是将它们添加到列表并传递 ListView . 这就是我要做的,但是访问那些模型对象是另一回事。
控制器类:

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

r8xiu3jd1#

引用属性 Product.name 以及 Customer.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方法与视图中的变量名相似。你可以喝一杯 getMyCalculation() 方法没有 myCalculation 属性,以便计算总计、平均值、格式化日期或需要在视图中显示但不存储在模型中的任何内容。
另外,对于各种 Products 或者 Customers 使用列表:

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>

elcex8rz

elcex8rz2#

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

相关问题