继续获取:BindingResult或bean名称'index'的纯目标对象都不可用作请求属性 [英] Keep getting: Neither BindingResult nor plain target object for bean name 'index' available as request attribute

查看:1242
本文介绍了继续获取:BindingResult或bean名称'index'的纯目标对象都不可用作请求属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I can't understand what I am doing wrong. I have a controller:

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

}

p>

And Spring html form:

<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

When I try to access URL: http://localhost:8080/webclient/index.htm

我会收到这个异常:

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

我的控制器有什么问题,为什么我一直得到这样的异常?

What is wrong with my controller and why I keep getting an exception like that?

推荐答案

将进行以下更改。首先,您的 GET 方法应该是:

I would make the following changes. First your GET method should be something like:

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

使用 @ModelAttribute 注释会自动将index放入请求的模型中。

Using the @ModelAttribute annotation will automatically put "index" into the model for the request.

您的 POST 例如:

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

最后,发布,将控制器类的 @RequestMapping 注释更改为:

Finally, and probably the real issue, change the controller class's @RequestMapping annotation to:

@RequestMapping(value = "/index")

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

The ".htm" that you have is redundant. You've already configured web.xml and your Spring config to respond to ".htm" requests.

这篇关于继续获取:BindingResult或bean名称'index'的纯目标对象都不可用作请求属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆