spring validation: cleanest way to makeup accompanying labels of the validated input

StackOverflow https://stackoverflow.com/questions/4313357

  •  29-09-2019
  •  | 
  •  

문제

I'm validating the input field that's bound to path. I'm using hibernate-validator 4 for this. Now I'd like to highlight the age label so it pops out of the page (bold, red colour etc.). However I'm wondering what the cleanest way to do this is.

<spring:hasBindErrors name="*"/> seems to be for the whole form object instead of for a specific field. Any input is appreciated.

도움이 되었습니까?

해결책

Spring provides special jsp tags for forms, which support this task (highlighing in case of error):

For example this jsp

...
<%@ taglib prefix='form' uri='http://www.springframework.org/tags/form'%>
...
<form:form method="post"
           commandName="myCommand">
  <form:input path="name"
              cssClass="normalLayout"
              cssErrorClass="normalLayout error"/>
  <form:errors path="name"
               cssClass="errorMessage"/>
</form:form>
...

In this case: the input field uses the css class "normalLayout" if every thing is ok, and the css classes "normalLayout" and "name" if there is a validation error for the field.

form:errors is to print the error message generated while validation.

@see http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/view.html#view-jsp-formtaglib

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top