Thymeleaf Neither BindingResult nor plain target object for bean name 'person' available as request attribute -


from can tell set correctly getting following error:

java.lang.illegalstateexception: neither bindingresult nor plain target  object bean name 'person' available request attribute 

form

<form action="#" th:action="@{/person}" th:object="${person}" method="post" th:required="required">                 <input type="text" th:field="*{subject}" class="contact col-md-6" placeholder="name *" th:required="required"/>                 <input type="text" th:field="*{name}" class="contact col-md-6" placeholder="name *" th:required="required"/>                 <input type="text" th:field="*{lastname}" class="contact col-md-6" placeholder="name *" th:required="required"/>                 <input type="email" th:field="*{email}" class="contact nomarr col-md-6" placeholder="e-mail address *" th:required="required"/>                 <textarea name="comment" class="contact col-md-12" th:field="*{message}" placeholder="message *" ></textarea>                 <input type="submit" id="submit" class="contact submit" value="send message" />             </form> 

person.java

public class person {      private int id;     private string name;     private string lastname;     private string email;     private string subject;     private string message;      .... } 

controller

@controller public class applicationcontroller {      @requestmapping(value = "/", method = requestmethod.get)     public string indexpage(){         return "index";     }      @requestmapping(value="/person", method=requestmethod.get)     public string contactform(model model) {         model.addattribute("person", new person());         return "index";     }      @requestmapping(value="/person", method=requestmethod.post)     public string contactsubmit(@modelattribute person person, model model) {         model.addattribute("person", person);         return "result";     } } 

i looked @ spring-boot , thmeleaf setup , looks setup identical.

---------------------update 1-----------------------

i have changed post method include bindingresult no success.

 @requestmapping(value="/person", method=requestmethod.post)     public string contactsubmit(@valid @modelattribute person person, bindingresult bindingresult, model model) {          if(bindingresult.haserrors()){             system.out.println("there error "+bindingresult);             system.out.println("person is: "+ person.getemail());             return "index";         }          model.addattribute("person", person);         return "result";     } 

you forgot add bindingresult after @modelattribute :

@requestmapping(value="/person", method=requestmethod.post) public string contactsubmit(@modelattribute person person, bindingresult bindingresult, model model) {     if (bindingresult.haserrors()) {         //errors processing     }       model.addattribute("person", person);     return "result"; } 

i'm have answered question :


Comments

Popular posts from this blog

java - nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet Hibernate+SpringMVC -

sql - Postgresql tables exists, but getting "relation does not exist" when querying -

asp.net mvc - breakpoint on javascript in CSHTML? -