Announcement Announcement Module
Collapse
No announcement yet.
Spring MVC data binding Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring MVC data binding

    Hi,

    I don't know if anyone has ever posted questions like this. I've browsing for a couple of pages in this forum and I found nothing. So I decided to start my situation and problems I am facing.

    I am building a web application using spring MVC + hibernate. I am focusing on Spring MVC part and someone else is doing hibernate work. Any newly created object will represent an empty row with only primary key in database.
    e.g. Instructor instructor = new Instructor(); The dao.save(instructor) will basically create a new row with only a primary key. We realize that we have to set the object as null if we do not want hibernate to create an empty row for an empty object.

    This is basically a conflict to the spring MVC data binding.
    Object that I have:
    public class Registry {
    private final Logger logger = Logger.getLogger(Registry.class);
    /** identifier field */
    private int registryId;
    private Instructor instructor;
    private InstructorType instructorType;
    ......
    getters and setters
    }

    The instructor and instructor type are mutual exclusive. This means a registry can have only an instructor or an instructor type or both null.

    On jsp, I am using spring custom taglib.
    <sform:select path="instructor.id">
    <sform:options items="${instructors}" itemValue="id" itemLabel="user.name" />
    </sform:select>
    <sform:select path="instructorType.cd">
    <sform:options items="${look_instructorTypes}" itemValue="key" itemLabel="value" />
    </sform:select>

    If I use spring custom taglib, I have to initialize the instructor and instructortype object and set it to the registry object. Otherwise, I always received error like:
    org.springframework.beans.NullValueInNestedPathExc eption: Invalid property 'instructor' of bean class [frameworks.scd.controller.admin.RegistryFormData]: Value of nested property 'instructor' is null

    I did sth like in formBackingObject method.
    registry.setInstructor(new Instructor());
    registry.setInstructorType(new LookInstructorType());

    My form will only allow user specify either instructor or instructortype or both empty. So after binding, either instructor or instructortype will be an empty object. For hibernate to work correctly, I have to set that empty object to null manually.

    Anyone has ever experienced similar situation?
Working...
X