使用带有注释的spring 3实现一个简单的jsr303验证,当帖子发现缺少的元素时,我得到以下结果:

java.lang.IllegalStateException:BeanResult'dataForm'的BindingResult和普通目标对象都不能用作请求属性

控制器如下所示:

@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 ...
  }
}


该表格如下所示:

public class DataEntryForm
{
  @NotNull
  private Double dataAmount;

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

  ... sets and gets ...
}


jsp表单部分如下所示:

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


我正在为逻辑视图名称实现标准的Spring返回类型String。这是完整的堆栈跟踪:

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)


有什么建议?并且,是否有任何文档详细描述了此背后的情况?

谢谢

最佳答案

我以前有这个问题。我相信,除了使用@Valid之外,还必须使用@ModelAttribute("dataForm"),这样它才能知道如何将表单映射到DataEntryForm对象。

  @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 ...
  }


看起来有点难看。我希望两个注释可以某种方式组合。

08-08 01:31