绑定到 String 属性的 h:inputText 提交空字符串而不是 null

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2203322/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 00:37:38  来源:igfitidea点击:

h:inputText which is bound to String property is submitting empty string instead of null

stringjsftomcatnullel

提问by ThreeFingerMark

I have a JSF 2.0 application on Tomcat with many <h:inputText>fields to input data in my database. Some fields are not required.

我在 Tomcat 上有一个 JSF 2.0 应用程序,其中有许多<h:inputText>字段可以在我的数据库中输入数据。某些字段不是必需的。

<h:inputText value="#{registerBean.user.phoneNumber}" id="phoneNumber">
    <f:validateLength maximum="20" />
</h:inputText>

When the user leave this field empty JSF sets empty string ""instead of null.

当用户将此字段留空时,JSF 会设置空字符串""而不是null.

How can I fix this behavior without checking every String with

如何在不检查每个字符串的情况下修复此行为

if (string.equals("")) { string = null; }

回答by BalusC

You can configure JSF 2.x to interpret empty submitted values as null by the following context-paramin web.xml(which has a pretty long name, that'll also be why I couldn't recall it ;) ):

您可以配置JSF 2.x的解释由以下空提交的数据为空context-paramweb.xml(其中有一个很长的名字,这也将是为什么我不记得它;)):

<context-param>
    <param-name>javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL</param-name>
    <param-value>true</param-value>
</context-param>


For reference and for ones who are interested, in JSF 1.2 (and thus not 1.1 or older because it's by design not possible to have a Converterfor java.lang.String) this is workaroundable with the following Converter:

作为参考和感兴趣的人,在 JSF 1.2(因此不是 1.1 或更早版本,因为设计上不可能有Converterfor 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();
    }

}

...which needs to be registered in faces-config.xmlas follows:

...需要注册faces-config.xml如下:

<converter>
    <converter-for-class>java.lang.String</converter-for-class>
    <converter-class>com.example.EmptyToNullStringConverter</converter-class>
</converter>

In case you're not on Java 6 yet, replace submittedValue.empty()by submittedValue.length() == 0.

如果您还没有使用 Java 6,请替换submittedValue.empty()submittedValue.length() == 0.

See also

也可以看看

回答by ThreeFingerMark

i hope this is the right way to say that i can't find a solution for the Problem.

我希望这是正确的说法,我找不到问题的解决方案。

I have add the context-paramto my web.xmlbut it has no result. I use a Tomcat 6.0.24 Server with this two context-param: javax.faces.PROJECT_STAGE =Development javax.faces.VALIDATE_EMPTY_FIELDS = true

我已将其添加context-param到我的web.xml但没有结果。我使用 Tomcat 6.0.24 Server 进行这两个context-param: javax.faces.PROJECT_STAGE =开发javax.faces.VALIDATE_EMPTY_FIELDS = true