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

In theory after clicking submit, the login method should be called and check the user’s username and password for a match and redirect them to another .jsp page, but when I try to load the .jsp page, I get the following error.

I’m certain the error has something to do with my .jsp file but I’m not sure what could have gone wrong.

Jul 31, 2017 8:03:46 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [jsp] in context with path [/Atomicity] threw exception [An exception occurred processing JSP page [/login.jsp] at line [20]

17:                 class="register-form">
18:                 <table>
19:                     <tr>
20:                         <td><form:input path="username" /></td>
21:                         <td><form:errors path="username" /></td>
22:                     </tr>
23:                     <tr>


Stacktrace:] with root cause
java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'login' available as request attribute
at org.springframework.web.servlet.support.BindStatus.<init>(BindStatus.java:144)
at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getBindStatus(AbstractDataBoundFormElementTag.java:168)
at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getPropertyPath(AbstractDataBoundFormElementTag.java:188)
at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getName(AbstractDataBoundFormElementTag.java:154)
at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.autogenerateId(AbstractDataBoundFormElementTag.java:141)
at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.resolveId(AbstractDataBoundFormElementTag.java:132)
at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.writeDefaultAttributes(AbstractDataBoundFormElementTag.java:116)
at org.springframework.web.servlet.tags.form.AbstractHtmlElementTag.writeDefaultAttributes(AbstractHtmlElementTag.java:422)
at org.springframework.web.servlet.tags.form.InputTag.writeTagContent(InputTag.java:142)
at org.springframework.web.servlet.tags.form.AbstractFormTag.doStartTagInternal(AbstractFormTag.java:84)
at org.springframework.web.servlet.tags.RequestContextAwareTag.doStartTag(RequestContextAwareTag.java:80)
at org.apache.jsp.login_jsp._jspx_meth_form_005finput_005f0(login_jsp.java:298)
at org.apache.jsp.login_jsp._jspService(login_jsp.java:165)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:443)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:385)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:329)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:475)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:80)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:625)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:498)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:796)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1372)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)

[Login.jsp]

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<link rel="shortcut icon" target="_blank" rel="nofollow" href="favicon1.ico" />
<title>Login page</title>
<link rel="stylesheet" target="_blank" rel="nofollow" href="css/normalize.css" />
<link rel="stylesheet" target="_blank" rel="nofollow" href="css/mystyle.css" />
</head>
<body>
<img id="logoImg2" src="img/atomicitylogo.png" alt="atomicity logo">
<div class="login-page">
    <div class="form">
        <form:form action="login" method="POST" commandName="login"
            class="register-form">
            <table>
                <tr>
                    <td><form:input path="username" /></td>
                    <td><form:errors path="username" /></td>
                </tr>
                <tr>
                    <td><form:input path="password" /></td>
                    <td><form:errors path="password" /></td>
                </tr>
                <tr>
                    <td colspan="2"><input type="submit" value="Log In" />
                        <p class="message">
                </tr>
            </table>
            <p>
                Not registered? <a target="_blank" rel="nofollow" href="register.html">Create an account</a>
            </p>
        </form:form>
        </div>
    </div>
</body>
</html>

[LoginComponent.java]

package com.atomicity.components;

import javax.validation.constraints.NotNull;

import org.springframework.stereotype.Component;

@Component(value = "login")
public class LoginComponent {

@NotNull
private String username;
@NotNull
private String password;

public String getUsername() {
    return username;
}

public void setUsername(String username) {
    this.username = username;
}

public String getPassword() {
    return password;
}

public void setPassword(String password) {
    this.password = password;
}
}

[LoginController.java]

package com.atomicity.controllers;

import javax.validation.Valid;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.atomicity.components.LoginComponent;
import com.atomicity.dao.UsersDAO;
import com.atomicity.domain.Users;

@Controller
public class LoginController {

@RequestMapping(value = "/loggingIn", method = RequestMethod.POST)
public String login(@Valid @ModelAttribute("login") LoginComponent login, BindingResult br, Model m) {
    ApplicationContext appCon = new ClassPathXmlApplicationContext("beans.xml");
    UsersDAO uDAO = (UsersDAO) appCon.getBean("usersDAO");
    Users user = uDAO.getUserByName(login.getUsername());

    if (user != null) {
        // Check if password is correct
        if (user.getPassword().equals(login.getPassword())) {
            m.addAttribute("firstname", user.getFirstName());
            m.addAttribute("lastname", user.getLastName());
            m.addAttribute("email", user.getEmail());
            m.addAttribute("username", login.getUsername());
            ((AbstractApplicationContext) appCon).close();
            return "account";
        } else {
            // Wrong Password
        }
    } else {
        // User/Password does not exists
    }
    ((AbstractApplicationContext) appCon).close();
    return null;
}
}

Any help is welcomed.

[Edit]Removing the table in login.jsp allows the page to show, so I think the problem might be there (or not).

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

Looking at the stack trace it indicates you have not passed the login object to the view. Thus the JSP cannot be rendered correctly..

@GetMapping("/login")
public String login(Model m) {

    //Use model to expose the LoginComponent object
    m.addAttribute("login", new LoginComponent());
    return "login";
}

You can find a more detailed walkthrough here: https://spring.io/guides/gs/handling-form-submission/