validating the form using spring annotations

I am new to spring i am using spring 3.0.5 RELEASE.My requirement is i want to validate the fields in a form using spring annotations.The code i am using to do this is as follow. This is the validator class which is implementing the validator interface.

public class FluctuationValidator implements Validator{

    @Override
    public boolean supports(Class<?> clazz) {
         return ReportsCommand.class.isAssignableFrom(clazz);
    }
    @Override
    public void validate(Object command, Errors errors) {
        ReportsCommand reportsCommand = (ReportsCommand) command;
        System.out.println("Inside the validate method of validator class");
        ValidationUtils.rejectIfEmpty(errors, "callType", "PackageNameIsRequired");
    }
    }

Here ReportsCommand is the class which have the data entered in the form. In the controller the code is like below.

@Autowired
    FluctuationValidator fluctuationValidator;
public ModelAndView showForm(@ModelAttribute("reports") ReportsCommand model, ModelAndView modelAndView,BindingResult result,
            HttpServletRequest request, HttpServletResponse response) throws Exception {

        fluctuationValidator.validate(modelAndView,result);
        if (result.hasErrors()) {
           modelAndView = new ModelAndView("report-generate-form");
        return modelAndView;
        } 
       }

In the jsp file i am using

<form:errors path="callType" cssClass="error" />

Any thing wrong in my code.If i use like this i ma getting org.springframework.beans.NotReadablePropertyException: Invalid property 'callType' of bean class [org.springframework.web.servlet.ModelAndView]: Bean property 'callType' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter? Exception.

For the property callType it is not searching in the ReportsCommand class instead it is searching in the spring ModelAndView class.What might be the pbm.Can anyone tell me...

Answers


If you want to use annotations for validations, sounds like you might be interested in JSR-303 validation using @Valid.

See http://static.springsource.org/spring/docs/current/spring-framework-reference/html/validation.html#validation-beanvalidation-overview


Instead of

fluctuationValidator.validate(modelAndView,result);

you need

fluctuationValidator.validate(model,result);

model being ReportsCommand object

The actual object you want to validate is ReportsCommand object if i understood correctly.

You can have your controller validate your bean using @Valid annotation and setting the validator by overriding initBinder method. See below for sample

@Controller    
public class MyController{
      @Autowired
      private MyValidator myValidator;

      @Override
      @InitBinder
      public void initBinder(WebDataBinder binder) {
        binder.setValidator(myValidator);
      }

      @Override
      @RequestMapping(method = RequestMethod.POST)
      public String create(@ModelAttribute("myBean") @Valid MyBean myBean, 
                BindingResult bindingResult,
                Model uiModel) {
       .....
      }
    }

Need Your Help

How to open a new screen with additional parameters?

android

I've read through the FAQ of Android Dev Guid (http://developer.android.com/guide/appendix/faq/commontasks.html#opennewscreen) but I'm not sure how can I open a new screen with additional parameters

Universal Apps workflow

iphone ios xcode ipad ios-universal-app

So far I've only dealed with iPhone Apps and the time has come to make them universal. I was watching the lecture about universal Apps by Paul Hegarty on iTunesU and some questions poped into my he...