提交表单

时间:2017-03-10 12:33:43

标签: spring forms modelattribute

我有两个"形式"类:登录和注册,注册扩展登录,因为它们共享emailAddress和密码字段。

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

对于注册表单,我的注册对象为空,这里是构建注册视图的方法:

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

Login对象作为参数传递,因为可以通过提交登录表单来访问此页面,以便通过emailAddress和密码字段填充它(除了&#之外还有一个"注册"按钮) 34;登录" one)。

这里是注册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()中实例化的对象。

1 个答案:

答案 0 :(得分:0)

您使用的是multipart/form-data表单。如果您不使用Spring Boot,则必须注册MultipartResolver。

看一下下面的链接:

DEMO HERE

Using a MultipartResolver