Java spring框架表单验证

Tw1*_*sty 4 java forms validation model-view-controller spring

我正在使用spring framework 3.0.5.

我有一个表格:

<form:form method="POST" modelAttribute="postedEntry">
    Category: <form:select path="category" items="${menus}" itemValue="id" itemLabel="menuName"/><form:errors path="category"/>
    <br/>Title: <form:input path="title"/><form:errors path="title"/>
    <br/>Short Description: <form:textarea path="shortDesc"/><form:errors path="shortDesc"/>
    <br/>Body: <form:textarea path="body"/><form:errors path="body"/>
    <br/><input type="submit" value="POST IT!11"/>
</form:form>
Run Code Online (Sandbox Code Playgroud)

和一个域类条目:

public class Entry {
    private int id;
    private int category;
    private String title;
    private String shortDesc;
    private String body;
    private Date date;
//getters and setters
}
Run Code Online (Sandbox Code Playgroud)

和控制器:

@RequestMapping(value="/entryPost",method=RequestMethod.POST)
public String entryPost(@ModelAttribute("postedEntry") Entry entry,BindingResult result){
    entryValidator.validate(entry, result);
    if(result.hasErrors()){
        return "entryPost";
    }else{
        rusService.postEntry(entry);
        return "redirect:entryPost";
    }
}
Run Code Online (Sandbox Code Playgroud)

在我的服务对象中:

public void postEntry(final Entry entry){
    String sql = "INSERT INTO entries (category,title,shortDesc,body,date) VALUES(?,?,?,?,?)";
    jdbcTemplate.update(sql,new Object[]{entry.getCategory(),entry.getTitle(),entry.getShortDesc(),entry.getBody(),entry.getDate()});
}
Run Code Online (Sandbox Code Playgroud)

还有一个验证器:

public class EntryValidator implements Validator{
    public boolean supports(Class clazz){
        return Entry.class.isAssignableFrom(clazz);
    }
    public void validate(Object target,Errors errors){
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "category", "required.category","Category is required!");
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "title", "required.title", "Title is required!");
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "shortDesc","required.shortDesc", "Short description is required!");
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "body", "required.body", "Body is required!");
        Entry entry = (Entry) target;
        entry.setDate(new Date());
    }
}
Run Code Online (Sandbox Code Playgroud)

如果有人发送类别的字符串值而不是整数,它将在表单附近键入:无法将类型为java.lang.String的属性值转换为属性类别所需的int类型; 嵌套异常是org.springframework.core.convert.ConversionFailedException:无法将值类型"h"从类型java.lang.String转换为int类型; 嵌套异常是java.lang.NumberFormatException:对于输入字符串:"h"

但我不希望它打字.我怎么能以某种方式验证这个值并抛出我自己的消息?我尝试通过添加以下内容在我的验证器中进行检查:

int cat = entry.getCatrgory();
if(cat.equals("0"))
    errors.reject("invalid.category","The category is invalid!");
Run Code Online (Sandbox Code Playgroud)

但它没有用 - 它仍然抛出ConversionFailedException异常.

Alf*_*rio 5

您可以在资源包中使用typemismatch键.

例:

typeMismatch = This is not a number!
Run Code Online (Sandbox Code Playgroud)