问题
Is it possible to set the maxlength attribute of an <h:inputText> tag to be the same as its entity class @Size max value?
For example, given:
@Size(max = 50)
String name;
...somehow automatically/programmatically set the number 50 in:
<h:inputText id="name" maxlength="50" .../>
回答1:
This can be done using Apache MyFaces Extensions Validator, ExtVal.
This link and this link explain how to add ExtVal into your project. You need to add the required jars and to add the annotation @MaxLength above the field, and you will see the maxLength inside the html input.
来源:https://stackoverflow.com/questions/8575770/automatically-copy-inputtext-maxlength-from-entity-class-size-value