2013-08-24 62 views
2

我正在嘗試在春季mvc進行驗證。我已經添加了hibernate-validator-4.0.2.GA。罐子和驗證-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> 

爲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> 

我一直在使用命令名,而不是modelattrib也嘗試但我仍然得到相同的異常,並嘗試使用get和post方法。

回答

2

1 - 加載你的Contact.jsp

@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

0

我以前已經有過這個錯誤,問題是我沒有在tha模型中放置一個像「Contact」這樣的簡單域對象,而我的Spring窗體正在等待一個域對象。 嘗試做類似的事情:

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

而且應該有效。

+0

我已經添加了這行代碼,但仍然收到相同的錯誤 – coder25

+0

您詢問了有關驗證的問題,所以我想如果沒有綁定結果和@Valid,它會起作用吧? – sam

+0

不,它沒有工作,沒有綁定結果和@Valid – coder25