Q:

Unable to do form validation in spring

I am trying to do validation in spring mvc. I have added the hibernate-validator-4.0.2.GA. jar and validation-api-1.0.0GA.jar but i am getting exception
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
public class Contact {
@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>

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>

I have also tried using commandName instead of modelattribute but still i get the same exception and also tried using both get and post methods.

A:

1 - Create a new Contact instance before loading your Contact.jsp
@RequestMapping("/contacts")
public ModelAndView showContacts() {
     ModelAndView m = new ModelAndView("contact");
     m.add("contact", new Contact());
     return m;
}

2 - Ensure you are invoking the right servlet path :
@RequestMapping(value = "/addContact", method = RequestMethod.GET)

and change it in you form's header: 
<form:form method="get" action="addContact" modelAttribute="contact">

Some further information about this error here

