gpt4 book ai didi

java - Spring MVC : Neither BindingResult nor plain target object for bean name 'user' available as

转载 作者:行者123 更新时间:2023-12-04 14:25:17 27 4
gpt4 key购买 nike

我正在使用 Spring MVC,但出现以下错误:

Caused by: java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'user' available as request attribute

当我们没有在 Controller 代码中传递/添加模型中的对象时,通常会发生此错误。但我已经这样做了,但我仍然收到错误。

我在互联网上查看了完全相同错误的解决方案,但所有这些都指向在 Controller 中添加新对象。不知道为什么对我来说它不起作用。

不确定我做错了什么。

这是我在 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";
}
}

最佳答案

在你绑定(bind)的html表单中

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

另一方面,您在 Controller 方法 processLoginInfo 中默认绑定(bind)了一个 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";
}

关于java - Spring MVC : Neither BindingResult nor plain target object for bean name 'user' available as,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46752886/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com