java.lang.IllegalStateException:既不是BindingResult也不是bean的普通目标对象

时间:2014-07-10 11:06:29

标签: java spring hibernate

我正在使用spring和hibernate.i我正在遭遇这个问题。我不明白我的意思。帮助我!谢谢提前。我遭受了java.lang.IllegalStateException:我的项目中没有BindingResult和bean问题的普通目标对象。

Contact.jsp

        <%@taglib uri="http://www.springframework.org/tags" prefix="spring"%>
                        <%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
                        <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
                        <html>
                        <head>
                            <title>Spring 3 MVC Series - Contact Manager | viralpatel.net</title>
                        </head>
                        <body>

                        <h2>Contact Manager</h2>

                        <form:form method="post" action="add" commandName="contact">

                            <table>
                            <tr>
                                <td><form:label path="firstname"><spring:message code="label.firstname"/></form:label></td>
                                <td><form:input path="firstname" /></td> 
                            </tr>
                            <tr>
                                <td><form:label path="lastname"><spring:message code="label.lastname"/></form:label></td>
                                <td><form:input path="lastname" /></td>
                            </tr>
                            <tr>
                                <td><form:label path="email"><spring:message code="label.email"/></form:label></td>
                                <td><form:input path="email" /></td>
                            </tr>
                            <tr>
                                <td><form:label path="telephone"><spring:message code="label.telephone"/></form:label></td>
                                <td><form:input path="telephone" /></td>
                            </tr>
                            <tr>
                                <td colspan="2">
                                    <input type="submit" value="<spring:message code="label.addcontact"/>"/>
                                </td>
                            </tr>
                        </table>  
                        </form:form>


                        <h3>Contacts</h3>
                        <c:if  test="${!empty contactList}">
                        <table class="data">
                        <tr>
                            <th>Name</th>
                            <th>Email</th>
                            <th>Telephone</th>
                            <th>&nbsp;</th>
                        </tr>
                        <c:forEach items="${contactList}" var="contact">
                            <tr>
                                <td>${contact.lastname}, ${contact.firstname} </td>
                                <td>${contact.email}</td>
                                <td>${contact.telephone}</td>
                                <td><a href="delete/${contact.id}">delete</a></td>
                            </tr>
                        </c:forEach>
                        </table>
                        </c:if>

                        </body>

                    </html>


                spring-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:aop="http://www.springframework.org/schema/aop"
                        xmlns:context="http://www.springframework.org/schema/context"
                        xmlns:jee="http://www.springframework.org/schema/jee"
                        xmlns:lang="http://www.springframework.org/schema/lang"
                        xmlns:p="http://www.springframework.org/schema/p"
                        xmlns:tx="http://www.springframework.org/schema/tx"
                        xmlns:util="http://www.springframework.org/schema/util"
                        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
                            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
                            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
                            http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
                            http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd
                            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
                            http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">

                        <context:annotation-config />
                        <context:component-scan base-package="rugal.sample.core.controller.ContactController" />

                        <bean id="jspViewResolver"
                            class="org.springframework.web.servlet.view.InternalResourceViewResolver">
                            <property name="viewClass"
                                value="org.springframework.web.servlet.view.JstlView" />
                            <property name="prefix" value="jsp/" />
                            <property name="suffix" value=".jsp" />
                        </bean>

                         <bean id="messageSource"
                            class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
                            <property name="basename" value="classpath:messages" />
                            <property name="defaultEncoding" value="UTF-8" />
                        </bean>
                       <!--  <bean id="propertyConfigurer"
                            class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
                            p:location="/WEB-INF/jdbc.properties" />-->

                        <bean id="dataSource"
                            class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"
                            p:driverClassName="com.mysql.jdbc.Driver"
                            p:url="jdbc:mysql://localhost:3306/testdb" p:username="root"
                            p:password="john" />


                        <bean id="sessionFactory"
                            class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
                            <property name="dataSource" ref="dataSource" />
                            <property name="configLocation">
                                <value>classpath:hibernate.cfg.xml</value>
                            </property>


                            <property name="hibernateProperties">
                                <props>
                                    <prop key="hibernate.dialect">${jdbc.dialect}</prop>
                                    <prop key="hibernate.show_sql">true</prop>
                                </props>
                            </property>
                        </bean>

                        <tx:annotation-driven />
                        <bean id="transactionManager"
                            class="org.springframework.orm.hibernate4.HibernateTransactionManager">
                            <property name="sessionFactory" ref="sessionFactory" />
                        </bean>
                    </beans>

            // its my controller class


            package rugal.sample.core.controller;

            import java.util.Map;





            import org.springframework.beans.factory.annotation.Autowired;
            import org.springframework.stereotype.Controller;
            import org.springframework.validation.BindingResult;
            import org.springframework.web.bind.annotation.ModelAttribute;
            import org.springframework.web.bind.annotation.PathVariable;
            import org.springframework.web.bind.annotation.RequestMapping;
            import org.springframework.web.bind.annotation.RequestMethod;

            import rugal.sample.core.form.Contact;
            import rugal.sample.core.service.ContactService;


            @Controller
            public class ContactController {


                @Autowired
                private ContactService contactService;

                @RequestMapping("/index")
                public String listContacts(Map<String, Object> map) {

                    map.put("contact", new Contact());
                    map.put("contactList", contactService.listContact());

                    return "contact";
                }

                @RequestMapping(value = "/add", method = RequestMethod.POST)
                public String addContact(@ModelAttribute("contact")
                Contact contact, BindingResult result) {

                    contactService.addContact(contact);

                    return "redirect:/index";
                }

                @RequestMapping("/delete/{contactId}")
                public String deleteContact(@PathVariable("contactId")
                Integer contactId) {

                    contactService.removeContact(contactId);

                    return "redirect:/index";
                }
            }

    giving me this ERROR

        org.apache.jasper.JasperException: An exception occurred processing JSP page /jsp/contact.jsp at line 16

        13:  
        14:     <table>
        15:     <tr>
        16:         <td><form:label path="firstname"><spring:message code="label.firstname"/></form:label></td>
        17:         <td><form:input path="firstname" /></td> 
        18:     </tr>
        19:     <tr>


        Stacktrace:
            org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:553)
            org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:452)
            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)
        root cause

        java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'contact' 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.jsp.contact_jsp._jspx_meth_form_005flabel_005f0(contact_jsp.java:210)
            org.apache.jsp.jsp.contact_jsp._jspx_meth_form_005fform_005f0(contact_jsp.java:132)
            org.apache.jsp.jsp.contact_jsp._jspService(contact_jsp.java:84)
            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)

2 个答案:

答案 0 :(得分:1)

JSP中的表单标记正在尝试绑定到名为contact的bean:

<form:form method="post" action="add" commandName="contact">

但您的控制器不会公开它。

尝试将此添加到您的控制器:

@ModelAttribute("contact")
public Contact createModel() {
    return new Contact();
}

答案 1 :(得分:0)

据我了解,您可能错过了将contact设置为控制器代码中的模型属性 你可以做这样的事情

public ModelAndView updateContact() {
       ModelAndView mv = new ModelAndView("viewName");
       mv.addAttribute("contact", new Contact());
       return mv;
}

您也可以在form标签中使用modelAttribute而不是像这样的

命令
<form:form modelAttribute = 'contact' ... >