Search code examples
javaspringjspspring-mvcmodelattribute

Model attribute is not available as request attribute in JSP page


I am trying to create a sample registration page with Spring MVC and JSP pages.

While opening the url on tomcat server, I am getting following error

root cause
java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'register' 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(

I have a JSP register.jsp

    <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Registration</title>
    </head>
    <body>
    <form:form action="/register/process" method="POST" modelAttribute="register">
        <table style="text-align: center;">

            <tr>
                <td><form:label path="fname">First Name</form:label></td>
                <td><form:input path="fname" name="fname"
                        id="fname" /></td>
            </tr>
            <tr>
                <td><form:label path="lname">Last Name</form:label></td>
                <td><form:input path="lname" name="lname" id="lname" />
                </td>
            </tr>
            <tr>
                <td></td>
                <td>
                <input type="submit" value="CREATE AN ACCOUNT"/>
                </td>
            </tr>
        </table>
    </form:form>
   </body>
  </html>

I have a controller class UserController.java

package vnfhub.supplier.controller;

@Controller
public class UserController {
    @RequestMapping(value = "/register", method = RequestMethod.GET)
    public String getRegisterForm(Model model) {
        model.addAttribute("register", new Register());
        return "register";
    }

   @RequestMapping(value = "/register/process", method = RequestMethod.POST)
   public String processRegistration(@ModelAttribute("register") Register register, BindingResult result) {
       return "success";
   }
}

and a success.jsp page

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>Success Form</title>
</head>
<body>
<font color="green"><h1>Hello</h1></font>

<h1>You have successfully registered</h1>
<font color="green"><h1>Welcome to Spring world !</h1></font>
</body>
</html>

I have tried many solution on stackoverflow.... but none of them worked.


Solution

  • I find your code okay so far as you given here. I mimic the situation with your code but unfortuantely found No Exception.

    enter image description here

    Things that you might have doing wrong is you are running some old build code in your tomcat. try to clean build and re-deploy in your container.

    NB: one friendly suggestion. You are doing one thing wrong that is having action of your form to /register/process that will send the request to the container root (e.g. localhost:8080/register/process). And you will get 404 for that. You are not probably want that. register/process should be your URL and this will POST the request relative to your application-context. If your application context is something localhost:8080/test, this will send the request to localhost:8080/test/register/process