“org.apache.jasper.JasperException”表单验证失败后

发布于 2024-11-05 22:47:55 字数 2890 浏览 0 评论 0原文

当我的表单验证失败时,我总是收到以下错误:

org.apache.jasper.JasperException:java.lang.IllegalStateException:BindingResult 和 bean 名称“regform”的普通目标对象都不能作为请求属性

当表单输入有效时,我不会收到此错误。根本原因是

java.lang.IllegalStateException: 既不是 BindingResult 也不是普通目标 bean 名称“regform”的对象 可作为请求属性

这是 net.sandbox.controllers.RegistrationController ,为简洁起见,省略了导入:

@Controller
@RequestMapping("/register")
public class RegistrationController {
    @Autowired
    private UserInfo userInfo;

    @RequestMapping(method = RequestMethod.GET)
    public String showRegForm(Model model) {
        RegistrationForm regForm = new RegistrationForm();
        model.addAttribute("regform", regForm);
        return "regform";
    }

    @RequestMapping(method = RequestMethod.POST)
    public String validateForm(@Valid RegistrationForm regForm, BindingResult result, Model model) {
        if (result.hasErrors()) {
            return "regform";
        }

        userInfo.setUserName(regForm.getFirstName());
        model.addAttribute("regform", regForm);
        return "regsuccess";
    }
}

这是什么意思?


更新:添加了请求的JSP文件。

regform.jsp

<jsp:include page="includes/header.jsp">
    <jsp:param name="pageTitle" value="Registration" />
</jsp:include>
<%@taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
        <h2>Register below.</h2>
        <form:form method="post" commandName="regform">
            <p><form:input path="firstName" /> <form:errors path="firstName" /></p>
            <p><input type="submit" /></p>
        </form:form>
<jsp:include page="includes/footer.jsp" />

header.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<jsp:useBean id="userInfo" scope="session" class="net.sandbox.sessionbeans.UserInfo" />
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title><%= request.getParameter("pageTitle") %></title>
    </head>
    <body>
        <h1 style="float: left; width: 50%">Sandbox -- <%= request.getParameter("pageTitle") %></h1>
        <h4 style="float: left; text-align: right; width: 50%"><% out.print(userInfo.getUserName()); %></h4>
        <hr style="clear: both" />

footer.jsp

    <hr />
    <p><i>Copyright information goes here.</i></p>
    </body>
</html>

I always get the following error when my form fails to validate:

org.apache.jasper.JasperException: java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'regform' available as request attribute

I don't get this error when the form input is valid. The root cause is

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

Here is net.sandbox.controllers.RegistrationController with the imports omitted for brevity's sake:

@Controller
@RequestMapping("/register")
public class RegistrationController {
    @Autowired
    private UserInfo userInfo;

    @RequestMapping(method = RequestMethod.GET)
    public String showRegForm(Model model) {
        RegistrationForm regForm = new RegistrationForm();
        model.addAttribute("regform", regForm);
        return "regform";
    }

    @RequestMapping(method = RequestMethod.POST)
    public String validateForm(@Valid RegistrationForm regForm, BindingResult result, Model model) {
        if (result.hasErrors()) {
            return "regform";
        }

        userInfo.setUserName(regForm.getFirstName());
        model.addAttribute("regform", regForm);
        return "regsuccess";
    }
}

What does it mean?


Update: added requested JSP files.

regform.jsp

<jsp:include page="includes/header.jsp">
    <jsp:param name="pageTitle" value="Registration" />
</jsp:include>
<%@taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
        <h2>Register below.</h2>
        <form:form method="post" commandName="regform">
            <p><form:input path="firstName" /> <form:errors path="firstName" /></p>
            <p><input type="submit" /></p>
        </form:form>
<jsp:include page="includes/footer.jsp" />

header.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<jsp:useBean id="userInfo" scope="session" class="net.sandbox.sessionbeans.UserInfo" />
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title><%= request.getParameter("pageTitle") %></title>
    </head>
    <body>
        <h1 style="float: left; width: 50%">Sandbox -- <%= request.getParameter("pageTitle") %></h1>
        <h4 style="float: left; text-align: right; width: 50%"><% out.print(userInfo.getUserName()); %></h4>
        <hr style="clear: both" />

footer.jsp

    <hr />
    <p><i>Copyright information goes here.</i></p>
    </body>
</html>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鸠书 2024-11-12 22:47:55

这是因为,在 validateForm(..) 方法中,一旦表单验证失败,您不会立即将表单支持对象放入 modelMap 中。如果您像这样重新组织代码:

   @RequestMapping(method = RequestMethod.POST)
    public String validateForm(@Valid RegistrationForm regForm, BindingResult result, Model model) {
        model.addAttribute("regform", regForm);
        if (result.hasErrors()) {
            return "regform";
        }

        userInfo.setUserName(regForm.getFirstName());        
        return "regsuccess";
    }

您可以解决您的问题,但这仍然不是最佳解决方案。最佳实践是使用这样的方法来填充表单对象:

@ModelAttribute("regform")
public RegistrationForm populateForm() {
     RegistrationForm regForm = new RegistrationForm();
     /* init regForm */
     return regForm;
}

使用 populateForm 方法,您不需要自己处理表单支持对象的创建。

That's because, in your validateForm(..) method you don't put a form backing object into the modelMap as soon as the form validation fails. If you would reorganize your code like this:

   @RequestMapping(method = RequestMethod.POST)
    public String validateForm(@Valid RegistrationForm regForm, BindingResult result, Model model) {
        model.addAttribute("regform", regForm);
        if (result.hasErrors()) {
            return "regform";
        }

        userInfo.setUserName(regForm.getFirstName());        
        return "regsuccess";
    }

you could solve your problem, but it is still not a optimal solution. Best practice is to use a method to populate form objects like this:

@ModelAttribute("regform")
public RegistrationForm populateForm() {
     RegistrationForm regForm = new RegistrationForm();
     /* init regForm */
     return regForm;
}

using populateForm method, you don't need to handle of creation of form backing objects yourself.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文