简体   繁体   中英

Empty modelAttribute after submitting a form

I have two "form" classes : Login and Registration, Registration extends Login because they share emailAddress and password fields.

When I submit login form everything works fine, I don't receive an empty Login object.

For Registration form, my Registration object is empty, here's the method building the registration view :

@RequestMapping(value = "inscription/", method = {RequestMethod.GET,
                                                  RequestMethod.POST})
public ModelAndView getRegistrationPage(@ModelAttribute Registration registration,
                                        @ModelAttribute Login login) {
    ModelAndView modelAndView;
    modelAndView = render("registration");
    modelAndView.addObject("headTitle", "Inscription");

    if(!login.isEmpty()){
        registration.setEmailAddress(login.getEmailAddress());
        registration.setPassword(login.getPassword());
    }

    modelAndView.addObject("registration", registration);
    return modelAndView;
}

A Login object is passed as a parameter as one can access this page by submitting the login form for it to be filled by emailAddress and password fields (it has a "Sign Up" button besides the "Sign In" one).

Here's the registration jsp form :

<form:form method="post" action="${WEBROOT}inscription-p/" modelAttribute="registration" class="sign" enctype="multipart/form-data">

    <form:label path="lastName">Nom</form:label><br/>
    <form:input type="text" path="lastName" id="last_name" size="30" value="" /><form:errors path="lastName" class="errors" element="label"/><br/>

    <form:label path="firstName">Prenom</form:label><br/>
    <form:input type="text" path="firstName" id="first_name" size="30" value="${registration.firstName}"/><form:errors path="firstName" class="errors" element="label"/><br/>

    <form:label path="password">Mot de passe</form:label><br/>
    <form:input type="password" path="password" id="pwd" size="30" value="${registration.password}"/><form:errors path="password" class="errors" element="label"/><br/>

    <form:label path="passwordConfirm">Confirmez votre mot de passe</form:label><br/>
    <form:input type="password" path="passwordConfirm" id="pwd_comfirm" size="30"  value="${registration.passwordConfirm}"/><form:errors path="passwordConfirm" class="errors" element="label"/><br/>

    <form:label path="address">Adresse</form:label><br/>
    <form:input type="text" path="address" id="address" size="30" value="${registration.address}"/><form:errors path="address" class="errors" element="label"/><br/>

    <form:label path="city">Ville</form:label><br/>
    <form:input type="text" path="city" id="city" size="30" value="${registration.city}"/><form:errors path="city" class="errors" element="label"/><br/>

    <form:label path="zipCode">Code Postal</form:label><br/>
    <form:input type="text" path="zipCode" id="zip_code" size="30" value="${registration.zipCode}"/><form:errors path="zipCode" class="errors" element="label"/><br/>

    <form:label path="phoneNumber">Telephone</form:label><br/>
    <form:input type="text" path="phoneNumber" id="phone_number" size="30" value="${registration.phoneNumber}"/><form:errors path="phoneNumber" class="errors" element="label"/><br/>

    <form:label path="emailAddress">Adresse email</form:label><br/>
    <form:input type="email" path="emailAddress" id="email_address" size="30"  value="${registration.emailAddress}"/><form:errors path="emailAddress" class="errors" element="label"/><br/>

    <form:label path="idCard">Pièce d'identité</form:label><br/>
    <form:input type="file" path="idCard" id="id_card"  style="position: relative; margin-left: 42.5%"/><form:errors path="idCard" class="errors" element="label"/><br/>

    <input type="submit" value="Valider" class="valider"/>

</form:form>

Submittion is proceeded by this method :

@RequestMapping(value = "inscription-p/", method = RequestMethod.POST)
public Redirection signUp(@ModelAttribute Registration registration,
                                          RedirectAttributes redirectAttributes,
                                          BindingResult bindingResult) {

    registrationService.preRegister(registration, userSession, bindingResult);

    return new Redirection(webroot+"inscription/");
}

In this one, Registration object is empty, it is a new object, not the one instantiated in getRegistrationPage().

You are using a multipart/form-data form. If you are not using Spring Boot you have to register a MultipartResolver.

Take a look the links below:

Using a MultipartResolver

Uploading Files

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM