无法在春季进行表单验证

时间:2013-08-24 10:52:04

标签: validation spring-mvc

我正在尝试在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)

Contact.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>  

Contact.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;
}

}

ContactController.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>

弹簧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方法。

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

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

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

这应该有用。