[英]Spring MVC Neither BindingResult nor plain target object for bean name
[英]Spring MVC : Neither BindingResult nor plain target object for bean name 'user' available as
我正在使用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";
}
}
您以html形式綁定
th:object="${user}" // user
在另一方面你要綁定一個userBO
在你的控制器方法默認processLoginInfo
。
你的方法應該像這樣
@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";
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.