gpt4 book ai didi

java - Spring模型验证错误

转载 作者:行者123 更新时间:2023-12-02 05:05:26 25 4
gpt4 key购买 nike

我尝试通过遵循一些示例代码来验证一个简单的模型。我遇到了以下异常。

这是模型:-

@Entity
public class Customer{

String password;
String confirmPassword;

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

}

validator 类:-

@Component
public class PasswordValidator implements Validator{

@Override
public boolean supports(Class clazz) {
//just validate the Customer instances
return Customer.class.isAssignableFrom(clazz);
}

@Override
public void validate(Object target, Errors errors) {

ValidationUtils.rejectIfEmptyOrWhitespace(errors, "password",
"required.password", "Field name is required.");

ValidationUtils.rejectIfEmptyOrWhitespace(errors, "confirmPassword",
"required.confirmPassword", "Field name is required.");

Customer cust = (Customer)target;

if(!(cust.getPassword().equals(cust.getConfirmPassword()))){
errors.rejectValue("password", "notmatch.password");
}

}

}

使用的 Controller 类:-

@Controller
public class PasswordController {

@Autowired
private UserValidator userValidator;

@InitBinder
private void initBinder(WebDataBinder binder) {
binder.setValidator(userValidator);
}

@RequestMapping("/password")
public ModelAndView getPssword() {


Customer customer = new Customer();//(Customer)command;
return new ModelAndView("customerForm","customerForm",customer);
}


@RequestMapping(value="/password", method=RequestMethod.POST)
public ModelAndView restPostEditUser(@ModelAttribute @Validated Customer customer,
BindingResult result) {
if (result.hasErrors()){

return new ModelAndView("customerForm");
}

ModelAndView model = new ModelAndView("CustomerSuccess");
model.addObject("customer", customer);;
return model;
}
}

我遇到以下异常:-

HTTP 状态 500 - 请求处理失败;嵌套异常是 java.lang.IllegalStateException: validator 目标无效 [com.fnx.reg.validator.UserValidator@1aeb77d1]:com.fnx.reg.model.Customer@2ca7c795

异常

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.IllegalStateException: Invalid target for Validator [com.fnx.reg.validator.UserValidator@1aeb77d1]: com.fnx.reg.model.Customer@2ca7c795
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:973)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:852)
javax.servlet.http.HttpServlet.service(HttpServlet.java:618)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
root cause

java.lang.IllegalStateException: Invalid target for Validator [com.fnx.reg.validator.UserValidator@1aeb77d1]: com.fnx.reg.model.Customer@2ca7c795
org.springframework.validation.DataBinder.assertValidators(DataBinder.java:495)
org.springframework.validation.DataBinder.setValidator(DataBinder.java:486)
com.fnx.reg.controller.PasswordController.initBinder(PasswordController.java:33)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

最佳答案

似乎您正在为 Binder 设置 UserValidator 而不是 PasswordValidator

关于java - Spring模型验证错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27837723/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com