Friday, 15 June 2012

spring - Validating @RequestParam using @Annotations -



spring - Validating @RequestParam using @Annotations -

i'm trying validate @requestparam of parameter. here's controller:

@requestmapping(value = "/addcategory", method = requestmethod.post) public modelandview addcategory(@validcategoryname @requestparam(value = "categoryname") string categoryname) { modelandview modelandview = new modelandview(); boolean addedsuccessfully = sideviewcontrollerdelegate.addmenucategory(categoryname); modelandview.setviewname("home_partial/side_view/category_completed"); if (addedsuccessfully) { modelandview.addobject("responsemessage", "added"); } homecoming modelandview; }

and @validcaegoryname defined this:

@target({method, field, annotation_type, parameter}) @retention(runtime) @documented @notnull @constraint(validatedby = validcategoryimpl.class) public @interface validcategoryname { string message() default "this category not seem allowed"; class<?>[] groups() default { }; class<? extends payload>[] payload() default { }; int min() default 30; }

and impl class this:

public class validcategoryimpl implements constraintvalidator<validcategoryname, string> { @autowired menucategoriesservice menucategoriesservice; private int min; @override public void initialize(validcategoryname constraintannotation){ min = constraintannotation.min(); } @override public boolean isvalid(string categoryname, constraintvalidatorcontext context){ homecoming categoryname.length() < min && menucategoriesservice.containscategoryname(categoryname); } }

am missing anything?

thanks!

spring spring-mvc annotations

No comments:

Post a Comment