|无效的财产'电子邮件'豆类|当我将用户输入传回表单中的字段时

时间:2018-01-09 20:11:22

标签: java forms spring-mvc

我想将用户输入传回到表单中的字段内,这样他就不必再写一次了,因为他忘了填写一个字段。我尝试了this question的一些方法,但现在我收到了这个错误:

org.springframework.beans.NotReadablePropertyException: Invalid property 'email' of bean class [org.springframework.validation.support.BindingAwareModelMap]: Bean property 'email' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter?

我的代码是

控制器

@RequestMapping(value = "/registration", method = RequestMethod.GET)
public ModelAndView getRegistration(Model model)
{
    ModelAndView modelAndView;
    if(model.containsAttribute("user")) {
        // **************** Here is the exception ****************
        modelAndView = new ModelAndView("registration", "command", model);
    }
    else {
        modelAndView = new ModelAndView("registration", "command", new User());
        modelAndView.addObject("sexValues", SexEnum.values());
    }
    //ModelAndView modelAndView = new ModelAndView("registration", "command", new User());

    return modelAndView;
}

@RequestMapping(value = "/registration", method = RequestMethod.POST)
public String postRegistration(HttpServletRequest request, RedirectAttributes attr, @ModelAttribute("user") User user, ModelMap model)
{

    if(!Objects.equals(user.getPassword(), user.getPasswordConfirmation())) {
        model.addAttribute(ERROR_ATTRIBUTE, "Hesla se neshodují.");

        return "redirect:/registration";
    }

    try {
        userManager.register(user);
        return "redirect:/";
    }
    catch (UserValidationException e) {
        //***** I'm trying to pass user data to redirect ****
        model.addAttribute(ERROR_ATTRIBUTE, e.getMessage());
        attr.addFlashAttribute("user", user);

        return "redirect:/registration";
    }
}

Registration.jsp

<form:form class="px-2 py-1 form-border" method="post">
  <div class="form-row">
    <div class="form-group col-md-6">
      <form:label class="col-form-label" for="email" path="email">E-mail:</form:label>
      <form:input type="email" class="form-control" id="email" placeholder="E-mail" maxlength="50" path="email"/>
    </div>
    <div class="form-group col-md-6">
      <form:label class="col-form-label" for="nickname" path="username">Přezdívka:</form:label>
      <form:input type="text" class="form-control" id="nickname" placeholder="Přezdívka" maxlength="50" path="username"/>
    </div>
  </div> 
<div class="form-row"> 
  <div class="form-group text-center col-sm-12">
    <input type="submit" class="btn btn-primary col-md-5" id="registration" value="Registrovat">
  </div>
</div>     

User.java

@Entity
@Table(name = "Users")

public class User extends BaseObject{

    /** User's name */
    private String username;
    /** Hash of user's password */
    private String passwordHash;
    /** User's e-mail */
    private String email;
    /** User's first name */
    private String firstName;
    /** User's last name */
    private String lastName;
    /** User's date of birth */
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date dateOfBirth;
    /** User's sex */
    private SexEnum sex;

    /** Users's password (in web form) */
    private String password;
    /** Users's password confirmation (in web form) */
    private String password2;

    /** Date of user's creation */
    private Date dateOfCreation;

    /**************************************************************
     * Create actual date of user creation.
     **************************************************************/
    @PrePersist
    protected void onCreate() {
        dateOfCreation = new Date();
    }

    /**************************************************************
     * New user constructor
     *
     * @param username User's username
     * @param email User's e-mail
     * @param firstName User's first name
     * @param lastName User's last name
     * @param dateOfBirth User's date of birth
     * @param sex User's sex
     **************************************************************/
    public User(String username, String email, String firstName, String lastName, Date dateOfBirth, SexEnum sex) {
        this.username = username;
        this.email = email;
        this.firstName = firstName;
        this.lastName = lastName;
        this.dateOfBirth = dateOfBirth;
        this.sex = sex;
    }

    /**************************************************************
     * Default user constructor
     **************************************************************/
    public User() {

    }

    public void validate() throws UserValidationException {
        if(StringUtils.isBlank(email)) {
            throw new UserValidationException("E-mail je povinný!");
        }
        if(StringUtils.isBlank(username)) {
            throw new UserValidationException("Přezdívka je povinná!");
        }
        if(StringUtils.isBlank(firstName)) {
            throw new UserValidationException("Jméno je povinné!");
        }
        if(StringUtils.isBlank(lastName)) {
            throw new UserValidationException("Příjmení je povinné!");
        }
        if(StringUtils.isBlank(password)) {
            throw new UserValidationException("Heslo je povinné!");
        }
        if(sex == null) {
            throw new UserValidationException("Pohlaví je povinné!");
        }
    }
}
   // plus getters and setters for every field

如果我打开页面registration,它将毫无例外地打开。但是,如果我发送带有一些空白字段的表单并且代码正在通过IF部分,我将获得异常。你能否告诉我如何以正确的方式处理表单中的传递值?

0 个答案:

没有答案