My ultimate goal is to have a model with annotations and do validations on form inputs. I have tried that with writing some validators but it didn't work (The form would not even show fields where I could enter sp there was nothing to test the validation on), so I was trying it from the start in little steps. As a first step I just want to make the model binding work before I start any sort of validation.
As a simple try, I used the mailItemModel form http://www.javabeat.net/introduction-to-spring-web-flow-swf/
I have added the following line to the servlet-config.xml, which is linked in the web.xml:
<mvc:annotation-driven />
<context:annotation-config />
<context:component-scan base-package="com.mypackage.kumo.**" />
I had that from another tutorial and I kind of thought this would make is scan the whole package and I would not need to specify all the models in variables explicitly. I get the titular error. I have also tried adding "@Component" to the model class. But that didn't change anything.
Into the flow XML I then tried adding
<var name="MailItemModel" />
as from the above linked tutorial. I got the error, that element var" requires a "class" attribute. So I make it this instead:
<var name="MailItemModel" class="com.mypackage.kumo.model.MailItemModel" />
but that just trows the very same error as before.
My flow looks like this now:
<?xml version="1.0" encoding="UTF-8"?>
<flow xmlns="http://www.springframework.org/schema/webflow"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:ns0="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/webflow
http://www.springframework.org/schema/webflow/spring-webflow-2.0.xsd"
start-state="Start">
<var name="MailItemModel" class="com.mypackage.kumo.model.MailItemModel" />
<view-state id="Start" view="/Search" model="MailItemModel">
<transition on="entered" to="temp" />
<transition on="cancel" to="SubEnde" />
</view-state>
<view-state id="temp" view="/End" />
<view-state id="SubEnde" view="/Start" />
</flow>
Search.jsp (Form excerpt)
<form:form method = "post" modelAttribute = "mailItemModel">
Mail Username: <form:input path="username" />
<br />
<input type="submit" name="_eventId_entered" value="Entered" />
<input type="submit" name="_eventId_cancel" value="Cancel" />
</form:form>
Start.jsp and End.jsp are just static jsps with some lorem ipsum content.
The Stacktrace:
root cause
java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'mailItemModel' available as request attribute
org.springframework.web.servlet.support.BindStatus.<init>(BindStatus.java:144)
org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getBindStatus(AbstractDataBoundFormElementTag.java:168)
org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getPropertyPath(AbstractDataBoundFormElementTag.java:188)
org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getName(AbstractDataBoundFormElementTag.java:154)
org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.autogenerateId(AbstractDataBoundFormElementTag.java:141)
org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.resolveId(AbstractDataBoundFormElementTag.java:132)
org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.writeDefaultAttributes(AbstractDataBoundFormElementTag.java:116)
org.springframework.web.servlet.tags.form.AbstractHtmlElementTag.writeDefaultAttributes(AbstractHtmlElementTag.java:422)
org.springframework.web.servlet.tags.form.InputTag.writeTagContent(InputTag.java:142)
org.springframework.web.servlet.tags.form.AbstractFormTag.doStartTagInternal(AbstractFormTag.java:84)
org.springframework.web.servlet.tags.RequestContextAwareTag.doStartTag(RequestContextAwareTag.java:80)
org.apache.jsp.WEB_002dINF.jsp.Search_jsp._jspx_meth_form_005finput_005f0(Search_jsp.java:559)
org.apache.jsp.WEB_002dINF.jsp.Search_jsp._jspx_meth_form_005fform_005f0(Search_jsp.java:522)
org.apache.jsp.WEB_002dINF.jsp.Search_jsp._jspx_meth_dsx_005fcontent_002dbox_005f0(Search_jsp.java:478)
org.apache.jsp.WEB_002dINF.jsp.Search_jsp._jspx_meth_dsx_005fcontent_005f0(Search_jsp.java:439)
org.apache.jsp.WEB_002dINF.jsp.Search_jsp._jspx_meth_dsx_005fhtml_002dbody_005f0(Search_jsp.java:375)
org.apache.jsp.WEB_002dINF.jsp.Search_jsp._jspx_meth_dsx_005fhtml_005f0(Search_jsp.java:293)
org.apache.jsp.WEB_002dINF.jsp.Search_jsp._jspService(Search_jsp.java:126)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:209)
org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:267)
org.springframework.webflow.mvc.servlet.ServletMvcView.doRender(ServletMvcView.java:55)
org.springframework.webflow.mvc.view.AbstractMvcView.render(AbstractMvcView.java:187)
org.springframework.webflow.engine.ViewState.render(ViewState.java:296)
org.springframework.webflow.engine.ViewState.refresh(ViewState.java:243)
org.springframework.webflow.engine.ViewState.resume(ViewState.java:221)
org.springframework.webflow.engine.Flow.resume(Flow.java:545)
org.springframework.webflow.engine.impl.FlowExecutionImpl.resume(FlowExecutionImpl.java:258)
org.springframework.webflow.executor.FlowExecutorImpl.resumeExecution(FlowExecutorImpl.java:169)
org.springframework.webflow.mvc.servlet.FlowHandlerAdapter.handle(FlowHandlerAdapter.java:183)
org.springframework.webflow.mvc.servlet.FlowController.handleRequest(FlowController.java:174)
org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.handle(SimpleControllerHandlerAdapter.java:50)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:938)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:870)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:852)
javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
What am I doing wrong? I have looked into other posts of the same error, but they all use @requestmapping to an URL in a controller, but since I am using Webflow, I don't (and can't it would seem) use controllers, so it didn't really help.
There are 2 things flawed in your configuration
In your configuration you named your model object MailItemModel
(notice the M)
<var name="MailItemModel" class="com.mypackage.kumo.model.MailItemModel" />
In your view it is named mailItemModel
(notice the m).
<form:form method = "post" modelAttribute = "mailItemModel">
Either change it in your flow configuration to mailItemModel
or in your view use MailItemModel
the names have to match, including the casing.
Regarding your configuration.
<context:component-scan base-package="com.mypackage.kumo.**" />
The base-package
property is just that the name(s) of the base packages to scan. It doesn't take an ant-style expression. Remove the ending .**
.
<context:component-scan base-package="com.mypackage.kumo" />
A final note the use of <context:component-scan … />
already implies <context:annotation-config />
so you can remove that line of configuration.