3
votes

I am trying to validate an input field that is supposed to contain (if not empty) a natural number (i.e. a positive non-zero integer: 1, 2, 3, ....)

I am using the following annotations:

@Digits(integer=10, fraction=0)
@Min(value = 1)
private Long number;

(Is this the best way to describe my constraint???)

When I submit a number such as 1.5 I get a VALIDATION MESSAGE which is good. However when I submit an input such as -1 I do not get any VALIDATION MESSAGE. What do I miss?

Thanks!

P.S. Since my (other) Hibernate annotations for this field were on the getter of the field, I just move these two annotations to the getter as well (instead of being on the actual field). Did not help.

EDIT

I just read I might need to add <mvc:annotation-driven /> to my XML. I did it, however while starting the server I am getting the exception:

org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Unable to locate Spring NamespaceHandler for XML schema namespace [http://www.springframework.org/schema/mvc] Offending resource: ServletContext resource [/WEB-INF/dispatcher-servlet.xml]

I am not sure what it means and if I actually need (????) that annotation-driven tag... also, if I needed that annotation-driven tag in my xml, why did my other annotations (including one of the validation annotations) work without it?

3

3 Answers

3
votes

You do need the <mvc:annotation-driven /> tag. That's what tells Spring Web MVC to activate JSR-303 validation.

A few things to check:

1) Make sure you declare the mvc namespace prefix in your dispatcher-servlet.xml config. For example (using Spring 3 here):

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
    ...
</beans>

2) Also make sure you have the Spring Web MVC JAR on your classpath. The JAR contains the namespace handler, which is the thing that knows how to process MVC configuration namespace.

3) Be sure that Hibernate Validator is on the classpath. Spring needs it in order to perform the validation.

As to your question of why your other annotations worked: <mvc:annotation-driven> isn't responsible for all annotations, just some of them. One thing it is responsible for is JSR-303 validations, as noted above.

Regarding why your other validation worked, my guess is that it wasn't a validation that worked at all. Instead, when you try to put 1.5 into a Long, Spring can't do it, and you're probably getting an error message of some sort. I don't think you need the @Digits annotation here at all. If you remove it, I'm guessing you'll still get the message.

1
votes

You have to additionally explicitly specify Spring to validate your bean at the point of binding using @Valid annotation, can you confirm you are doing this. I am assuming the error that you are getting with 1.5 as the input is simply because 1.5 cannot be put into a Long field and is a binding exception, not a validation exception.

0
votes

If its an integer, why not make it an Integer? Also, it should be @Min(1).