0

I would like to pass user input back inside fields in form so he does not have to write it again only because he forgot to fill one field. I tried some approach from this question, but now I'm getting this error:

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?

My code is

Controller

@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

If I open page registration it will open without exception. But if I send form with some blank fields and code is going through IF section I will get the exception. Can you please give me hint how to handle passing values from form back with the right way?

Sk1X1
  • 1,305
  • 5
  • 22
  • 50

0 Answers0