2016-08-16 153 views
0

我準備了一個應用程序,使用表單驗證使用休眠驗證消息不顯示

Spring MVC和Hibernate。

我已經加入了@NotNull約束我的「工資」領域,與消息

「請輸入正確的價值」,但是沒有得到顯示該消息

我該如何解決這個??

messages.properties:

NotNull.person.salary =不能爲空

模式

@Entity 
public class Person { 

    @Id  
    @GeneratedValue(strategy=GenerationType.AUTO) 
    private Integer id; 
    private String name;  
    private String address; 
    @Valid @NumberFormat(style=Style.CURRENCY) @NotNull(message="Please enter a value") 
    private int salary; 
    private String gender; 

form:form action="/MainAssignment3/save" method="post" modelAttribute="p"> 
     <!-- <input type="hidden" name="id" > --> 
     name : <input type="text" name="name"> <br/> 
     address : <input type="text" name="address" ><br/> 
     gender : <input type="text" name="gender" ><br/> 
     salary : <input type="text" name="salary" ><br/> 
     <input type="submit"value="submit"> 
    </form:form> 

AddPerson.jsp

<head> 
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 
<title>Add Employee</title> 
</head> 
<body> 
<springForm:form action="/MainAssignment3/save" method="POST" 
     commandName="person"> 

     <table style="border: thin; border: ridge;"> 

      <tr> 
       <td>name</td> 
       <td><springForm:input path="name" id="name" 
         placeholder="Enter Name" /></td> 

      </tr> 
      <tr> 
       <td>address</td> 
       <td><input type="text" name="address" placeholder="Enter Address" ></td> 
      </tr> 
      <tr> 
       <td>gender</td> 
       <td><springForm:input path="gender" id="gender" placeholder="Enter Gender" /></td> 

      </tr> 
      <tr> 
       <td>salary</td> 
       <td><input type="text" name="salary" placeholder="Enter Salary" ></td> 
       <td><springForm:errors path="salary" /></td> 


       <td style="padding-left: 110px;"><input type="submit"value="submit"></td> 
      </tr> 

     </table> 
    </springForm:form> 

messages.properties

NotNull.p.salary =無效

//////////////////// //////控制器/////////////////

@RequestMapping(value = "/newPerson", method = RequestMethod.GET) 
public ModelAndView newPerson(ModelAndView model) throws IOException { 
    @SuppressWarnings("unused") 
    Person p = new Person(); 
    model.addObject("person", new Person()); 
    model.setViewName("AddPerson"); 

    return new ModelAndView("AddPerson", "person", new Person()); 

} 

@RequestMapping(value="/save") 
public String save(@ModelAttribute @Valid Person p, BindingResult result) { 

    if(result.hasErrors()) 
    { 

     return "AddPerson"; 
    } 
    else 
    {  
    ps.save(p); 

    return "redirect:http://localhost:8080/MainAssignment3/"; 

    } 
} 
+0

是否有人對此有什麼建議? –

+0

willl help?我試圖把它在我的JSP,字段下面:​​<彈簧扣:錯誤路徑=「名稱」 /> –

+0

我已經編輯和更新我的文件 –

回答

0

你需要做的是

在你的控制器

public String save(@ModelAttribute @Valid person) {//instructions.. } 

然後在你的JSP使用<form:error path="person">。在這裏,在path你把你在你的控制器綁定屬性。 欲瞭解更多信息,請查看link

+0

我做在jsp這種變化:​​<彈簧扣:錯誤路徑=「人」 /> –

+0

現在,我上線得到一個錯誤:​​<彈簧扣:錯誤路徑=「人」 /> \t我有更新我的問題與我的控制器 –

+0

錯誤解決。但是,我現在得到一個NumberFormatException異常,當我不把任何價值的「工資」字段在運行時。爲了測試表單驗證,我嘗試將薪資列留空,因爲我已對其應用@NotNull驗證。我收到了一個異常:無法將屬性值[java.lang.String]轉換爲所需的類型[int];嵌套異常是java.lang.NumberFormatException:對於輸入字符串:「」 –