Spring BindingResult错误

时间:2010-11-08 17:07:14

标签: java spring spring-mvc

我收到以下错误消息:

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

我的JSP看起来像:

    <form:form commandName="billingInfoCommand" method="post" 
onsubmit="return checkVals()">

我的控制器如下:

public class BillingInfoController extends FusionFormController {
    protected final Log logger = LogFactory.getLog(getClass());
    private FieldAccessService fieldAccessService;

        public BillingInfoController(){
        setCommandClass(CommonCommand.class);
        setCommandName("billingInfoCommand");
    }

   protected Map referenceData(HttpServletRequest request,Object command, Errors errors) throws Exception {

        CommonCommand commonCommand=(CommonCommand)command;
        HashMap<String,Object> refDataMap=new HashMap<String,Object>();
        refDataMap.put("billingTypes",this.getLookupValsAsMap("getBillingTypes"));
        Long stageId=commonCommand.getEmeaMain().getStageId();
        refDataMap.put("requiredFields",requiredFields);
        return refDataMap;
    }


  public Object formBackingObject(HttpServletRequest request){
        BillingInfo billingInfoObj=null;
        Long cepWorkItemNo=null;
        CommonCommand billingInfoCommand=new CommonCommand();
        try{
         cepWorkItemNo=new Long(request.getParameter("cep_work_item_no"));
        }catch(NumberFormatException e){
        }
        if(cepWorkItemNo==null){
           billingInfoObj=new BillingInfo();
        }else{
          billingInfoObj=(BillingInfo)domainService.getDomainObject(BillingInfo.class,cepWorkItemNo);
        }
        if(billingInfoCommand.getBillingInfo()==null)
           billingInfoCommand.setBillingInfo(billingInfoObj);
        if(billingInfoCommand.getEmeaMain()==null)
            billingInfoCommand.setEmeaMain(emeaMain);
        return billingInfoCommand;
   }
}

我的Pojo课程如下:

public class BillingInfo {

    private Integer billingId;

    public Integer getBillingId() {
        return billingId;
    }
    public void setBillingId(Integer billingId) {
        this.billingId = billingId;
    }
}

注意

我这里只发布了相关的代码片段。我不确定为什么我会提到关于IllegalStateException的错误,一些有用的指针将不胜感激。

此外,获取Neither BindingResult nor plain target object for bean name x available as request attribute错误的正常原因是什么?

完整堆栈跟踪

 - java.lang.IllegalStateException:
   Neither BindingResult nor plain
   target object for bean name
   'billingInfoCommand' available as
   request attribute
       java.lang.IllegalStateException: Neither BindingResult nor plain
   target object for bean name
   'billingInfoCommand' available as
   request attribute at
   org.springframework.web.servlet.support.BindStatus.(BindStatus.java:148)
   at
   org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getBindStatus(AbstractDataBoundFormElementTag.java:179)
   at
   org.springframework.web.servlet.tags.form.AbstractHtmlElementTag.resolveCssClass(AbstractHtmlElementTag.java:404)
   at
   org.springframework.web.servlet.tags.form.AbstractHtmlElementTag.writeDefaultAttributes(AbstractHtmlElementTag.java:380)
   at
   org.springframework.web.servlet.tags.form.LabelTag.writeTagContent(LabelTag.java:89)
   at
   org.springframework.web.servlet.tags.form.AbstractFormTag.doStartTagInternal(AbstractFormTag.java:90)
   at
   org.springframework.web.servlet.tags.RequestContextAwareTag.doStartTag(RequestContextAwareTag.java:77)
   at
   org.apache.jsp.WEB_002dINF.jsp.globalecep.ap.billing_005finfo_jsp._jspx_meth_form_005flabel_005f0(billing_005finfo_jsp.java:1243)
   at
   org.apache.jsp.WEB_002dINF.jsp.globalecep.ap.billing_005finfo_jsp._jspService(billing_005finfo_jsp.java:363)
   at
   org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:98)
   at
   javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
   at
   org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:369)
   at
   org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:308)
   at
   org.apache.jasper.servlet.JspServlet.service(JspServlet.java:259)
   at
   javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
   at
   org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:269)
   at
   org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
   at
   org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:659)
   at
   org.apache.catalina.core.ApplicationDispatcher.doInclude(ApplicationDispatcher.java:565)
   at
   org.apache.catalina.core.ApplicationDispatcher.include(ApplicationDispatcher.java:493)
   at
   org.apache.jasper.runtime.JspRuntimeLibrary.include(JspRuntimeLibrary.java:965)
   at
   org.apache.jasper.runtime.PageContextImpl.include(PageContextImpl.java:600)
   at
   org.apache.struts.tiles.TilesUtilImpl.doInclude(TilesUtilImpl.java:137)
   at
   org.apache.struts.tiles.TilesUtil.doInclude(TilesUtil.java:177)
   at
   org.apache.struts.taglib.tiles.InsertTag.doInclude(InsertTag.java:756)
   at
   org.apache.struts.taglib.tiles.InsertTag$InsertHandler.doEndTag(InsertTag.java:881)
   at
   org.apache.struts.taglib.tiles.InsertTag.doEndTag(InsertTag.java:473)
   at
   org.apache.jsp.WEB_002dINF.fusion.jsp.tab_005ftemplate_jsp._jspx_meth_tiles_005finsert_005f2(tab_005ftemplate_jsp.java:1807)
   at
   org.apache.jsp.WEB_002dINF.fusion.jsp.tab_005ftemplate_jsp._jspService(tab_005ftemplate_jsp.java:307)
   at
   org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:98)
   at
   javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
   at
   org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:369)
   at
   org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:308)
   at
   org.apache.jasper.servlet.JspServlet.service(JspServlet.java:259)
   at
   javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
   at
   org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:269)
   at
   org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
   at
   org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:659)
   at
   org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:457)
   at
   org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:395)
   at
   org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:311)
   at
   org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:145)
   at
   org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:251)
   at
   org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1144)
   at
   org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:880)
   at
   org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:793)
   at
   org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:476)
   at
   org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:431)
   at
   javax.servlet.http.HttpServlet.service(HttpServlet.java:627)
   at
   javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
   at
   org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:269)
   at
   org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
   at
   org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
   at
   org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:172)
   at
   org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
   at
   org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117)
   at
   org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:108)
   at
   org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:174)
   at
   org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:879)
   at
   org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:665)
   at
   org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:528)
   at
   org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:81)
   at
   org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:689)
   at
   java.lang.Thread.run(Thread.java:595)

Servlet配置映射文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                           http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
                           http://www.springframework.org/schema/aop
                           http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">


    <bean id="apbillingInfoController" class="ap.BillingInfoController">
      <property name="queryService" ref="queryService" />
      <property name="domainService" ref="domainService" />
      <property name="formView" value="ap_billinginfo"/>
      <property name="validator" ref="apbillingInfoValidator" />
      <property name="fieldAccessService"  ref="fieldAccessService"/>
    </bean>

    <bean id="apbillingInfoValidator"  class="validator.ap.BillingInfoValidator">
           <property name="queryService" ref="queryService" />
    </bean>

    <bean id="apcompanyBillingInfoController" class="ap.company.COMPANYBillingInfoController">
      <property name="commandName"   value="companyBillingInfoBean"/>
      <property name="commandClass"  value="command.ap.company.COMPANYCommonCommand"/>
      <property name="queryService" ref="queryService" />
      <property name="domainService" ref="domainService" />
      <property name="formView" value="ap_companybillinginfo"/>
      <property name="validator" ref="apcompanyBillingInfoValidator" />
      <property name="fieldAccessService"  ref="fieldAccessService"/>
    </bean>

    <bean id="apcompanyBillingInfoValidator"  class="validator.ap.company.COMPANYBillingInfoValidator">
        <property name="fieldAccessService"  ref="fieldAccessService"/>
       <property name="queryService" ref="queryService" />
    </bean>
      <property name="mappings">
        <props>
          <prop key="/ap_billing.htm">apbillingInfoController</prop>
        </props>
      </property>
    </bean>

      <property name="mappings">
        <props>
          <prop key="/company_ap_billing.htm">apcompanyBillingInfoController</prop>
        </props>
      </property>
    </bean>
</beans>

感谢。

3 个答案:

答案 0 :(得分:2)

渲染jsp时会出现错误。我假设你有一个可能带有标签的jsp(spring:bind会导致同样的问题)

<form:form modelAttribute="billingInfoCommand">
...
</form:form>

当它呈现jsp时,它会在您的请求范围中查找名为billingInfoCommand的bean,并使其可用于jstl表达式(以及其他内容)。当请求范围中没有对象时,抛出您看到的异常。

编辑:

很高兴看到你没有从删除所有代码中得到任何错误。接下来,请尝试直接查看请求范围。

<div>The request scope is: ${requestScope}</div>
<div>The command is: ${requestScope.billingInfoCommand}</div>

如果您看到类似

的内容

请求范围是:javax.servlet.jsp.el.ImplicitObjectELResolver ......
   命令是:

然后你的bean实际上不在请求范围内。接下来要看的是支持对象放在请求范围中的位置。首先想到的是formBackingObject方法可能与将其放入请求有关。

答案 1 :(得分:0)

这似乎是错误的控制器/视图接线。我这样说是因为我在堆栈跟踪中看不到对BilingInfoController的引用。至少应该有一个对handleRequest / Internal的控制器的引用来加载formBackingObject。

答案 2 :(得分:0)

确保使用的是在请求中设置绑定对象或Display对象  

这应该在使用Binding Object之前或任何include jsp标记之前放置 如下