1

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;
}
Adnan
  • 4,517
  • 15
  • 44
  • 54

1 Answers1

4

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

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555