Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There are two ways to validate user input: annotations and by inheriting Spring's Validator class. For simple cases, the annotations are nice. If you need complex validations (like cross-field validation, eg. "verify email address" field), or if your model is validated in multiple places in your application with different rules, or if you don't have the ability to modify your model object by placing annotations on it, Spring's inheritance-based Validator is the way to go. I'll show examples of both.</p> <p>The actual validation part is the same regardless of which type of validation you're using:</p> <pre><code>RequestMapping(value="fooPage", method = RequestMethod.POST) public String processSubmit(@Valid @ModelAttribute("foo") Foo foo, BindingResult result, ModelMap m) { if(result.hasErrors()) { return "fooPage"; } ... return "successPage"; } </code></pre> <p>If you are using annotations, your <code>Foo</code> class might look like:</p> <pre><code>public class Foo { @NotNull @Size(min = 1, max = 20) private String name; @NotNull @Min(1) @Max(110) private Integer age; // getters, setters } </code></pre> <p>Annotations above are <code>javax.validation.constraints</code> annotations. You can also use Hibernate's <code>org.hibernate.validator.constraints</code>, but it doesn't look like you are using Hibernate. </p> <p>Alternatively, if you implement Spring's Validator, you would create a class as follows:</p> <pre><code>public class FooValidator implements Validator { @Override public boolean supports(Class&lt;?&gt; clazz) { return Foo.class.equals(clazz); } @Override public void validate(Object target, Errors errors) { Foo foo = (Foo) target; if(foo.getName() == null) { errors.rejectValue("name", "name[emptyMessage]"); } else if(foo.getName().length() &lt; 1 || foo.getName().length() &gt; 20){ errors.rejectValue("name", "name[invalidLength]"); } if(foo.getAge() == null) { errors.rejectValue("age", "age[emptyMessage]"); } else if(foo.getAge() &lt; 1 || foo.getAge() &gt; 110){ errors.rejectValue("age", "age[invalidAge]"); } } } </code></pre> <p>If using the above validator, you also have to bind the validator to the Spring controller (not necessary if using annotations):</p> <pre><code>@InitBinder("foo") protected void initBinder(WebDataBinder binder) { binder.setValidator(new FooValidator()); } </code></pre> <p>Also see <a href="http://static.springsource.org/spring/docs/current/spring-framework-reference/htmlsingle/spring-framework-reference.html#validation-mvc-triggering" rel="nofollow noreferrer">Spring docs</a>.</p> <p>Hope that helps.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload