Spring模型验证错误

时间:2015-01-08 10:20:02

标签: java validation spring-mvc

我尝试通过遵循一些示例代码来验证简单模型。我得到了以下例外。

这是模特: -

@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
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)

1 个答案:

答案 0 :(得分:2)

似乎您正在为您的活页夹设置UserValidator而不是PasswordValidator