Spring MVC:既不能绑定结果,也不能作为 bean 名称"user"的普通目标对象



我正在使用Spring MVC,并收到以下错误:

由以下原因引起:java.lang.IllegalStateException:bean名称"user"的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>

我的控制器.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";
}       
}

在html表单中,您正在绑定

th:object="${user}" // user

另一方面,默认情况下,在控制器方法processLoginInfo中绑定userBO

你的方法应该是这样的

@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";
}

相关内容

最新更新