至於說
我想使用JSTL檢查任何錯誤
只要使用(這只是工作於Spring MVC 2.5 - 不可移植的Spring MVC的3.0 雖然我假設它是requestScope ['bindingResult。<COMMAND_NAME_GOES_HERE> .allErrors'])
<c:if test="${not empty requestScope['org.springframework.validation.BindingResult.<COMMAND_NAME_GOES_HERE>'].allErrors}">
An Error has occured!!!
</c:if>
記住默認命令名稱是The 不合格的命令類名的第一個字母小寫。通知波紋管的命令名稱是寵物
private PetValidator petValidator = new PetValidator();
@RequestMapping(method.RequestMethod.POST)
public void form(Pet command, BindingResult bindingResult) {
if(petValidator.validate(command, bindingResult)) {
// something goes wrong
} else {
// ok, go ahead
}
}
所以你的形式應該看起來像
<!--Spring MVC 3.0 form Taglib-->
<form:form modelAttribute="pet">
</form:form>
<!--Spring MVC 2.5 form Taglib-->
<form:form commandName="pet">
</form:form>
除非您使用@ModelAttribute
@RequestMapping(method.RequestMethod.POST)
public void form(@ModelAttribute("command") Pet command, BindingResult bindingResult) {
// same approach shown above
}
這樣,你的表單應該看起來像
<!--Spring MVC 3.0 form Taglib-->
<form:form modelAttribute="command">
</form:form>
<!--Spring MVC 2.5 form Taglib-->
<form:form commandName="command">
</form:form>