[英]org.hibernate.PropertyAccessException: Null value was assigned to a property of boolean type
I'm working on java web app, which uses Spring Boot, Hibernate and thymeleaf. 我正在使用Spring Boot,Hibernate和thymeleaf的Java Web应用程序上工作。 At the moment I'm trying to implement registration process for my application and I'm stuck on a problem with my entity class.
目前,我正在尝试为我的应用程序实现注册过程,但是我遇到了实体类问题。
Part of User @Entity clas 用户@Entity clas的一部分
@Column(name = "aktywny")
private boolean enabled;
@Column(name = "token")
private String confirmationToken;
public boolean getEnabled() {
return enabled;
}
public void setEnabled(boolean value) {
this.enabled = value;
}
Request method 申请方法
@RequestMapping(value = "/register", method = RequestMethod.POST)
public ModelAndView processRegistrationForm(Model model, ModelAndView modelAndView, @Valid User user, BindingResult bindingResult, @RequestParam Map requestParams, RedirectAttributes redir, HttpServletRequest httpServletRequest){
//Lookup user in db by email
User userExist = userService.findByEmail(user.getEmail());
System.out.println(userExist);
if( userExist != null){
model.addAttribute("alreadyRegisteredMessage", "Użytkownik o podanym adresie e-mail już istnieje");
bindingResult.reject("email");
}
if(bindingResult.hasErrors()){
modelAndView.setViewName("home");
}else {
//set disabled until confirmation link clicked
user.setEnabled(false);
//generate string token
user.setConfirmationToken(UUID.randomUUID().toString());
Zxcvbn passwordCheck = new Zxcvbn();
Strength strength = passwordCheck.measure(requestParams.get("password").toString());
if(strength.getScore() < 3) {
bindingResult.reject("password");
redir.addFlashAttribute("errorMessage", "Twoje hasło jest zbyt słabe, wybierz silniejsze");
modelAndView.setViewName("redirect: confirm?token=" + requestParams.get("token"));
System.out.println(requestParams.get("token"));
// Set new password
user.setPassword(bCryptPasswordEncoder.encode(requestParams.get("password").toString()));
}
userService.saveUser(user);
String appUrl = httpServletRequest.getScheme() + "://" + httpServletRequest.getServerName();
SimpleMailMessage registrationEmail = new SimpleMailMessage();
registrationEmail.setTo(user.getEmail());
registrationEmail.setSubject("Potwierdzenie rejestracji");
registrationEmail.setText("Aby dokończyć rejestrację, kliknij w poniższy link: "
+ appUrl + "/confirm?token=" + user.getConfirmationToken());
registrationEmail.setFrom("hotelwaltertorun@gmail.com");
emailService.sendEmail(registrationEmail);
if (user == null) { // No token found in DB
modelAndView.addObject("invalidToken", "Oops! This is an invalid confirmation link.");
} else { // Token found
modelAndView.addObject("confirmationToken", user.getConfirmationToken());
}
model.addAttribute("confirmationMessage", "E-mail potwierdzający został wysłany na adres " + user.getEmail());
modelAndView.setViewName("home");
}
return modelAndView;
}
HTML form code HTML表单代码
<form th:autocomplete="on" id="register_form" class="form-horizontal" action="#"
th:action="@{/register}" th:object="${user}" method="post" role="form"
data-toggle="validator">
<input type="hidden" name="token" th:value="${confirmationToken}">
<div class="col-md-6 form-group">
<div class="input-group">
<span class="input-group-addon"><i class="glyphicon glyphicon-user"></i></span>
<input type="text" th:field="*{firstname}"
placeholder="Imię" class="form-control" required/>
</div>
</div>
<div class="col-md-6 form-group">
<div class="input-group">
<span class="input-group-addon"><i class="glyphicon glyphicon-user"></i></span>
<input type="text" th:field="*{lastname}"
placeholder="Nazwisko" class="form-control" required/> </div>
</div>
<div class="col-md-6 form-group">
<div class="input-group">
<span class="input-group-addon"><i class="glyphicon glyphicon-user"></i></span>
<input type="text" th:field="*{username}"
placeholder="Login" class="form-control" required/>
</div>
</div>
<div class="col-md-6 form-group">
<div class="input-group">
<span class="input-group-addon"><i class="glyphicon glyphicon-lock"></i></span>
<input name="password" type="password" id="password"
placeholder="Hasło" class="form-control" required />
</div>
</div>
<div class="col-md-6 form-group">
<div class="input-group">
<span class="input-group-addon"><i class="glyphicon glyphicon-lock"></i></span>
<input type="password" class="form-control" id="signup-password-confirm" placeholder="Potwierdź hasło" name="ConfirmPassword" data-fv-notempty="true"
data-fv-notempty-message="Please confirm password"
data-fv-identical="true"
data-fv-identical-field="password"
data-fv-identical-message="Both passwords must be identical" />
</div>
</div>
<div class="col-md-6 form-group">
<div class="input-group">
<span class="input-group-addon"><i class="glyphicon glyphicon-envelope"></i></span>
<input type="email" th:field="*{email}"
placeholder="Adres e-mail" class="form-control"
data-error="This email address is invalid" required />
</div>
</div>
<div class="col-md-6 form-group">
<div class="input-group">
<span class="input-group-addon"><i class="glyphicon glyphicon-phone"></i></span>
<input type="tel" th:field="*{phone}"
placeholder="Telefon" class="form-control"
data-error="This email address is invalid" required />
</div>
</div>
<div class="col-md-6 form-group">
<button id="register" class="btn btn-success" name="register" style="width:100%;">Zarejestruj <span class="glyphicon glyphicon-send"></span></button>
</div>
</form>
and error description from browser 和浏览器中的错误描述
There was an unexpected error (type=Internal Server Error, status=500).
发生意外错误(类型=内部服务器错误,状态= 500)。 org.hibernate.PropertyAccessException: Null value was assigned to a property [class com.kaceper.model.User.enabled] of primitive type setter of com.kaceper.model.User.enabled
org.hibernate.PropertyAccessException:空值已分配给com.kaceper.model.User.enabled原始类型设置器的属性[class com.kaceper.model.User.enabled]
Thanks for help 感谢帮助
Thymeleaf is trying to execute something like this: Thymeleaf正在尝试执行以下操作:
user.setEnabled(null)
Which causes a NullPointerException
. 这会导致
NullPointerException
。 Change the enabled
field to Boolean
instead of boolean
and update the getter and setter accordingly. 将
enabled
字段更改为Boolean
而不是boolean
并相应地更新getter和setter。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.