我準備了一個應用程序,使用表單驗證使用休眠驗證消息不顯示
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/";
}
}
是否有人對此有什麼建議? –
willl help?我試圖把它在我的JSP,字段下面:<彈簧扣:錯誤路徑=「名稱」 /> –
我已經編輯和更新我的文件 –