2011-05-08 109 views
1

後,我總是得到以下錯誤,當我的表格無法驗證:「org.apache.jasper.JasperException」失敗的表單驗證

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> 
+0

請發佈jsp頁面表單標籤 – 2011-05-08 17:44:36

+0

我添加了上面表單的JSP源代碼。 – Pieter 2011-05-08 18:24:40

回答

0

這是因爲,在你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方法,你不需要處理創建表單支持對象自己的。

+0

我不再收到服務器錯誤,但用戶沒有出現表單驗證錯誤。我確實在約束條件上設置了消息:'@NotEmpty(message =「請輸入您的名字。」)'和'@Size(min = 2,message =「您不太可能擁有單字母名稱。 「' – Pieter 2011-05-09 10:00:36