gpt4 book ai didi

model-view-controller - 使用 spring 3 注释,jsr303 既没有获取 BindingResult 也没有获取 bean 名称 'dataForm' 的普通目标对象

转载 作者:行者123 更新时间:2023-12-04 08:24:57 25 4
gpt4 key购买 nike

使用带有注释的 spring 3 实现简单的 jsr303 验证,当帖子找到丢失的元素时,我得到以下结果:

java.lang.IllegalStateException:Bean 名称“dataForm”的 BindingResult 和普通目标对象都不能用作请求属性

Controller 看起来像这样:

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

我正在为逻辑 View 名称实现标准的 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 ...
}

它看起来有点丑。我有点希望这两个注释能以某种方式结合起来。

关于model-view-controller - 使用 spring 3 注释,jsr303 既没有获取 BindingResult 也没有获取 bean 名称 'dataForm' 的普通目标对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6442956/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com