2

我正在尝试在 spring mvc 中进行验证。我添加了 hibernate-validator-4.0.2.GA。jar 和 validation-api-1.0.0GA.jar 但我遇到了异常

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: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.jsp.contact_jsp._jspx_meth_form_005flabel_005f0(contact_jsp.java:250)
org.apache.jsp.WEB_002dINF.jsp.contact_jsp._jspService(contact_jsp.java:103)
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:432)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:238)
org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:250)
org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1060)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:798)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:716)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:647)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:552)
javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
org.apache.jasper.runtime.PageContextImpl.doForward(PageContextImpl.java:745)
org.apache.jasper.runtime.PageContextImpl.forward(PageContextImpl.java:716)
org.apache.jsp.index_jsp._jspService(index_jsp.java:71)
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:432)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)

联系.jsp

 <form:form method="get" action="addContact.html" modelAttribute="contact">
     <table>
        <tr>
             <td><form:label path="firstname">First Name</form:label></td>
             <td><form:input path="firstname" /></td> 
             <form:errors path="firstname"></form:errors>
         </tr>
        <tr>
              <td><form:label path="lastname">Last Name</form:label></td>
              <td><form:input path="lastname" /></td>
              <form:errors path="lastname"></form:errors>
        </tr>
       <tr>
             <td><form:label path="email">Email</form:label></td>
             <td><form:input path="email" /></td>
             <form:errors path="email"></form:errors>
        </tr>
     <tr>
       <td colspan="2">
         <input type="submit" value="Add Contact"/>
       </td>
       </tr>
    </table>  

联系方式.java

公共类联系{

@NotEmpty
private String firstname = null;
@NotEmpty
private  String lastname = null;
@NotEmpty
private String email=null;
/*@NotEmpty
@Min(1)
@Max(110)
private int telephone*/;

public String getFirstname() {
    return firstname;
}
public void setFirstname(String firstname) {
    this.firstname = firstname;
}
public String getLastname() {
    return lastname;
}
public void setLastname(String lastname) {
    this.lastname = lastname;
}
public String getEmail() {
    return email;
}
public void setEmail(String email) {
    this.email = email;
}

}

联系人控制器.java

    @Controller
@SessionAttributes
public class ContactController {

     @RequestMapping(value = "/addContact", method = RequestMethod.GET)

      public String addContact( @Valid @ModelAttribute("contact")  
                                           Contact contact, BindingResult 
                                        result,ModelMap model){
         model.addAttribute("contact", new Contact());

         if(result.hasErrors()) {
             System.out.println("Hiii i am validator");
                return "contact";
           }

          model.addAttribute("message", "Successfully saved person: " + contact.toString());

             model.addAttribute("name",contact.getFirstname());
             model.addAttribute("surname",contact.getLastname());
         //    model.addAttribute("age",contact.getTelephone());
             model.addAttribute("email",contact.getEmail());
            System.out.println("First Name:" + contact.getFirstname() + 
                        "Last Name:" + contact.getLastname());

            return "result";
        }
     @RequestMapping("/contacts")
        public ModelAndView showContacts() {

            return new ModelAndView("contact", "command", new Contact());
        }
}

web.xml

       <servlet>
    <servlet-name>spring</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet
             </servlet-class>
    <load-on-startup>1</load-on-startup>
     </servlet>
    <servlet-mapping>
    <servlet-name>spring</servlet-name>
    <url-pattern>/</url-pattern>
    </servlet-mapping>

spring-servlet.xml

     <context:component-scan base-package="com.demo.Controller" /> 
     <mvc:annotation-driven />
     <context:annotation-config />

  <bean id="viewResolver"
    class="org.springframework.web.servlet.view.UrlBasedViewResolver">
    <property name="viewClass"
        value="org.springframework.web.servlet.view.JstlView" />
    <property name="prefix" value="/WEB-INF/jsp/" />
    <property name="suffix" value=".jsp" />
</bean>

<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
        <property name="basename" value="/WEB-INF/messages" />
</bean>

我也尝试过使用 commandName 而不是 modelattribute 但我仍然得到相同的异常并且还尝试使用 get 和 post 方法。

4

2 回答 2

2

1 - 在加载您的 Contact.jsp 之前创建一个新的 Contact 实例

@RequestMapping("/contacts")
public ModelAndView showContacts() {
     ModelAndView m = new ModelAndView("contact");
     m.add("contact", new Contact());
     return m;
}

2 - 确保您正在调用正确的 servlet 路径:

@RequestMapping(value = "/addContact", method = RequestMethod.GET)

并在您的表单标题中更改它:

<form:form method="get" action="addContact" modelAttribute="contact">

有关此错误的更多信息here

于 2013-09-02T17:37:03.380 回答
0

我之前已经遇到过这个错误,问题是我没有在模型中放置一个简单的域对象,比如你的“联系人”,而我的 Spring 表单正在等待一个域对象。尝试做类似的事情:

model.addAttribute("contact", new contact());

这应该有效。

于 2013-08-24T17:26:02.930 回答