This forum is now a read-only archive. All commenting, posting, registration services have been turned off. Those needing community support and/or wanting to ask questions should refer to the Tag/Forum map, and to http://spring.io/questions for a curated list of stackoverflow tags that Pivotal engineers, and the community, monitor.

Binding data with spring annotations

Jun 8th, 2009, 04:08 AM

Hi,
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

@Controller
@RequestMapping("/upload.do" )
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
}
@InitBinder
public void initBinder(WebDataBinder binder) {
// to actually be able to convert Multipart instance to byte[]
// we have to register a custom editor
binder.registerCustomEditor(byte[].class,
new ByteArrayMultipartFileEditor());
// now Spring knows how to handle multipart object and convert them
}
@ModelAttribute("readerExtensionForm" )
public ReaderExtensionForm getReaderExtensionForm() {
return new ReaderExtensionForm();
}
}

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.support.BindStatus.<init>(BindStatus.java:141)
at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getBindStatus(AbstractDataBoundFormElementTag.java:172)
at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getPropertyPath(AbstractDataBoundFormElementTag.java:192)
at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getName(AbstractDataBoundFormElementTag.java:158)
at org.springframework.web.servlet.tags.form.AbstractCheckedElementTag.autogenerateId(AbstractCheckedElementTag.java:80)
at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.resolveId(AbstractDataBoundFormElementTag.java:136)
at org.springframework.web.servlet.tags.form.AbstractSingleCheckedElementTag.writeTagContent(AbstractSingleCheckedElementTag.java:81)
at org.springframework.web.servlet.tags.form.CheckboxTag.writeTagContent(CheckboxTag.java:51)
at org.springframework.web.servlet.tags.form.AbstractFormTag.doStartTagInternal(AbstractFormTag.java:90)
at org.springframework.web.servlet.tags.RequestContextAwareTag.doStartTag(RequestContextAwareTag.java:77)
at org.apache.jsp.fileUpload_jsp._jspx_meth_form_005fcheckbox_005f0(fileUpload_jsp.java:190)
at org.apache.jsp.fileUpload_jsp._jspx_meth_form_005fform_005f0(fileUpload_jsp.java:112)
at org.apache.jsp.fileUpload_jsp._jspService(fileUpload_jsp.java:70)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:374)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:342)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:267)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:845)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
at java.lang.Thread.run(Unknown 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 !!!

Comment

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.

Comment

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 :