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{

    public boolean supports(Class<?> clazz) {
         return ReportsCommand.class.isAssignableFrom(clazz);
    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.

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

        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...


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


Instead of


you need


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

public class MyController{
      private MyValidator myValidator;

      public void initBinder(WebDataBinder binder) {

      @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?


I've read through the FAQ of Android Dev Guid ( 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...