Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 765673
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T16:55:26+00:00 2026-05-14T16:55:26+00:00

How to perform Spring validation in MultiActionController ?

  • 0

How to perform Spring validation in MultiActionController?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T16:55:27+00:00Added an answer on May 14, 2026 at 4:55 pm

    Let’s write the following one

    public class Person {
    
        private String name;
        private Integer age;
    
        public Integer getAge() {
            return age;
        }
    
        public void setAge(Integer age) {
            this.age = age;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
    }
    

    And your MultiActionController

    import static org.springframework.validation.ValidationUtils.*;
    
    @Component
    public class PersonController extends MultiActionController {
    
        public PersonController() {
            setMethodNameResolver(new InternalPathMethodNameResolver());
    
            setValidators(new Validator[] {new Validator() {
                public boolean supports(Class clazz) {
                    return clazz.isAssignableFrom(Person.class);
                }
    
                public void validate(Object command, Errors errors) {
                    rejectIfEmpty(errors, "age", "", "Age is required");
                    rejectIfEmptyOrWhitespace(errors, "name", "", "Name is required");
                }
    
            }});
        }
    
        public ModelAndView add(HttpServletRequest request, HttpServletResponse response, Person person) throws Exception {
            // do something (save our Person object, for instance)
    
            return new ModelAndView();
        }
    
    }    
    

    MultiActionController defines a property called validators where you should provide any Validator used by your MultiActionController. Here you can see a piece of code which is responsible for validating your Command object inside MultiActionController

    ServletRequestDataBinder binder = ...
    
    if (this.validators != null) 
        for (int i = 0; i < this.validators.length; i++) {
            if (this.validators[i].supports(command.getClass())) {
        ValidationUtils.invokeValidator(this.validators[i], command, binder.getBindingResult());
            }
        }
    }
    
    /**
      * Notice closeNoCatch method
      */
    binder.closeNoCatch();
    

    closeNoCatch method says

    Treats errors as fatal

    So if your Validator returns any Error, closeNoCatch will throw a ServletRequestBindingException. But, you can catch it inside your MultiActionController method, as follows

    public ModelAndView hanldeBindException(HttpServletRequest request, HttpServletResponse response, ServletRequestBindingException bindingException) {
        // do what you want right here
    
        BindException bindException = (BindException) bindingException.getRootCause();
    
        return new ModelAndView("personValidatorView").addAllObjects(bindException.getModel());
    }
    

    In order to test, let’s do the following one

    @Test
    public void failureValidation() throws Exception {
        MockHttpServletRequest request = new MockHttpServletRequest();
        request.setMethod("POST");
        request.setRequestURI("http://127.0.0.1:8080/myContext/person/add.html");
    
        /**
         * Empty values
         */
        request.addParameter("name", "");
        request.addParameter("age", "");
    
        PersonController personController = new PersonController();
    
        ModelAndView mav = personController.handleRequest(request, new MockHttpServletResponse());
    
        BindingResult bindingResult = (BindingResult) mav.getModel().get(BindingResult.MODEL_KEY_PREFIX + "command");
    
        /**
         * Our Validator rejected 2 Error
         */
        assertTrue(bindingResult.getErrorCount() == 2);
        for (Object object : bindingResult.getAllErrors()) {
            if(object instanceof FieldError) {
                FieldError fieldError = (FieldError) object;
    
                System.out.println(fieldError.getField());
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to perform a validation property. We have a nullable property called: public
I'm trying to perform remote validation on a property of an item within a
I am trying to perform validation on some fields by binding a function to
I want to perform some simple form validation in my controller. Here's an excerpt
I have a registration page and would like to perform some validation (in addition
I'm parsing an XLIFF document using the XDocument class. Does XDocument perform some validation
It seems that org.hibernate.cfg.Configuration object can be used to perform validation programmatically, by calling
Is it possible to have the same servlet perform validation? It seems that one
I perform a lengthy operation (connection test, validation of remote DB tables, etc.) in
In Spring MVC, one can define interceptors that can perform work before and after

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.