Announcement Announcement Module
No announcement yet.
Binding data with spring annotations Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Binding data with spring annotations

    I'm new to spring and I'm asked to do a project with spring annotations.
    I have a Form and I'm not able to binds its data. It's been a while now, that I'm looking on the internet but none of the answers I've found were applicable or worked.
    Here are the relevant lines of my code


    <form:form method="post" action="" enctype="multipart/form-data" modelAttribute="myForm">
              <input type="file" name="file"/>    
               enabledBarcodeDecoding<form:checkbox path="myBool"/>
               <input type="submit"/>

    @RequestMapping("/" )
    public class MyController {
     @RequestMapping(method = RequestMethod.POST)
     public void onSubmit(
       @ModelAttribute("myForm" ) MyForm myForm,
       HttpServletResponse response) {
      //code using myForm.file & myForm.myBool
      //I return void because in this controller I use the HTTPServletResponse to download a file given by a web service
     public void initBinder(WebDataBinder binder) {
      // to actually be able to convert Multipart instance to byte[]
      // we have to register a custom editor
        new ByteArrayMultipartFileEditor());
      // now Spring knows how to handle multipart object and convert them
     @ModelAttribute("readerExtensionForm" )
     public ReaderExtensionForm getReaderExtensionForm() {
      return new ReaderExtensionForm();


    public class MyForm {
     private byte[] file;
     private boolean myBool=true;
     public MyForm() {
     public boolean isMyBool() {
      return myBool;
     public void setMyBool(boolean myBool) {
      this.myBool = myBool;
     public void setFile(byte[] file) {
             this.file = file;
     public byte[] getFile() {
             return file;

    2009-06-05 11:14:25,113 ERROR org.springframework.web.servlet.tags.form.CheckboxTag - Neither BindingResult nor plain target object for bean name 'myForm' available as request attribute
    java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'myForm' available as request attribute
     at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getBindStatus(
     at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getPropertyPath(
     at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getName(
     at org.springframework.web.servlet.tags.form.AbstractCheckedElementTag.autogenerateId(
     at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.resolveId(
     at org.springframework.web.servlet.tags.form.AbstractSingleCheckedElementTag.writeTagContent(
     at org.springframework.web.servlet.tags.form.CheckboxTag.writeTagContent(
     at org.springframework.web.servlet.tags.form.AbstractFormTag.doStartTagInternal(
     at org.springframework.web.servlet.tags.RequestContextAwareTag.doStartTag(
     at org.apache.jsp.fileUpload_jsp._jspx_meth_form_005fcheckbox_005f0(
     at org.apache.jsp.fileUpload_jsp._jspx_meth_form_005fform_005f0(
     at org.apache.jsp.fileUpload_jsp._jspService(
     at org.apache.jasper.runtime.HttpJspBase.service(
     at javax.servlet.http.HttpServlet.service(
     at org.apache.jasper.servlet.JspServletWrapper.service(
     at org.apache.jasper.servlet.JspServlet.serviceJspFile(
     at org.apache.jasper.servlet.JspServlet.service(
     at javax.servlet.http.HttpServlet.service(
     at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(
     at org.apache.catalina.core.ApplicationFilterChain.doFilter(
     at org.apache.catalina.core.StandardWrapperValve.invoke(
     at org.apache.catalina.core.StandardContextValve.invoke(
     at org.apache.catalina.core.StandardHostValve.invoke(
     at org.apache.catalina.valves.ErrorReportValve.invoke(
     at org.apache.catalina.core.StandardEngineValve.invoke(
     at org.apache.catalina.connector.CoyoteAdapter.service(
     at org.apache.coyote.http11.Http11Processor.process(
     at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(
     at Source)

    /************************************************** ***************/

    It's been a while now, and I don't know what else to do. It might be a really stupid error but I don't know spring enough to find it, and reading & re-reading the manual did not help me.
    Thanks for helping me !!!
    Last edited by Toinou87; Jun 8th, 2009, 03:24 AM.

  • #2
    Hi ,

    Could you post you servlet-xml file. You need to specify the codebas for your beans in the servlet-xml



    • #3
      I have solved my problem by using <input type="checkbox" instead of <spring:checkbox.

      Nonetheless I m interested in your answer as I'll probably have to use other spring forms

      <?xml version="1.0" encoding="UTF-8"?>
      <beans xmlns=""
      	xmlns:xsi="" xmlns:context=""
          <context:component-scan base-package="my.package" />
          <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>
          <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
      	<bean id="viewResolver"
      		<property name="order" value="1" />
      		<property name="prefix" value="/WEB-INF/views/" />
      		<property name="suffix" value=".jsp" />
        <bean id="messageSource" class="">
          <property name="basename" value="classpath:ApplicationResources"/>
      	<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
          <!-- one of the properties available; the maximum file size in bytes -->
          <property name="maxUploadSize" value="1000000"/>


      • #4
        Just one question, where is your GET method, displaying the form ?
        It's in this method you should prepare your backing object.
        This exception ususally happens when no backing object has been prepared for binding.


        • #5
          I don't understand what you call "GET method, displaying the form"

          if thats the GET method that sends me to the jsp that contains the form it's

          public class FileUpload {
          	@RequestMapping(method = RequestMethod.GET)
          	public void onSubmit(){
          and with my view resolver it goes to /WEB-INF/views/fileUpload.jsp which contains the form.

          Is that what you ask for?


          • #6
            Yes. With spring, any form submit with automatic binding is a two phases process :

            - User comes with a GET request => You have to prepare your backing object for future binding, then display the form
            - User comes with a POST request => Automatic binding occurs, then the populated backing object is available for validation (if you want to). If any error occurs during binding or validation, usually you use the same view as in step 1 to let the user correct his errors. If no error occurs, you do whatever you want with the backing object and redirect the user to another page.

            GET and POST requests should come at the same URL.
            Here is a complete example :

            public class MyController {
              @RequestMapping(method = RequestMethod.GET)
              public String showForm(ModelMap model) {
                // Prepare backing object
                model.addAttribute("command", new MyForm());
                // Show form view
                return "form-view";
              @RequestMapping(method = RequestMethod.POST)
              public String onSubmit(@ModelAttribute("command") MyForm myForm, BindingResult result) {
                  // Validate the form backing object, optional
                  validator.validate(myForm, result);
                  if(result.hasError()) {
                    // There are errors, going back to form
                    return "form-view";
                  // Do business with form backing object
                  // Redirect user to another page (redirect after POST)
                  return "redirect:./another-page.html";
            Hope this makes you things clearer.


            • #7
              Indeed it's clearer (I think)!

              So: the fact that I have 2 controllers:
              1 to go to the jsp
              1 to use the form
              is not good

              it would be better if I use your example to have:
              1 controller with 2 mappings (for example : by changing the action name in my form and in the controller to "/fileUpload.htm")

              Right now I'm working on other stuffs (as I had found a (bad) solution) but I shall come back on this soon and I'll post here again if I get any problem.

              Whatever thanks a lot for your explanations, it makes me understand a bit more Spring and MVC ^^