with spring 3 annotations, jsr303 getting Neither

2019-05-08 22:22发布

问题:

Implementing a simple jsr303 validation using spring 3 with annotations and I am getting the following result when the post finds missing elements:

java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'dataForm' available as request attribute

The controller looks like so:

@Controller
@SessionAttributes
@RequestMapping( "/dataentry.html" )
public class EnterDataController
{
  @RequestMapping( method = RequestMethod.GET )
  public String enterNewDataHandler( Map model )
  {
    DataEntryForm dataForm = new DataEntryForm();
    model.put( "dataForm", dataForm );
    return ( "dataentry" );
  }

  @RequestMapping( method = RequestMethod.POST )
  public String addData( @Valid DataEntryForm dataForm,
                           BindingResult          result,
                           Map                    model )
  {
    if ( result.hasErrors() ) { return ( "dataentry" ); }

    ... code for saving data ...
  }
}

The form looks like so:

public class DataEntryForm
{
  @NotNull
  private Double dataAmount;

  @NotNull
  @Temporal(value=TemporalType.TIMESTAMP)
  private Date dataDate;

  ... sets and gets ...
}

The jsp form section looks like so:

<form:form method="post"
           commandName="dataForm"
           action="dataentry.html">
  <table>
  <tr>
    <td><form:label path=dataAmount">Data</form:label></td>
    <td><form:input path="dataAmount"/></td>
    <td><div class="error"><form:errors path="dataAmount"/></div></td>
  </tr>
  <tr>
    <td><form:label path="dataDate">Date</form:label></td>
    <td><form:input path="dataDate"/></td>
    <td><div class="error"><form:errors path="dataDate"/></div></td>
  </tr>
  <tr>
    <td colspan="2"><input type="submit" value="Add Data"/></td>
  </tr>
</table>
</form:form>

I am implementing a standard spring return type of String for the logical view name. Here is the full stack trace:

java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'dataForm' 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.LabelTag.autogenerateFor(LabelTag.java:129)
org.springframework.web.servlet.tags.form.LabelTag.resolveFor(LabelTag.java:119)
org.springframework.web.servlet.tags.form.LabelTag.writeTagContent(LabelTag.java:89)
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.views.dataentry_jsp._jspx_meth_form_005flabel_005f0(dataentry_jsp.java:287)
org.apache.jsp.WEB_002dINF.views.dataentry_jsp._jspService(dataentry_jsp.java:114)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:419)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:391)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
org.apache.jasper.runtime.JspRuntimeLibrary.include(JspRuntimeLibrary.java:927)
org.apache.jasper.runtime.PageContextImpl.doInclude(PageContextImpl.java:684)
org.apache.jasper.runtime.PageContextImpl.include(PageContextImpl.java:678)
org.apache.tiles.jsp.context.JspTilesRequestContext.include(JspTilesRequestContext.java:103)
org.apache.tiles.jsp.context.JspTilesRequestContext.dispatch(JspTilesRequestContext.java:96)
org.apache.tiles.renderer.impl.TemplateAttributeRenderer.write(TemplateAttributeRenderer.java:44)
org.apache.tiles.renderer.impl.AbstractBaseAttributeRenderer.render(AbstractBaseAttributeRenderer.java:106)
org.apache.tiles.renderer.impl.ChainedDelegateAttributeRenderer.write(ChainedDelegateAttributeRenderer.java:76)
org.apache.tiles.renderer.impl.AbstractBaseAttributeRenderer.render(AbstractBaseAttributeRenderer.java:106)
org.apache.tiles.impl.BasicTilesContainer.render(BasicTilesContainer.java:670)
org.apache.tiles.impl.BasicTilesContainer.render(BasicTilesContainer.java:336)
org.apache.tiles.template.InsertAttributeModel.renderAttribute(InsertAttributeModel.java:210)
org.apache.tiles.template.InsertAttributeModel.end(InsertAttributeModel.java:126)
org.apache.tiles.jsp.taglib.InsertAttributeTag.doTag(InsertAttributeTag.java:311)
org.apache.jsp.WEB_002dINF.views.layout_jsp._jspx_meth_tiles_005finsertAttribute_005f3(layout_jsp.java:287)
org.apache.jsp.WEB_002dINF.views.layout_jsp._jspService(layout_jsp.java:128)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:419)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:391)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
org.apache.tiles.servlet.context.ServletTilesRequestContext.forward(ServletTilesRequestContext.java:241)
org.apache.tiles.servlet.context.ServletTilesRequestContext.dispatch(ServletTilesRequestContext.java:222)
org.apache.tiles.renderer.impl.TemplateAttributeRenderer.write(TemplateAttributeRenderer.java:44)
org.apache.tiles.renderer.impl.AbstractBaseAttributeRenderer.render(AbstractBaseAttributeRenderer.java:106)
org.apache.tiles.impl.BasicTilesContainer.render(BasicTilesContainer.java:670)
org.apache.tiles.impl.BasicTilesContainer.render(BasicTilesContainer.java:690)
org.apache.tiles.impl.BasicTilesContainer.render(BasicTilesContainer.java:644)
org.apache.tiles.impl.BasicTilesContainer.render(BasicTilesContainer.java:627)
org.apache.tiles.impl.BasicTilesContainer.render(BasicTilesContainer.java:321)
org.springframework.web.servlet.view.tiles2.TilesView.renderMergedOutputModel(TilesView.java:124)
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.doPost(FrameworkServlet.java:560)
javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)

Any suggestions? And, is there any docs that describe in detail what happens under the hood with this?

Thanks

回答1:

I have had this problem before. I believe that in addition to using @Valid you must also use @ModelAttribute("dataForm") so it knows how to map your form to your DataEntryForm object.

  @RequestMapping( method = RequestMethod.POST )
  public String addData( @ModelAttribute("dataForm") @Valid DataEntryForm dataForm,
                       BindingResult          result,
                       Map                    model )
  {
      if ( result.hasErrors() ) { return ( "dataentry" ); }

      ... code for saving data ...
  }

It looks a little ugly. I kind of wish that the two annotations could be combined somehow.