context-param
次のようにして、空の送信された値をnullとして解釈するようにJSF 2.xを構成できますweb.xml
(これはかなり長い名前を持っているので、思い出せなかった理由でもあります;)):
<context-param>
<param-name>javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL</param-name>
<param-value>true</param-value>
</context-param>
Converter
参考までに、そして興味のある人のために、JSF 1.2では(したがって、設計上forを持つことができないため、1.1以前ではありませんjava.lang.String
)、これは次の方法で回避できますConverter
。
public class EmptyToNullStringConverter implements Converter {
public Object getAsObject(FacesContext facesContext, UIComponent component, String submittedValue) {
if (submittedValue == null || submittedValue.isEmpty()) {
if (component instanceof EditableValueHolder) {
((EditableValueHolder) component).setSubmittedValue(null);
}
return null;
}
return submittedValue;
}
public String getAsString(FacesContext facesContext, UIComponent component, Object modelValue) {
return (modelValue == null) ? "" : modelValue.toString();
}
}
...次のように登録する必要がありfaces-config.xml
ます:
<converter>
<converter-for-class>java.lang.String</converter-for-class>
<converter-class>com.example.EmptyToNullStringConverter</converter-class>
</converter>
まだJava6を使用していない場合は、に置き換えsubmittedValue.empty()
てsubmittedValue.length() == 0
ください。
も参照してください