2

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;
3
  • Paste Code of User Class Commented Aug 14, 2013 at 14:05
  • Yes details of User class would be useful. Also what's userRepository Commented Aug 14, 2013 at 14:09
  • 1
    @cowls userRepository is probably a @Repository component that handle data access. Commented Aug 14, 2013 at 14:16

1 Answer 1

3

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

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

3 Comments

Firebug send the data as RequestPayload, ------WebKitFormBoundary0dEr9CJVMOP5TYVA Content-Disposition: form-data; name="firstName" Rodrigo.
Removed the suggested enctype.
Yes, I tested now and It worked, I´m migrating from php, Java, for me, is too complicated...best regard´s.

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.