问题
For converting String values in JSF bean validation, which converter will be used?
Like for numeric values we use <f:convertNumber>
and for date we use <f:convertDataTime>
.
My code snipest are as follows:
JSF page:
<h:inputText id="Name" label="Name" value="#{employee.eName}"/>
<h:message for="Name" styleClass="errorMessages"/>
Bean class:
public class Employee implements Serializable{
@NotNull @Size(min = 3, max = 30)
String eName;
}
回答1:
String
doesn't have a default converter. Request parameters are String
already.
If you intend to hook a custom converter on String
, use
@FacesConverter(forClass=String.class)
public class StringConverter implements Converter {
// ...
}
The only use case I've seen for this is to set them to null
instead of empty string, but that can also be achieved by setting the following context param in web.xml
.
<context-param>
<param-name>javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL</param-name>
<param-value>true</param-value>
</context-param>
This way the @NotNull
annotation will be triggered when you submit an empty string. Otherwise you have to use Hibernate-specific @NotBlank
instead.
See also
- JSF 2.0 tutorial with Eclipse and Glassfish - chapter "Prepare JSF project" point 6
来源:https://stackoverflow.com/questions/5910606/is-there-a-converter-for-string