表单验证错误后的Spring MVC(BeanResult既不是BindingResult也不是普通的目标对象) [英] Spring MVC (Neither BindingResult nor plain target object for bean name) after Form Validation error

查看:60
本文介绍了表单验证错误后的Spring MVC(BeanResult既不是BindingResult也不是普通的目标对象)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到一个模型属性的问题,该属性在表单验证后似乎消失了":

I am encountering a problem with a model attribute that seems to 'disappear' after form validation:

public class QuestionController {
    //...
    @RequestMapping(value="/get", method=RequestMethod.GET)
    public String prepareVoterBean(Model model, @RequestParam String voterID) {
        ...
        VoterBean questions = service.getQuestionBean(voterID);
        model.addAttribute("questions", questions);
        return "questionPage";
    }

    @RequestMapping(method=RequestMethod.POST)
    public String processSubmit(@Valid VoterBean questions, BindingResult result) {
        if (result.hasErrors()) {
            logger.info("QuestionController encountered form errors ");
            return "questionPage";
        }
        return "redirect:/ballot/get";
       }

以下是questionPage.jsp,其中Bean名称(问题")的BindingResult和普通目标对象都不会发生:

The following is questionPage.jsp where Neither BindingResult nor plain target object for bean name ('questions') occur:

<form:form modelAttribute="questions" method="post">
    <fieldset>      
        <legend>Security Questions</legend>
        <p>
            <form:label for="birthDate" path="birthDate" cssErrorClass="error"> <fmt:message key="questions.birthDate"/>: </form:label></br>
            <form:input path="birthDate" /><form:errors path="birthDate"/>
        </p>
        //...

questionPage可以通过HTTP get请求很好地呈现,但是当我提交表单验证错误时,从而触发processSubmit()返回问题页面,我遇到了BindingResult错误.对于我做错的事情,我感到很困惑,因为我怀疑第一次返回bean时必须使它对questionPage可用,但是随后页面突然在验证错误的HTTP POST请求之后无法找到bean.非常感谢您的帮助.谢谢.

questionPage is rendered just fine with a HTTP get request, but when I submit the form wtith validation errors, thereby triggering processSubmit() to return back to questionPage, I have the BindingResult error. I am very confused as to what I am doing wrong, because I questions bean must have been made available to questionPage when it was returned for the first time, but then suddenly the page fails to find the bean after a HTTP POST request with validation error. Your help is greatly appreciated. Thanks.

推荐答案

如果您希望Bean自动神奇地返回地图,则需要通过注释method参数来告诉Spring使用地图中的Bean作为绑定目标:

If you want the bean auto-magically back on the map you need to tell Spring to use a bean from the map as binding target by annotating the method parameter:

@RequestMapping(method=RequestMethod.POST)
public String processSubmit(@Valid @ModelAttribute("questions") VoterBean questions, BindingResult result) {
    if (result.hasErrors()) {
        logger.info("QuestionController encountered form errors ");
        return "questionPage";
    }
    return "redirect:/ballot/get";
   }

这篇关于表单验证错误后的Spring MVC(BeanResult既不是BindingResult也不是普通的目标对象)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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