0

I am trying to switch from html form to spring form so that I can use message properties which I think is a pretty cool feature. But I have encountered error Http 500:

org.apache.jasper.JasperException: An exception occurred processing JSP page /index.jsp at line 19

18:             <form:form action="example" method="post" modelAttribute="example" >
19:                 UserName<form:input path="email" />
20:                 Password<form:input path="password" />
21:             </form:form>    


Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:568)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:465)
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:727)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
root cause

java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'example' available as request attribute

I look for other threads in the forum but couldn't find the cause. Can you suggest me where I fix it? This is my controller code:

@RequestMapping(value="/example", method=RequestMethod.POST)
public String example(@ModelAttribute("example") UserAccount aUser, BindingResult result, Model model) {
    model.addAttribute("example", aUser);
    return "";
}
Terry dang
  • 17
  • 4
  • possible duplicate of [java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'category' available as request attribute](http://stackoverflow.com/questions/21790656/java-lang-illegalstateexception-neither-bindingresult-nor-plain-target-object-f) – Sotirios Delimanolis Sep 03 '14 at 23:55
  • @SotiriosDelimanolis: there a lot of similar questions about this problem asked in the forum. I searched and tried but I couldn't find out the cause before I asked ... – Terry dang Sep 04 '14 at 00:00

1 Answers1

0

First, forget about the handler method you posted. It is completely uninvolved in what happened here.

Second, the error states

Neither BindingResult nor plain target object for bean name 'example' available as request attribute

Therefore, when your JSP code

<form:form action="example" method="post" modelAttribute="example" >
    UserName<form:input path="email" />
    Password<form:input path="password" />
</form:form>    

is being rendered by the JSP Servlet, and processed by Spring, no HttpServletRequest attributed named example exists. This means that you haven't added one.

So go to the controller (if one doesn't exist, create it) that returns the JSP view name of the JSP above, and add a HttpServletRequest attribute or a model attribute named example and of type UserAccount. For example

model.addAttribute("example", new UserAccount());

Spring will use this (command) object to create an HTML <form> with corresponding input fields.

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724