digoferra
digoferra

Reputation: 1001

Spring MVC form setting fields as null

I´m studying Spring MVC with Tiles 3, JPA and all other cool stuff. I created a form to send data, below is the form:

<form:form method="POST" action="/users/save" enctype="multipart/form-data" modelAttribute="formUsuario">
    <form:label path="username" for="username">Username</form:label>
    <form:input type="text" id="username" path="username" class="span4" value="${user.username}" />
    <form:label path="firstName" for="firstName">First name</form:label>
    <form:input type="text" id="firstName" path="firstName" class="span4" value="${user.firstName}" />
</form:form>

The method inside the controller to add a new record:

@Secured("ROLE_USER")
@RequestMapping(value = "/new", method = RequestMethod.GET)
public String getNew(@ModelAttribute("formUsuario") br.com.rodrigoferra.domain.User user) {
    logger.info("Add new user");
    return "users/edit";
}

The method to save the new record:

@Secured("ROLE_USER")
@RequestMapping(value = "/save", method = RequestMethod.POST)
public @ResponseBody String doSave(@ModelAttribute("formUsuario") br.com.rodrigoferra.domain.User user, BindingResult results, Model model) {
    if(results.hasErrors()) {
        return "users/edit";
    }
    user = userRepository.save(user);
    logger.info("Username: " + user.getUsername());
    return "redirect:/users/";
}

It is saving the record, but it is saving everything as Null! The data from the form is being received by the controller. I searched a lot and found the same examples as this, but I can't get mine to work!

Is there any configuration I've missed?

Thanks and sorry for my bad english.

Edited:

Users bean:

@Entity(name="users")
public class User implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue( strategy = GenerationType.IDENTITY)
    private Long id;

    private String firstName;
    private String lastName;

    @Column(unique=true)
    private String username;

    private String password;
    @Transient
    private String passwordConfirmation;

    private Date birthdate;

    private String signature;
    private String email;
    private String sex;

    private int active;

Upvotes: 2

Views: 2905

Answers (1)

Sithsu
Sithsu

Reputation: 2219

Use Firebug or similar tool to see if the form field values are correctly included in the request, and the request is sent to correct URI.

Log (or debug) and check the user object content right after receiving the request, in doSave().

Log (or debug) and check the user object content in userRepository.save() to make sure all values are available, prior to persisting it.

Following could be the actual reason, but first checking above would iron out other possiblities

Remove enctype="multipart/form-data" in form definition as this form does not intend to send binary data.
Check following for more info on that:
What does enctype='multipart/form-data' mean?
Form content types in HTML 4 spec
Multipart form data encoding in HTML 5 spec

Upvotes: 3

Related Questions