【问题标题】:java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'configbean' available as request attributejava.lang.IllegalStateException:Bean 名称“configbean”的 BindingResult 和普通目标对象都不能用作请求属性
【发布时间】:2016-06-23 05:01:29
【问题描述】:

当尝试使用 maven 运行 spring 示例应用程序时,我得到了提到的异常。

这是我的程序

Firstpage1.jsp [出现在网页内容中]

<html>
<head>
<title>Finacle data retriever</title>
</head>
<body>
    <h2>Enter the configuration details</h2>
    <form:form method="POST" action="/formexample/Configgen" commandName="configbean">
        <table>
            <tr>
                <td><form:label path="tnu">Total number of Users</form:label></td>
                <td><form:input path="tnu" /></td>
            </tr>
            <tr>
                <td><form:label path="cnu">Concurrent number of Users</form:label></td>
                <td><form:input path="cnu" /></td>
            </tr>
            <tr>
                <td><form:label path="kat">Keep Alive Timeout</form:label></td>
                <td><form:input path="kat" /></td>
            </tr>
            <tr>
                <td colspan="2"><input type="submit" value="Submit" /></td>
            </tr>
        </table>
    </form:form>
</body>
</html>

result.jsp [Webcontent/WEB-INF/jsp]

<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<html>
<head>
<title>Result page</title>
</head>
<body>
<h2> Result page information</h2>
<table>
<tr>
    <td>Total Number of users</td>
    <td>${congibean.tnc}</td>
</tr>
<tr>
    <td>Concurrent Number of users</td>
    <td>${congibean.cnu}</td>
</tr>
<tr>
    <td>Keep alive timeout</td>
    <td>${congibean.kat}</td>
</tr>
</table>
</body>
</html>


**spring dependencies are present in maven dependencies.**

Web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    id="WebApp_ID" version="2.5">
    <jsp-config>
        <taglib>
            <taglib-uri>/WEB-INF/spring-form.tld</taglib-uri>
            <taglib-location>spring-form.tld</taglib-location>
        </taglib>
    </jsp-config>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/formex-servlet.xml</param-value>
    </context-param>
    <welcome-file-list>
        <welcome-file>Firstpage1.jsp</welcome-file>
    </welcome-file-list>
    <servlet>
        <servlet-name>formex</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>formex</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>


</web-app>

formex-servlet.xml

<?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:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd">

   <context:component-scan base-package="formexamplepack1"></context:component-scan>

   <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
   <property name="prefix" value="/WEB-INF/jsp"/>
   <property name="suffix" value=".jsp"/>
   </bean>
</beans>

formexamplepack1 - 包

configbean.java

package formexamplepack1;

public class configbean {


    private int tuc;
    private int cnu;
    private int kat;

    public int getTuc() {
        return tuc;
    }
    public void setTuc(int tuc) {
        this.tuc = tuc;
    }
    public int getCnu() {
        return cnu;
    }
    public void setCnu(int cnu) {
        this.cnu = cnu;
    }
    public int getKat() {
        return kat;
    }
    public void setKat(int kat) {
        this.kat = kat;
    }

}

ConfigurationController.java

package formexamplepack1;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;



@Controller
@RequestMapping(value = "/Configgen")
public class ConfigurationController {


     @RequestMapping(method=RequestMethod.GET)
       public String showform(ModelMap model) {
                configbean configbean=new configbean();
                model.addAttribute("configbean",configbean);
                return "Firstpage";
            }

    @RequestMapping(method=RequestMethod.POST)
    public String getdetails(@ModelAttribute("configbean")configbean configbean,ModelMap model,BindingResult bindingResult){
        model.addAttribute("tnu", configbean.getTuc());
        model.addAttribute("cnu", configbean.getCnu());
        model.addAttribute("kat",configbean.getKat());
        return "result";

    }




}

帮助我解决以下异常。我已经浏览了所有答案,但无法弄清楚。使用最新的 java 1.8 和 spring jar [4.2.6]

tomcat- 7.0.68

以下是例外情况

org.apache.jasper.JasperException: java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'configbean' available as request attribute
    org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:556)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:472)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)



root cause 
java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'configbean' available as request attribute
    org.springframework.web.servlet.support.BindStatus.<init>(BindStatus.java:144)
    org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getBindStatus(AbstractDataBoundFormElementTag.java:168)
    org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getPropertyPath(AbstractDataBoundFormElementTag.java:188)
    org.springframework.web.servlet.tags.form.LabelTag.autogenerateFor(LabelTag.java:130)
    org.springframework.web.servlet.tags.form.LabelTag.resolveFor(LabelTag.java:120)
    org.springframework.web.servlet.tags.form.LabelTag.writeTagContent(LabelTag.java:90)
    org.springframework.web.servlet.tags.form.AbstractFormTag.doStartTagInternal(AbstractFormTag.java:84)
    org.springframework.web.servlet.tags.RequestContextAwareTag.doStartTag(RequestContextAwareTag.java:80)
    org.apache.jsp.Firstpage1_jsp._jspx_meth_form_005flabel_005f0(Firstpage1_jsp.java:216)
    org.apache.jsp.Firstpage1_jsp._jspx_meth_form_005fform_005f0(Firstpage1_jsp.java:152)
    org.apache.jsp.Firstpage1_jsp._jspService(Firstpage1_jsp.java:105)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:439)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

【问题讨论】:

  • 这是一种预感,但我觉得你的类名可能在这里造成了问题,因此尝试将类名从 configbean 更改为 Configbean

标签: spring spring-mvc modelattribute


【解决方案1】:

试试:

  • 没有 ModelAttribute 注释
  • 在命令对象之后直接绑定结果

喜欢:

@RequestMapping(method=RequestMethod.POST)
public String getdetails(configbean configbean, BindingResult bindingResult){
    model.addAttribute("tnu", configbean.getTuc());
    model.addAttribute("cnu", configbean.getCnu());
    model.addAttribute("kat",configbean.getKat());
    return "result";

}

【讨论】:

    【解决方案2】:

    您正在尝试在 result.jsp 文件中使用 ${congibean.tnc},但您没有将其作为模型属性传递。相反,您正在尝试传递 "tnu","cnu" 和 "kat" 。 结果它给了你一个 jasper 异常,因为编译器无法找到任何名为 "configbean" 的模型属性。 要解决此问题,要么在 getdetails() 方法中仅传递“configBean”作为模型属性,要么将 result.jsp 文件更改为仅使用 "${tnc}", "${cnu}" and "${kat}"

    【讨论】:

      猜你喜欢
      • 2016-02-17
      • 2020-09-26
      • 2018-01-07
      • 2020-12-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多