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 6385471
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T02:54:15+00:00 2026-05-25T02:54:15+00:00

I am trying to apply form validation on a MultiActionController (I do know that

  • 0

I am trying to apply form validation on a MultiActionController (I do know that the controller classes are now deprecated).

I’ve found this answer (which got me closer to my target, but not completely):
How to perform Spring validation in MultiActionController?

Ok, so according to the javadoc, an exception handler method is a controller’s method with the parameters (HttpServletRequest request, HttpServletResponse response, ExceptionClass exception).

As far as I can see (correct me if I am wrong), it looks like the work flow is as follows: the spring dispatcher-servlet goes to the requested method of the controller, and if there happens an exception during its execution (e.g. a binding exception due to validation failure), it will go to the exception handler method that its exception parameter matches the occurring exception (if there is such an exception handler method).

But unlike the regular controller’s methods, this exception handler method has no command object parameter. So my question is how do I access in that method, the command object that was sent with the request in which the binding exception (due to a validation error) happened?

For example, when I use validation by annotations, I have in the request handler methods access (as method parameters) to both BindingResult and the command object, so in case of a validation error I can load the returned ModelAndView with the command object data.

However, with my exception handler method (in my MultiActionController), which ends with

BindException bindException = (BindException) bindingException.getRootCause();
return new ModelAndView("myFormView").addAllObjects(bindException.getModel());

— after submitting invalid data, I am getting and exception that my JSP view (“myFormView”) cannot be rendered because the command object cannot be found.

Thanks!

MORE INFO:

My actual request handler method in my controller (SearchBookController) looks like this:

public ModelAndView list(HttpServletRequest request, HttpServletResponse response, Book book) throws Exception {

    ModelMap modelMap = new ModelMap();

    //getting a list of books according to the propertiest of the command object book...
    modelMap.addAttribute("bookList", bookDAO.listBooks(book));

    return new ModelAndView("bookForm", modelMap);
}

I’ve also added the following exception handler method to the controller:

public ModelAndView hanldeBindException(HttpServletRequest request, HttpServletResponse response, ServletRequestBindingException bindingException) {
    // do what you want right here

    //I WOULD LIKE TO ADD HERE THE SUBMITTED BOOK AND THE FETCHED BOOKLIST TO THE ModelAndView, BUT I DO NOT KNOW HOW TO DO IT

    BindException bindException = (BindException) bindingException.getRootCause();
    return new ModelAndView("bookForm").addAllObjects(bindException.getModel());
}

This is how I added my validator to SearchBookController in my servlet-dispatcher.xml:

<bean name="/book/search.htm" class="com.books.web.SearchBookController" p:validators-ref="searchBookValidator" >
    <property name="bookDAO" ref="myBookDAO" />
</bean>

<bean id="searchBookValidator" class="com.books.validator.SearchBookValidator" />

The validator right now just makes sure that the book properties are validated by ValidationUtils.rejectIfEmptyOrWhitespace.

My view (bookForm.jsp) shows both the submit fields and the search results (it re-displays the submitted fields when the result view is rendered). So after the submit, the view should get both the book command object, and the bookList object.

bookForm.jsp looks like this:

    <tr>
        <td>Details :</td>
        <td><form:input path="details" /></td>
        <td><form:errors path="details" cssClass="error"/></td>
    </tr>

(details is one of the fields of Book).

Here is the exception message I am getting when I try to load bookForm.jsp (even before the submit, simply when i try to load the page so I could fill in the form):

(*** When I remove the p:validators-ref="searchBookValidator" from the controller definition in the servlet-dispatcher.xml, the jsp page loads correctly before and after the submit).

HTTP Status 500 - 

--------------------------------------------------------------------------------

type Exception report

message 

description The server encountered an internal error () that prevented it from fulfilling this request.

exception 

org.apache.jasper.JasperException: An exception occurred processing JSP page /WEB-INF/jsp/bookForm.jsp at line 209

206:        --%>
207:        <tr>
208:            <td>Details :</td>
209:            <td><form:input path="details" /></td>
210:            <td><form:errors path="details" cssClass="error"/></td>
211:        </tr>
212:        <tr>


Stacktrace:
    org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:510)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:413)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:238)
    org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:250)
    org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1047)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:817)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:549)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:717)


root cause 

java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'book' available as request attribute
    org.springframework.web.servlet.support.BindStatus.<init>(BindStatus.java:141)
    org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getBindStatus(AbstractDataBoundFormElementTag.java:174)
    org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getPropertyPath(AbstractDataBoundFormElementTag.java:194)
    org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getName(AbstractDataBoundFormElementTag.java:160)
    org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.autogenerateId(AbstractDataBoundFormElementTag.java:147)
    org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.resolveId(AbstractDataBoundFormElementTag.java:138)
    org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.writeDefaultAttributes(AbstractDataBoundFormElementTag.java:122)
    org.springframework.web.servlet.tags.form.AbstractHtmlElementTag.writeDefaultAttributes(AbstractHtmlElementTag.java:408)
    org.springframework.web.servlet.tags.form.InputTag.writeTagContent(InputTag.java:140)
    org.springframework.web.servlet.tags.form.AbstractFormTag.doStartTagInternal(AbstractFormTag.java:102)
    org.springframework.web.servlet.tags.RequestContextAwareTag.doStartTag(RequestContextAwareTag.java:79)
    org.apache.jsp.WEB_002dINF.jsp.bookForm_jsp._jspx_meth_form_005finput_005f0(bookForm_jsp.java:593)
    org.apache.jsp.WEB_002dINF.jsp.bookForm_jsp._jspService(bookForm_jsp.java:326)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:377)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:238)
    org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:250)
    org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1047)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:817)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:549)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

UPDATE:

Following the answer below, I can now access the command object from the exception handler method. I still have a problem though: if I submit data that should have triggered an error message, e.g. through

<tr>
    <td>Details :</td>
    <td><form:input path="details" /></td>
    <td><form:errors path="details" cssClass="error"/></td>
</tr>

in the result view JSP – I do not see that error message.

My validator looks like this:

public class SearchBookValidator implements Validator {

    //......

    @Override
    public void validate(Object target, Errors errors) {
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "details", "details.required");
    }
}

and my messages.properties contains the following line:

details.required=details are required

To help me see what happens, I’ve included the following code in my controller’s hanldeBindException:

    Map mp = bindException.getModel();
    for (Object o : mp.entrySet()) {
        Map.Entry pairs = (Map.Entry)o;
        System.out.println(pairs.getKey() + " = " + pairs.getValue());
    }

When I submit the form with deliberately wrong data (i.e. the details text field is empty), I am getting the following input on the console:

command = com.books.domain.Book@1173447
org.springframework.validation.BindingResult.command = org.springframework.validation.BeanPropertyBindingResult: 1 errors
Field error in object 'command' on field 'details': rejected value []; codes [details.required.command.details,details.required.details,details.required.java.lang.String,details.required]; arguments []; default message [null]

Can you see why I do not get any error message through the <form:errors path="details" cssClass="error"/> ?

  • 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-25T02:54:15+00:00Added an answer on May 25, 2026 at 2:54 am

    Your Stacktrace message

    java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name ‘book’ available as request attribute

    Well, i suppose you have a Spring form as shown below (Notice commandName attribute)

    <form:form commandName="book">
    

    However, when your page is rendered, form Tag looks for any request attribute called book. If Spring does find any request attribute called book, you will see this nice message

    Neither BindingResult nor plain target object for bean name ‘book’ available as request attribute

    In your list method, your command object is not included in the model as we can see

    public ModelAndView list(HttpServletRequest request, HttpServletResponse response, Book book) throws Exception {
        /**
          * Book object has not been added to the model
          */ 
        ModelMap modelMap = new ModelMap();
        modelMap.addAttribute("bookList", bookDAO.listBooks(book);
    
        return new ModelAndView("bookForm", modelMap);
    }
    

    Use instead

    public ModelAndView list(HttpServletRequest request, HttpServletResponse response, Book book) throws Exception {
        return new ModelAndView("bookForm")
               .addAttribute("bookList", bookDAO.listBooks(book))
               .addAttribute(book);
    }
    

    A couple of notes: Your Exception handler will just be called if the thrown exception match the Exception taken as parameter

    // It will be just called when some validation or binding Exception occurs
    // Otherwise, Spring will bypass it
    public ModelAndView hanldeBindException(HttpServletRequest request, HttpServletResponse response, ServletRequestBindingException bindingException) {
        BindException bindException = (BindException) bindingException.getRootCause();
    
        BindingResult bindingResult = (BindingResult) bindException.getModel().get(BindingResult.MODEL_KEY_PREFIX + "book");
    
        /**
          * bindingResult.getTarget() returns submitted Book object
          */
    
        return new ModelAndView("bookForm")
               .addAllObjects(bindException.getModel())
               .addAttribute("bookList", bookDAO.listBooks(bindingResult.getTarget()));
    }
    

    UPDATE

    Have you registered your message source ?

    <!--IT MUST BE CALLED messageSource-->
    <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
        <property name="basenames" value="ValidationMessages"/>
    </bean>
    

    The previous settings uses a ValidationMessages.properties in the root of the classpath. Update according to your properties files

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to apply a method to an existing object which involves using
I have a PHP-based web app that I'm trying to apply Apache's mod_rewrite to.
I'm newbie on this, I'm trying to apply the shake effect to a dialog
I've been trying to figure out how to work this jQuery Form plugin to
I am trying to use a textarea form entry which I have some javascript
I am trying to validate the form input but my validation is not working.
I'm using this jQuery validation plugin and getting the famous: $(#Form).validate is not a
I'm having troubles trying to apply MS JQuery Validation in my forms where I
i have many fields in my form i was trying to apply different css
im trying to make a registration form with ajax validation check. validation check is

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.