Spring form binding return null with many-2-one relationship

64 Views Asked by At

Here is the problem, when I try to submit form, user entity returns null.

Form is

<form:form class="g-form" modelAttribute="objView" id="userAssignmentForm">
<form:hidden path="id" value="${objView.id}"/>
        ${objView.user.id}
        <div class="g-form-group required">
            <label for="user">User</label>
            <form:hidden id="user" path="user" value="${objView.user}"/>
            <input type="text" value="${objView.user.userName}" readonly="true"/>
            <input type="button" class="import-input" onclick="gImport.showImportUserForm()"/>
        </div> 

Controller is

@RequestMapping(value = "/create", method = RequestMethod.POST)
public @ResponseBody
String create(
        @ModelAttribute("objView") UserAssignmentView objView, BindingResult result,
        SessionStatus status,
        HttpServletRequest request) throws UnsupportedEncodingException {

    UserAssignment obj = new UserAssignment();
    obj.setUser(objView.getUser());
    userAssignmentService.create(obj);

    return "ok";
}

Model is below contains a view entity. What am I missing?

public class UserAssignmentView extends UserAssignment {

public UserAssignmentView() {
}

public UserAssignmentView(UserAssignment obj) {
    setId(obj.getId());
    setStatus(obj.getStatus());
    setUser(obj.getUser());
}

}

And this is form view part of controller

@RequestMapping(value = "/form", method = RequestMethod.POST)
public ModelAndView form(HttpServletRequest request) {
    UserAssignment obj = new UserAssignment();
    Account account = AccountRegistry.getByHttpSession(request.getSession());
    ModelAndView modelAndView = new ModelAndView("forms/userAssignmentForm");
    modelAndView.addObject("objView", UserAssignmentWrapper.wrap(obj));
    return modelAndView;
}

I could not solve since 3 days, how can I set user to userassignment?

0

There are 0 best solutions below