Reputation: 1586
I am working with Spring MVC and I am getting the below error:
Caused by: java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'user' available as request attribute
This error usually occurs when we have not passed/added the object in the model inside the controller code. But I have done that and I am still getting the error.
I have looked through solutions on the internet with exact same errors but all of them point to adding the new object in the controller. Not sure why for me it's not working.
Not sure what I am doing wrong.
This is my form in login.html:
<div class="container">
<div class="starter-template">
<h2>Login</h2>
</div>
<form th:object="${user}" th:method="post" th:action="validateUser" class="form-horizontal">
<table class="table table-striped">
<tr>
<td>
<div class="control-group">
<label class="control-label">Email</label>
</div>
</td>
<td>
<div class="controls">
<input type="text" class="form-control" th:field="*{emailAddress}"/>
<label class="control-label"></label>
</div>
</td>
</tr>
<tr>
<td>
<div class="control-group">
<label class="control-label">Password</label>
</div>
</td>
<td>
<div class="controls">
<input type="password" class="form-control" th:field="*{password}"/>
<label class="control-label"></label>
</div>
</td>
</tr>
<tr>
<td></td>
<td>
<div class="form-actions pull-right">
<input type="submit" name="_eventId_validateUser" value="Login"
class="btn btn-success" tabindex="5"/>
<input type="submit" name="_eventId_cancel" value="Cancel"
class="btn btn-danger" tabindex="6"/>
</div>
</td>
</tr>
</table>
</form>
</div>
My Controller.java:
package com.niti.controller;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.niti.authentication.service.AuthenticationService;
import com.niti.bo.UserBO;
import com.niti.service.exception.ServiceBusinessException;
@Controller
public class LoginController {
private static final Logger Logger = LoggerFactory.getLogger(LoginController.class);
@Autowired
private AuthenticationService authenticationService;
@RequestMapping(value = "/login", method = RequestMethod.GET)
public String login(Model model) {
model.addAttribute("user", new UserBO());
return "login";
}
@RequestMapping(value = "/validateUser", method = RequestMethod.POST)
public String processLoginInfo(@ModelAttribute UserBO userBO) throws ServiceBusinessException {
UserBO user = authenticationService.authenticateUser(userBO.getEmailAddress(), userBO.getPassword());
return "userDetails";
}
}
Upvotes: 3
Views: 4288
Reputation: 12582
In your html form you are binding
th:object="${user}" // user
On the other hand you are binding a userBO
by default in your controller method processLoginInfo
.
Your method should be like so
@RequestMapping(value="/validateUser" , method=RequestMethod.POST)
public String processLoginInfo(@ModelAttribute("user") UserBO userBO) throws ServiceBusinessException {
UserBO user = authenticationService.authenticateUser(userBO.getEmailAddress(), userBO.getPassword());
return "userDetails";
}
Upvotes: 3