Spring MVC - accessing objects from a list in JSP for a form
Solution 1
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!!!)
<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:
private int attributeName
public int getAttributeName()
BUT: as long as the getter method in the class is like your variable name in the view. 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.
Also, for various Products
or Customers
use a list:
List<Customer> customers = // fill the list!!!
model.put("customers", customers);
And iterate with a <for:earch>
<c:forEach items="${customers}" var="customer">
<c:out value="${customer.phoneNo}"/>
</c:forEach>
Solution 2
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}
.
Edgar.A
Updated on June 15, 2022Comments
-
Edgar.A almost 2 years
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 classesCustomer
andProduct
.Customer
hasphoneNo
variable,Product
hasname
variable. I found that easiest way to pass multiple objects is to add them to list and passList
to aView
. 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> ....
-
Edgar.A about 9 yearsYup thank you, it worked. On example i did with single object path was defined with single string like path="variable", so that got me confused. And for getter - of course it does, but does it matter if it has default naming like "getVariableName()", or can getter have any name as long as it returns variable needed?
-
androberz about 9 yearsOh, my bad, I haven't noticed that model object are used in the
path
attr. I meant that to access model's object you have to use plaseholders, and in this way you should specify something like that:${command.get("customer")}