Analysis and Solution for Spring MVC Form Binding Exception: Neither BindingResult nor plain target object for bean name 'login' available as request attribute

Nov 19, 2025 · Programming · 18 views · 7.8

Keywords: Spring MVC | Form Binding | Model Attribute | Exception Handling | Annotation Driven

Abstract: This article provides an in-depth analysis of the common Spring MVC exception 'Neither BindingResult nor plain target object for bean name available as request attribute'. Through practical case studies, it demonstrates the causes of this exception and presents comprehensive solutions. The article explains the working mechanism of Spring form binding, including model attribute transmission, request processing flow, and view rendering process, along with complete code examples and best practice recommendations.

Problem Background and Exception Analysis

In Spring MVC framework development, form processing is a common functional requirement. However, many developers encounter a typical exception when first working with Spring annotation-driven development: Neither BindingResult nor plain target object for bean name 'login' available as request attribute. This exception typically occurs when JSP pages attempt to render forms and the system cannot find the corresponding form binding object.

Exception Generation Mechanism

Spring MVC's form tag library relies on form binding objects in the model to correctly render form elements. When using the <form:form> tag, the system attempts to find the specified modelAttribute object in the request attributes. If this object does not exist, the aforementioned exception is thrown.

In a typical Spring MVC request processing flow:

  1. User accesses GET request to display the form
  2. Controller method adds form object to the model
  3. View renders using the form object from the model
  4. User submits the form (POST request)
  5. Controller processes form data

Root Cause Analysis

From the provided code case, the main issue occurs in the GET request processing phase. The developer only defined a method to handle POST requests but lacked a method to handle GET requests for displaying the form. When users first access the form page, the system cannot obtain the login object, causing the exception.

Specific problem analysis:

@Controller
@SessionAttributes
public class LoginController {

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public String login(@ModelAttribute("login") Login login, BindingResult result) {
        System.out.println(" email entered "+ login.getEmail()+ "\n");
        return "test";
    }
}

This code only handles POST requests. When users access /login via GET request, there is no corresponding controller method to provide the login object.

Solution

According to best practices, it's necessary to add a method to handle GET requests and initialize the form object in the model:

@Controller
@SessionAttributes("login")
public class LoginController {

    @RequestMapping(value = "/login", method = RequestMethod.GET)
    public String showLoginForm(Model model) {
        model.addAttribute("login", new Login());
        return "login";
    }

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public String processLogin(@ModelAttribute("login") Login login, 
                              BindingResult result) {
        // Process form submission logic
        System.out.println("Email entered: " + login.getEmail());
        return "test";
    }
}

Detailed Explanation of Spring Form Binding Mechanism

Spring MVC's form processing mechanism is based on the Model-View-Controller pattern. When using the @ModelAttribute annotation, Spring performs the following operations:

  1. In GET requests, controller methods add form objects to the model
  2. During view rendering, the form tag library retrieves objects from the model
  3. In POST requests, Spring automatically binds request parameters to model objects
  4. Validation results are returned through the BindingResult object

The @SessionAttributes annotation serves to store model attributes in the session, ensuring state persistence across multiple requests. This is particularly useful in multi-step form processing.

Complete Configuration Example

Below is a complete configuration example, including JSP page and controller:

JSP Page (login.jsp):

<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<form:form method="POST" action="login.htm" modelAttribute="login">
    <div>
        <label for="email">Email:</label>
        <form:input path="email" size="20" />
    </div>
    <div>
        <input type="submit" value="Login" />
    </div>
</form:form>

Form Object (Login.java):

public class Login {
    private String email;
    
    public String getEmail() {
        return email;
    }
    
    public void setEmail(String email) {
        this.email = email;
    }
}

Best Practice Recommendations

Based on practical development experience, it's recommended to follow these best practices:

  1. Separate GET and POST Request Handling: Define separate processing methods for form display and form submission
  2. Proper Use of Model Attributes: Ensure form objects are added to the model before displaying forms
  3. Reasonable Use of Session Attributes: Use @SessionAttributes only when state persistence across requests is necessary
  4. Configure Component Scanning: Ensure controller classes are within the component scanning path
  5. Exception Handling: Add appropriate exception handling mechanisms to provide user-friendly error messages

Common Issue Troubleshooting

When encountering form binding exceptions, follow these troubleshooting steps:

  1. Check if the controller includes methods to handle GET requests
  2. Verify that model.addAttribute() calls are correct
  3. Confirm that modelAttribute names in JSP pages match those in the controller
  4. Check if component scanning configuration is correct
  5. Verify that form objects have proper getter and setter methods

Conclusion

While Spring MVC's form binding mechanism is powerful, developers need to understand its working principles. By properly configuring GET request processing methods and initializing form objects in the model, the 'Neither BindingResult nor plain target object' exception can be avoided. Mastering these fundamental concepts is crucial for building robust web applications.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.