【发布时间】:2018-03-26 22:42:06
【问题描述】:
我正在使用 Spring MVC,但出现以下错误:
原因:java.lang.IllegalStateException:Bean 名称“用户”的 BindingResult 和普通目标对象都不能用作请求属性
当我们没有在控制器代码中的模型中传递/添加对象时,通常会发生此错误。但我已经这样做了,我仍然收到错误。
我在互联网上查看了具有完全相同错误的解决方案,但所有这些都指向在控制器中添加新对象。不知道为什么对我来说它不起作用。
不知道我做错了什么。
这是我在 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>
我的 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";
}
}
【问题讨论】:
标签: java spring spring-mvc thymeleaf