Is there a converter for String?

浪子不回头ぞ 提交于 2019-12-11 09:03:38

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!