|bean 类的无效属性“email”|当我将用户输入传递回表单字段时

问题描述 投票:0回答:1

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

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

注册.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>     

用户.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
部分,我将得到异常。您能给我提示如何以正确的方式处理从表单返回的值吗?

java forms spring-mvc
1个回答
0
投票

我通过清除 cookie 就解决了这个问题。

© www.soinside.com 2019 - 2024. All rights reserved.