java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'user' available as request attribute So i got this error when i'm trying to run this web page, and i don't know why. I saw and read a lot about this error but i can't find out what is the problem.
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web- app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>onallo_labor</display-name>
<welcome-file-list>
<welcome-file>welcome.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>servlet_1</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>servlet_1</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/servlet_1-servlet.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
</web-app>
servlet_1-servlet.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="org.springframework.samples.petclinic.web"/>
</beans>
CreateController:
package onallo_labor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.portlet.ModelAndView;
@Controller
public class CreateController {
@RequestMapping(value="create",method=RequestMethod.GET)
public ModelAndView create(){
return new ModelAndView("create","user",new User());
}
@RequestMapping(value="",method=RequestMethod.GET)
public ModelAndView welcome(){
return new ModelAndView("welcome");
}
@RequestMapping(value="create_ready",method=RequestMethod.POST)
public String addContact(@ModelAttribute("user")
User user,Model model) {
model.addAttribute("username",user.getUsername());
model.addAttribute("birthday",user.getBirthday());
return "create_ready";
}
}
create.jsp:
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>User Account Creation</title>
</head>
<body>
<div align="center">
<form:form method="POST" modelAttribute="user">
<table border="0">
<tr>
<td colspan="2" align="center"><h2>Registration Form</h2></td>
</tr>
<tr>
<td>Username: </td>
<td><form:input path="username" id="djb"/></td>
</tr>
<tr>
<td>Password: </td>
<td><form:password path="password"/></td>
</tr>
<tr>
<td>Birthday: </td>
<td><form:input path="birthday"/></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="Submit" /></td>
</tr>
</table>
</form:form>
</div>
</body>
</html>
The create_ready.jsp:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h2>Submitted User Information</h2>
<table>
<tr>
<td>Name</td>
<td>${username}</td>
</tr>
<tr>
<td>Birthday</td>
<td>${birthday}</td>
</tr>
</table>
</body>
</html>
The User Class:
package onallo_labor;
public class User {
public User(){}
public String username;
public String password;
public String birthday;
public String getUsername(){return this.username;}
public void setUsername(String un){this.username=un;}
public String getPassword(){return this.password;}
public void setPassword(String pw){this.password=pw;}
public String getBirthday(){return this.birthday;}
public void setBirthday(String bd){this.birthday=bd;}
}
There is a pic about the structure of the files.
http://kepkezelo.com/images/lm418k9xegg2aukio3.png
UPDATE1: So i changed the method name's in the User class, now here is the error:
org.apache.jasper.JasperException: An exception occurred processing JSP page /create.jsp at line 20
17: </tr>
18: <tr>
19: <td>Username: </td>
20: <td><form:input path="username" id="djb"/></td>
21: </tr>
22: <tr>
23: <td>Password: </td>
Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:510)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:413)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
root cause
java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'user' available as request attribute
org.springframework.web.servlet.support.BindStatus.<init>(BindStatus.java:144)
org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getBindStatus(AbstractDataBoundFormElementTag.java:168)
org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getPropertyPath(AbstractDataBoundFormElementTag.java:188)
org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getName(AbstractDataBoundFormElementTag.java:154)
org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.writeDefaultAttributes(AbstractDataBoundFormElementTag.java:117)
org.springframework.web.servlet.tags.form.AbstractHtmlElementTag.writeDefaultAttributes(AbstractHtmlElementTag.java:422)
org.springframework.web.servlet.tags.form.InputTag.writeTagContent(InputTag.java:142)
org.springframework.web.servlet.tags.form.AbstractFormTag.doStartTagInternal(AbstractFormTag.java:84)
org.springframework.web.servlet.tags.RequestContextAwareTag.doStartTag(RequestContextAwareTag.java:80)
org.apache.jsp.create_jsp._jspx_meth_form_005finput_005f0(create_jsp.java:181)
org.apache.jsp.create_jsp._jspx_meth_form_005fform_005f0(create_jsp.java:125)
org.apache.jsp.create_jsp._jspService(create_jsp.java:78)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:377)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
Thank you for the answers.
Your User class is wrong for it to be a java bean it has to follow certain naming conventions. Your setter and getter have wrong names, example,
The problem is a combination of things. You have
and
The
ModelAndView
from the portlet library is not a default recognized return type for theDispatcherServlet
stack. As such, it will be considered as a plainObject
and added to the model itself. Because your URI is.../create
, Spring will use that last path element to map to a view. So it will end up resolving yourcreate.jsp
as a view but for the wrong reasons.Change your
ModelAndView
toI don't know why you have a portlet dependency, but get rid of it if you don't need it.
You're also missing
from your servlet context.
You're
<servlet>
is mapped toso it's unclear to me what URL you're sending your request to,
../create.html
?