2014-09-24 50 views
0

我創建了一個Spring MVC項目,但窗體的錯誤不顯示。爲什麼?
我試圖創建一個簡單的form.This是我Project.The項目的作品,但在JSP中的錯誤都沒有dispayed..Where我纔出問題的代碼?謝謝春天錯誤表單不顯示

Student.java

package coreservlets; 

import javax.validation.constraints.NotNull; 
import javax.validation.constraints.Size; 

public class Student { 

@Size(min=4,max=5,message="Size wrong") 
@NotNull 
private String name; 
@Size(min=4,max=5,message="Size wrong") 
@NotNull 
private Integer age; 
@Size(min=4,max=5,message="Size wrong") 
@NotNull 
private Integer id; 

public String getName(){return name;}//getName 

public void setName(String name){this.name=name;}//setName 

public Integer getAge(){return age;}//getAge 

public void setAge(Integer age){this.age=age;}//setAge 

public Integer getId(){return id;}//getId 

public void setId(Integer id){this.id=id;}//setId 

}//Student 

StudentController.java

package coreservlets; 

import javax.validation.Valid; 

import org.springframework.stereotype.Controller; 
import org.springframework.ui.ModelMap; 
import org.springframework.web.bind.annotation.ModelAttribute; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 
import org.springframework.web.servlet.ModelAndView; 

@Controller 
public class StudentController { 

@RequestMapping(value="/",method=RequestMethod.GET) 
public ModelAndView student(){ 

    return new ModelAndView("student", "command", new Student()); 

}//student 

@RequestMapping(value="/addStudent", method=RequestMethod.POST) 
public String addStudent(@ModelAttribute @Valid Student student,ModelMap model){ 

    model.addAttribute("name",student.getName()); 
    model.addAttribute("age",student.getAge()); 
    model.addAttribute("id",student.getId()); 

    return "result"; 
}//addStudent 

}//StudentController 

student.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" 
pageEncoding="ISO-8859-1"%> 
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4 /loose.dtd"> 
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 
<title>Spring MVC Form Handling</title> 
</head> 
<body> 
<h2>Student Information</h2> 
<form:form method="post" action="/SpringMVCFormExample/addStudent" > 

<table> 
<tr> 
    <td><form:label path="name">Name</form:label></td> 
    <td><form:input path="name"/></td> 
    <form:errors path="name" /> 
</tr> 

<tr> 
    <td><form:label path="age">Age</form:label></td> 
    <td><form:input path="age"/></td> 
    <form:errors path="age" /> 
</tr> 

<tr> 
    <td><form:label path="id">Id</form:label></td> 
    <td><form:input path="id"/></td> 
    <form:errors path="id" /> 
</tr> 

<tr> 
    <td colspan="2"> 
     <input type="submit" value="Submit"/> 
    </td> 
</tr> 

</table> 
</form:form> 

result.jsp中

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" 
pageEncoding="ISO-8859-1"%> 
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html/loose.dtd"> 
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 
<title>Spring MVC Form Handling</title> 
</head> 
<body> 
<h2>Submitted Student Information</h2> 
<table> 
<tr> 
    <td>Name</td> 
    <td>${name}</td> 
</tr> 

<tr> 
    <td>Age</td> 
    <td>${age}</td> 
</tr> 

<tr> 
    <td>Id</td> 
    <td>${id}</td> 
</tr> 
</table> 

</body> 
</html> 

的web.xml

<?xml version="1.0" encoding="UTF-8"?> 
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> 

<display-name>Spring MVC Form</display-name> 

<servlet> 
    <servlet-name>SpringMVCForm</servlet-name> 
    <servlet-class> 
     org.springframework.web.servlet.DispatcherServlet 
    </servlet-class> 
    <load-on-startup>1</load-on-startup> 
</servlet> 

<servlet-mapping> 
    <servlet-name>SpringMVCForm</servlet-name> 
    <url-pattern>/</url-pattern> 
</servlet-mapping> 


</web-app> 

SpringMVCForm-servlet.xml中

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:context="http://www.springframework.org/schema/context" 
xmlns:mvc="http://www.springframework.org/schema/mvc" 
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd 
    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd"> 

    <context:component-scan base-package="coreservlets" /> 

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
    <property name="prefix" value="/WEB-INF/jsp/" /> 
    <property name="suffix" value=".jsp" /> 

+1

而且又何必呢? YOu總是顯示'result'頁面。在'@ ModelAttribute'註釋參數後面添加一個'BindingResult'類型的參數後,檢查是否有錯誤,如果是這樣的話,再次顯示同一頁面,如果不行的話。 – 2014-09-24 10:09:28

+0

我想要[link] http://postimg.org/image/grajjtiej/ 錯誤以紅色顯示 – Alex 2014-09-24 10:14:54

回答

0

以下代碼將在發生驗證錯誤時將用戶返回到student表單。如果沒有驗證錯誤,result模板將顯示

@RequestMapping(value="/addStudent", method=RequestMethod.POST) 
public String addStudent(@Valid Student student, BindingResult errors, Model model){ 
    if (errors.hasErrors()) { 
     return "student"; 
    } 
    model.addAttribute("name",student.getName()); 
    model.addAttribute("age",student.getAge()); 
    model.addAttribute("id",student.getId()); 

    return "result"; 
} 

你需要確保BindingResult errors之後@ModelAttribute @Valid Student student

退房thisthis教程更多的信息準確定位。

另一方面,您應該在成功發佈POST後重定向用戶。有關更多詳細信息,請參閱this

+0

如果我寫你的代碼,我有這個錯誤: HTTP狀態500 - 請求處理失敗;嵌套異常是org.springframework.web.bind.annotation.support.HandlerMethodInvocationException:無法調用處理程序方法[public java.lang.String coreservlets.StudentController.addStudent(coreservlets.Student,org.springframework.ui.ModelMap,org.springframework .validation.BindingResult)];嵌套的異常是java.lang.IllegalStateException:聲明的Errors/BindingResult參數沒有先前的模型屬性。檢查你的處理器方法簽名! – Alex 2014-09-24 10:35:31

+0

@Alex檢查我的更新回答 – geoand 2014-09-24 11:03:31

0

如果設置在模型層,你的驗證如果你想處理在客戶端級別,你可以jquery.validate.js驗證

@RequestMapping(value="/addStudent", method=RequestMethod.POST) 
    public String addStudent(@ModelAttribute @Valid Student student,ModelMap model,BindingResult errors){ 
    if (errors.hasErrors()) { 
       List<FieldError> lerr = errors.getFieldErrors(); 
       for (FieldError err: lerr) { 
        // manage the errorrs 
       } 
      } 
    model.addAttribute("name",student.getName()); 
    model.addAttribute("age",student.getAge()); 
    model.addAttribute("id",student.getId()); 

    return "result"; 
    } 

:應該使用BindingResult

+0

student.jsp中的標記不顯示student.java中的註釋錯誤消息。爲什麼? – Alex 2014-09-24 10:39:13

+0

你需要創建一個擴展'Validator'的StudentValidator,看看這個教程http://examples.javacodegeeks.com/enterprise-java/spring/mvc/spring-mvc-form-errors-tag-example/ – 2014-09-24 10:48:07

1

在您的方法中使用BindingResult類作爲參數。 你的方法應該是這樣的

公共字符串傳遞addStudent(@ModelAttribute @Valid學生的學生,ModelMap模型,BindingResult結果)

Spring MVC的將結合其屬性後驗證由@Valid註解模型對象來自使用Spring的表單標籤的JSP表單的輸入。任何約束違規將在BindingResult對象中暴露爲錯誤,因此我們可以像這樣檢查控制器方法中的違規:

if(result。hasErrors()){

// form validation error 

}其他{

// form input is ok 

}

+0

我想在標籤中顯示student.jsp中的錯誤。 – Alex 2014-09-24 10:32:57

+0

標記呈現bindingResult的錯誤。要了解更多信息,您可以通過此鏈接http://www.java4s.com/spring-mvc/spring-mvc-annotation-jsr-303-bean-validation-with-valid-example/ – 2014-09-24 10:39:50