2011-06-22 56 views
4

實現用彈簧3與註釋簡單jsr303驗證和我得到以下結果無論BindingResult也不平原目標對象時後發現缺失的元素:與彈簧3個註解,jsr303越來越爲bean名稱「的數據形」

java.lang.IllegalStateException:既不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> 

我在執行字符串的邏輯視圖名稱的標準彈簧復位式。這是完整的堆棧跟蹤:

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) 

有什麼建議嗎?而且,是否有任何文檔詳細描述了引擎蓋下會發生什麼?

謝謝

回答

6

我以前有過這個問題。我相信,除了使用@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 ... 
    } 

它看起來有點難看。我希望這兩個註釋可以以某種方式組合。

+0

我之前實現了這個建議,它確實有效,但是我嘗試過的兩個示例應用程序,它們沒有使用@ModelAttribute,運行良好,沒有錯誤,所以這裏有些事情仍然沒有意義。謝謝 – sldahlin

+0

@sldahlin:同意你的看法,控制器中的模式屬性是沒有必要的。但是如果你不使用模型屬性,你必須以某種方式向Spring展示如何解析你的對象。嘗試在窗體標記中添加modelAttribute =「dataAmount」 –

+1

我知道它已經超過4個月了,但這個答案解決了我的問題,謝謝! – Guillaume