Spring MVC 3.0: How to efficiently check a path variable that is global for all query mappings?

I'm trying to get my feet wet with Spring MVC 3.0, and while I can get it working, I cannot deal with this particular scenario effectively.

I have a controller with this descriptor "/ {studyName} / module", and it looks something like this: -

@Controller @RequestMapping(value = "/{studyName}/module") public class ModuleController { @RequestMapping(...) public ModelAndView getA(@PathVariable String studyName, ...) { if (!validStudy(studyName)) { return bad request; } ... } @RequestMapping(...) public ModelAndView getB(@PathVariable String studyName, ...) { if (!validStudy(studyName)) { return bad request; } ... } @RequestMapping(...) public ModelAndView getC(@PathVariable String studyName, ...) { if (!validStudy(studyName)) { return bad request; } ... } @RequestMapping(...) public ModelAndView getD(@PathVariable String studyName, ...) { if (!validStudy(studyName)) { return bad request; } ... } } 

The problem with this code is that I have a studyName check scattered across all methods and possibly other controller methods. Is there a way by which I can validate on the pathnameName variable in one place without using something like AOP? How do you handle validation as follows?

Thanks.

+7
source share
5 answers

Right now, it's a little difficult to do this automatically, but it's possible. You should use the Bean Validation Provider (JSR-303), which implements the C application. Currently this is Apache BeanValidation or Hibernate Validator 4.2 (which is in beta).

Add the selected Bean validation implementation to the classpath. This will be the JSR-303 implementation used by Spring MVC.

Secondly, annotate the method parameter with @Valid and any constraint annotations like @NonNull.

It will look something like this:

 public ModelAndView getB(@Valid @NonNull @PathVariable String studyName, ...) { 

That should work. Then you will need to check your Spring errors for any problems.

Alternatively, if you are not using any other Spring parameters, you can register the validator with InitBinder like this:

 @InitBinder public void initBinder(WebDataBinder binder) { binder.setValidator(new StudyNameValidator()); } 
+2
source

Create a StudyName class, then register a WebArgumentResolver for StudyName and check your validation.

  public ModelAndView getA(@PathVariable StudyName studyName){ ... } public class StudyNameResolver implements WebArgumentResolver{ //have resolveArgument method do validation if resolved to a StudyName } 
+1
source

I am starting to use spring 3, and I really like your test solution as follows: public ModelAndView getB (@Valid @NonNull @PathVariable String studyName, ...) {

However, as soon as pathvariable is invalid (in this case studyName = null), how do you catch and display this error?

I tried using the binding result, but it just doesn't work. Also, do you know how to display error on jsp?

thanks

+1
source

Create a simple validation class:

 public class StudyValidator { public boolean validateStudy(String studyName) { //your validate logic here } } 

then enter it in ModuleController :

 class ModuleController { private StudyValidator sv = new StudyValidator(); //use spring injection to populate. boolean validStudy(String studyName) { return sv.validateStudy(studyName); } } 

Simples.

0
source

Hmmm, not sure if this will work, but you can use the @Valid annotation as briefly mentioned in this link to validators.

Good luck

0
source

All Articles