0

I'm using an object in another class which is used as a command object in form using spring mvc as shown below.

public class ManufacturerDO {
int manufacturerID;
String manufacturerName,manufacturerAddress;
}

this is an domain object under my command object.My doubt is how to get the fields in this domain object in my spring form.

I'm tried using the followings :

<form:select  path="productManufacturer.manufacturerID">
        <c:forEach var="manufacturer" items="${manufacturerList}">
            <form:option value="${manufacturer.manufacturerName}">
                <c:out value="${manufacturer.manufacturerName}" />
            </form:option>
        </c:forEach>
</form:select>

But getting the following exception: Invalid property 'productManufacturer' of bean class [com.global.onlineShop.ProductDO]: Value of nested property 'productManufacturer' is null.

Thanks in advance

1 Answer 1

1

1) You should pass a command object instance to the view. I do it this way:

public ModelAndView myFunc(ModelAndView model) {
    /* .... */
    ManufacturerDO commandObject = new ManufacturerDO;
    model.addObject("productManufacturer", commandObject);
    return model;
}

(However I advice to use autowiring and not to create a new instance in controller action)

2) In JSTL form tag point reference to the commandObject by provided key:

<form:form method="post" commandName="productManufacturer">

3) When you provide a command object in JSTL form tag you can skip a name of commandObject in the attribute 'path' in all form element JSTL tag so let's write:

<form:select path="manufacturerID">

4) And of course - do not forget about getters and setters in in a class of a command object.

Sign up to request clarification or add additional context in comments.

4 Comments

I'm using the above method for product which is my command object which contains productmanufacturer object as a field.now i want to access productManufacturer's data fields.how to achieve this?
Does class ProductDO have getter for its field productManufacturer?
Yes it has. package com.global.onlineShop; public class ProductDO { ManufacturerDO productManufacturer; public ManufacturerDO getProductManufacturer() { return productManufacturer; } public void setProductManufacturer(ManufacturerDO productManufacturer) { this.productManufacturer = productManufacturer; } }the above is the productDO domain object which is the command object in the form
Well, if pasted code does not work, I do not have any idea how to make it to work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.