Spring MVC + Hibernate Validator + Velocity

I can not get the basic validation working with Spring MVC.

I have the following POJO:

public class Example {

    @NotNull(message="You must enter a value")
    @Size(min=2, max="2", message="You must enter 2 characters")
    private String value;

    public String getValue() { return value; }
    public void setValue(String value) { this.value = value; }

}

My main controller is as follows:

@Controller
@RequestMapping("/value.vm")
public class ValueController {

    @Autowired
    private Validator validator;

    @RequestMapping()
    public ModelAndView display(@Valid @ModelAttribute("example") Example e, BindingResult result) {
        //Details omitted
    }

My speed pattern is equally simple:

<form target="_self" enctype="application/x-www-form-urlencoded" action="/value.vm" method="post">
    #springBind("example")
    #springFormInput("example.value", "")
    #springShowErrors("<br />", "")
</form>

My servlet.xml file includes:

<mvc:annotation-driven />

If I manually verify the submission of an empty form (i.e. validator.validate (e)), I see that my error messages are generated, however they never exist on my BindingResult.

Any ideas why my binding result is not getting these errors?

+3
source share
2 answers

I mistakenly used:

javax.validation.Validator 

When I needed to use

org.springframework.validation.Validator

Switching to this version and changing the verification code to:

validator.validate(e, result);

solved my problem.

+2
source

JSR 303 @Valid , javax.validation.Validator, .

spring, <mvc:annotation-driven/> @EnableWebMvc, Java

Bean API , , Hibernate Validator

0

All Articles