我是春季启动新手,在尝试使用我为创建表单而创建的类中的服务时发现了一个问题
我创建了一个验证器类来验证表格,如此处提及
https://spring.io/guides/gs/validating-form-input/
按预期工作
package com.nexill.bank.form.user;
import com.nexill.bank.entity.User;
import com.nexill.bank.repository.UserRepository;
import org.hibernate.validator.constraints.Email;
import org.hibernate.validator.constraints.NotEmpty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.stereotype.Component;
import javax.validation.constraints.AssertTrue;
import javax.validation.constraints.Size;
@Component
@Configurable
public class UserForm {
@Autowired
UserRepository userRepository;
@Email(message = "Must be a valid email")
@NotEmpty(message = "Email cannot be empty")
private String email;
@NotEmpty(message = "Password cannot be empty")
@Size(min=6, max=30, message = "Password need to be between 6 to 30 characters")
private String password;
@NotEmpty(message = "Confirm password cannot be empty")
@Size(min=6, max=30, message = "Confirm password need to be between 6 to 30 characters")
private String confirmPassword;
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getConfirmPassword() {
return confirmPassword;
}
public void setConfirmPassword(String confirmPassword) {
this.confirmPassword = confirmPassword;
}
@AssertTrue(message = "Password and confirm password need to match")
public boolean isDifferentPass() {
return !password.equals(confirmPassword) ? false : true;
}
@AssertTrue(message = "Email already exists")
public boolean isUniqueEmailForUser() {
User x = userRepository.findUserByEmail(getEmail());
return (x) == null;
}
}
问题是即时获取HV000090:无法访问isUniqueEmailForUser。
当我骚扰存储库时为null。我已经在系统中使用了userRepository其他地方的autowire,它按预期工作。
主要包是com.nexill.bank包。我错过了什么。
表格在控制器中使用,如
@RequestMapping(value = "/adminDashboard/user", method= RequestMethod.POST)
public ModelAndView adminDashboardAddUserPost(ModelAndView modelAndView, @Valid UserForm userForm, BindingResult bindingResult) {
modelAndView.addObject("currentUser", authenticationFacade.getUser());
modelAndView.addObject("userForm", userForm);
if (!bindingResult.hasErrors()) {
User user = new User(userForm);
user.setRole(roleService.getRepository().findByName("BANK_STAFF"));
userService.createUser(user);
ModelAndView redirectModelAndView = new ModelAndView("redirect:/adminDashboard");
redirectModelAndView.addObject("saveStatus" , true);
return redirectModelAndView;
}
modelAndView.setViewName("adminDashboard/user");
return modelAndView;
}
提前致谢。
答案 0 :(得分:1)
正如我在你的案例评论中提到的,你在这里只有一个POJO实例而不是bean。所以这里没有豆子可以自动装配。
相反,您可以添加自定义约束并为Validator提供注入的bean。
@Target({ElementType.METHOD, ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy=MyConstraintValidator.class)
public @interface MyConstraint {
}
可以注入bean的验证器
import javax.validation.ConstraintValidator;
public class MyConstraintValidator implements ConstraintValidator {
@Autowired;
private UserRepository userRepository;
...
}
从链接中获取代码