【问题标题】:Keep getting: Neither BindingResult nor plain target object for bean name 'index' available as request attribute不断得到: Bean 名称“索引”的 BindingResult 和普通目标对象都不能用作请求属性
【发布时间】:2025-12-02 03:05:02
【问题描述】:

我不明白我做错了什么。我有一个控制器:

@Controller
@RequestMapping(value = "/index.htm")
public class LoginController {

    @Autowired
    private AccountService accountService;

    @RequestMapping(method = RequestMethod.GET)
    public String showForm(Map model) {
        model.put("index", new LoginForm());
        return "index";
    }

    @ModelAttribute("index")
    public LoginForm getLoginForm() {
        return new LoginForm();
    }

    @RequestMapping(method = RequestMethod.POST)
    public String processForm(LoginForm loginForm, BindingResult result,
                              Map model) {

        if (result.hasErrors()) {
            HashMap<String, String> errors = new HashMap<String, String>();
            for (FieldError error : result.getFieldErrors()) {
                errors.put(error.getField(), error.getDefaultMessage());
            }
            model.put("errors", errors);
            return "index";
        }

        List<Account> accounts = accountService.findAll();
        loginForm = (LoginForm) model.get("loginForm");


        model.put("index", loginForm);
        return "loginsuccess";
    }

}

还有Spring html表单:

<form:form action="index.htm" commandName="index">

    <table cellspacing="10">
        <tr>
            <td>
                <form:label path="username">
                    <spring:message code="main.login.username"/>
                </form:label>
            </td>
            <td>
                <form:input path="username" cssClass="textField"/>
            </td>
        </tr>
        <tr>
            <td>
                <form:label path="password">
                    <spring:message code="main.login.password"/>
                </form:label>
            </td>
            <td>
                <form:password path="password" cssClass="textField"/>
            </td>
        </tr>
        <tr>
            <td>
                <input type="submit" class="button" value="Login"/>
            </td>
        </tr>
    </table>

</form:form>

当我尝试访问 URL 时:http://localhost:8080/webclient/index.htm

我不断收到此异常:

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

我的控制器出了什么问题,为什么我不断收到这样的异常?

【问题讨论】:

  • 也许尝试删除commandName="index"
  • 已经尝试过了,如果我删除它,我会得到: BindingResult 和 bean 名称 'command' 的普通目标对象都不能用作请求属性。有“命令”而不是“索引”。
  • 不应该是modelAttribute而不是commandName吗?
  • 已经试过了。还是同样的问题:/

标签: java spring spring-mvc controller


【解决方案1】:

我会进行以下更改。首先你的GET 方法应该是这样的:

@RequestMapping(method = RequestMethod.GET)
public String showForm(@ModelAttribute("index") LoginForm loginForm) {
    return "index";
}

使用@ModelAttribute注解会自动将“索引”放入请求的模型中。

你的POST 方法声明应该是这样的:

@RequestMapping(method = RequestMethod.POST)
public String processForm(@ModelAttribute("index") LoginForm loginForm, 
                          BindingResult result, Map model) {

最后,也可能是真正的问题,将控制器类的 @RequestMapping 注释更改为:

@RequestMapping(value = "/index")

您拥有的“.htm”是多余的。您已经配置了 web.xml 和 Spring 配置以响应“.htm”请求。

【讨论】:

  • 谢谢,我会尽力告诉你的。