【问题标题】:"org.apache.jasper.JasperException" after failing form validation表单验证失败后出现“org.apache.jasper.JasperException”
【发布时间】:2011-05-08 16:22:14
【问题描述】:

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

org.apache.jasper.JasperException: java.lang.IllegalStateException: Bean name 'regform' 的 BindingResult 和普通目标对象都不能用作请求属性

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

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>

【问题讨论】:

  • 请张贴jsp页面表单标签
  • 我为上面的表单添加了 JSP 源代码。

标签: spring jsp spring-mvc


【解决方案1】:

这是因为,在您的 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 方法,您不需要自己处理表单支持对象的创建。

【讨论】:

  • 我不再收到服务器错误,但未向用户显示表单验证错误。我确实在约束上设置了消息:@NotEmpty(message = "Please enter your first name.")@Size(min = 2, message = "It is highly unlikely that you have a one-letter name."
猜你喜欢
  • 2013-07-25
  • 2013-11-05
  • 1970-01-01
  • 2017-08-15
  • 1970-01-01
  • 1970-01-01
  • 2020-08-27
  • 2011-09-05
  • 1970-01-01
相关资源
最近更新 更多