提交表单后清空 modelAttribute

Empty modelAttribute after submitting a form

我有两个 "form" 类:登录和注册,注册扩展了登录,因为它们共享电子邮件地址和密码字段。

当我提交登录表单时一切正常,我没有收到空的登录对象。

对于注册表单,我的注册对象是空的,下面是构建注册视图的方法:

@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;
}

登录对象作为参数传递,因为可以通过提交登录表单来访问此页面,并填写电子邮件地址和密码字段(除了 [=29= 按钮外还有一个 "Sign Up" 按钮]一个)。

这是注册 jsp 表格:

<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>

提交通过此方法进行:

@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/");
}

在这一个里,Registration对象是空的,是一个新的对象,不是getRegistrationPage()中实例化的对象。

您正在使用 multipart/form-data 表单。如果您不使用 Spring Boot,则必须注册一个 MultipartResolver。

看看下面的链接:

Using a MultipartResolver

Uploading Files