未显示 Hibernate 验证消息

Hibernate Validation Message Not Displayed

我已经准备了一个应用程序,可以使用

进行表单验证

Spring MVC 和 Hibernate。

我在我的 'salary' 字段上添加了 @NotNull 约束,并带有一条消息

'Please enter proper value',但是该消息没有显示

我该如何解决这个问题?

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/";

    }
}

你需要做的是

在你的控制器中:

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

然后在您的 jsp 中使用 <form:error path="person">。在这里,在 path 中,您将要绑定的属性放入控制器中。 有关更多信息,请查看此 link

请将 'salary' 的数据类型从 int 更改为 Integer。您需要一个包装器 class 才能使此验证生效。你可以参考这个http://forum.spring.io/forum/spring-projects/web/80192-validation-empty-int-field