簡體   English   中英

JSF 2.2將空字符串提交的值解釋為null無效

[英]JSF 2.2 interpret empty string submitted values as null not working

我已經從Java EE 6遷移到Java EE 7,現在使用JSF 2.2,上下文參數INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL似乎不起作用。 在JSF 2.1中我將它設置為“true”並且它完美地工作,但現在我總是得到空字符串。

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

誰能說點什么呢?

使用最新的Mojarra-2.2.5以及Wildfly 8 Final的glassfish 4也是如此。 我已經看到了關於此的多個錯誤報告,Manfried Riem說“已經確定這是一個EL問題並且已經修復了EL實現以修復此問題”,但不確定這是否意味着更新Mojarra修復它,因為它不在glassfish中我也更新了el,但也沒有用。

不幸的是,Glassfish 4中似乎存在一個錯誤。

看到:

INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL完全不起作用

和:

空字符串為Null在2.2.0中無效

Apache el實現執行空字符串(或0 int值)。 您可以在org.apache.el.parser.AstValue類中找到它:

public void setValue(EvaluationContext ctx, Object value)
        throws ELException {
    Target t = getTarget(ctx);
    ctx.setPropertyResolved(false);
    ELResolver resolver = ctx.getELResolver();

    // coerce to the expected type
    Class<?> targetClass = resolver.getType(ctx, t.base, t.property);
    if (COERCE_TO_ZERO == true
            || !isAssignable(value, targetClass)) {
        resolver.setValue(ctx, t.base, t.property,
                ELSupport.coerceToType(value, targetClass));
    } else {
        resolver.setValue(ctx, t.base, t.property, value);
    }
    if (!ctx.isPropertyResolved()) {
        throw new PropertyNotFoundException(MessageFactory.get(
                "error.resolver.unhandled", t.base, t.property));            
    }
}

您可以將COERCE_TO_ZERO設置為false(-Dorg.apache.el.parser.COERCE_TO_ZERO = false)。

或使用其他el impl:

    <dependency>
        <groupId>org.glassfish.web</groupId>
        <artifactId>el-impl</artifactId>
        <version>2.2</version>      
    </dependency>

並設置context-param:

    servletContext.setInitParameter("com.sun.faces.expressionFactory", "com.sun.el.ExpressionFactoryImpl");

那是el-api方面。

另一方面是JSF。 你必須為JSF設置這個context-param:

servletContext.setInitParameter("javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL", "true");

對不起我的英語不好!

事情現在更令人困惑。 我正在使用JSF 2.2.8-SNAPSHOT,雖然在JSF bean驗證期間該值被解釋為null,但實際值集是一個空String。 這意味着其他組件正在進行驗證,例如JPA失敗長度驗證為String值為“”。

由於這顯然需要改變規格 ,我不會很快就會想到這一點。 JIRA也包含在此描述的解決方法

PS。 這可能是正確的行為,因為文檔聲明null被傳遞給bean驗證框架,但這根本不直觀。

在類似的情況下,應用程序服務器有一個JVM屬性幫助了我。 請參閱Mojarra JSF 2.1中的錯誤INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL的解決方法

我已經嘗試過下面的工作了。 我們需要在web.xml中添加此條目

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM