【问题标题】:Blank input String fields submitted as Empty Strings vs Null - JSF 2.3 MyFaces - Tomcat (TomEE)/9.0.12 (8.0.0-M1)作为空字符串与空字符串提交的空白输入字符串字段 - JSF 2.3 MyFaces - Tomcat (TomEE)/9.0.12 (8.0.0-M1)
【发布时间】:2019-10-10 07:24:51
【问题描述】:

当预期值为 NULL 时,绑定到 bean String 类型的空表单输入字段作为空字符串 ("") 接收。

这是一个老问题,似乎在每个版本的 Oracle EL 或 Apache EL 或 Tomcat 中都会重现。

@BalusC 已多次解决此问题。 Empty String MadnessTomcat 8 (and 9) coerce behaviour, null strings are incorrectly set as empty strings 是几个例子。

我已经尝试了所有以前的解决方法,但都没有成功。

someform.xhtml

<h:form>
  <h:outputText value="Some Input: " />
  <p:inputText value="#{someBean.stringVariable}" />

  <p:commandButton id="search-button"
                    value="Search"
                    actionListener="#{someBean.doSearch}" />
</form>

SomeBean.java

private String stringVariable;
// Setter & Getter
public void doSearch()
{
    if(stringVariable == null)
    {
       System.out.println("Input is NULL");
    }
    else if(stringVariable.equals(""))
    {
       System.out.println("Input is EMPTY STRING");    
    }
}

faces-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<faces-config
    xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_3.xsd"
    version="2.3">
        <application>
        <el-resolver>com.example.EmptyToNullStringELResolver</el-resolver>
    </application> 
    <application>
        <resource-handler>org.jepsar.primefaces.theme.jepsar.FontAwesomeResourceHandler</resource-handler>
        <resource-handler>org.omnifaces.resourcehandler.UnmappedResourceHandler</resource-handler>
    </application>  
    <application>
        <el-resolver>
           org.primefaces.application.exceptionhandler.PrimeExceptionHandlerELResolver
        </el-resolver>
    </application>
    <factory>
           <exception-handler-factory>
                 org.primefaces.application.exceptionhandler.PrimeExceptionHandlerFactory
           </exception-handler-factory>
    </factory> 
 </faces-config>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <context-param>
    <param-name>javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL</param-name>
    <param-value>true</param-value>
  </context-param> 
  <context-param>     
    <param-name>org.apache.myfaces.EXPRESSION_FACTORY</param-name>
    <param-value>com.sun.el.ExpressionFactoryImpl</param-value>   
  </context-param>
</web-app>

当我使用自定义 EmptyToNullStringELResolver 方法时,会抛出 NPE。

java.lang.NullPointerException
    at org.apache.myfaces.shared.resource.ValueExpressionFilterInputStream.read(ValueExpressionFilterInputStream.java:130)
    at java.io.InputStream.read(InputStream.java:179)
    at java.nio.channels.Channels$ReadableByteChannelImpl.read(Channels.java:385)
    at org.omnifaces.util.Utils.stream(Utils.java:397)
    at org.omnifaces.resourcehandler.UnmappedResourceHandler.handleResourceRequest(UnmappedResourceHandler.java:176)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:196)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at ...

将最新的 EL jar 从 Oracle 放入 WEB/lib 似乎没有任何效果。 (javax.el-3.0.1-b11.jar)

谢谢你, 泰德S

【问题讨论】:

  • EmptyToNullStringELResolver 和 Oracle EL 不需要同时完成。只做一个或另一个很好。你能试试吗? NPE 是否仍然被抛出并且在两种方法上都完全相同?堆栈跟踪至少表明在您的一个 CSS 资源中某处有一个 #{...} 表达式,它实际上解析为 null
  • 感谢@BalusC。仅使用 EmptyToNullStringELResolver 解决了 String -> NULL 问题。至于我不知道的实际问题,我的一个样式表引用了一个丢失的图像文件。请问堆栈跟踪中的什么提示您?再次,非常感谢。
  • 像往常一样从下往上读取类/方法名称..处理资源请求..流..读取值表达式..砰,有些东西是空的!

标签: jsf el tomcat9 tomee-8


【解决方案1】:

根据The Empty String Madness 文章,EmptyToNullStringELResolver 和 Oracle EL 不需要同时完成。对于 Tomcat 8.0.16 或更高版本,列出了以下内容:

JF:添加javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL=true 上下文参数。
ER:注册EmptyToNullStringELResolver,或者,只需执行UE
UE:迁移/升级到 Oracle EL 实施版本 3.0.1-b05 或更高版本。

注意“替代”这个词。因此,只做其中一个就可以了。

对于观察到的NullPointerException,当ResourceHandler 需要流式传输可能包含EL 表达式的资源时,就会发生这种情况。默认情况下,这仅发生在 CSS 资源上,以便能够支持How to reference JSF image resource as CSS background image urlValueExpressionFilterInputStream 代表 MyFaces 的内部类,用于在读取 InputStream 时评估 EL 表达式。但是,它显然有一个错误,即它不希望 EL 表达式返回 null 而不是具体对象。反过来,这个问题实际上与“空弦疯狂”的解决方案无关。尽管如此,它还是会发生。您至少应该扫描您的自定义 CSS 文件以查找错误返回 null 的 EL 表达式并修复它们。

【讨论】:

    猜你喜欢
    • 2015-01-23
    • 2019-05-14
    • 2013-03-26
    • 1970-01-01
    • 1970-01-01
    • 2014-11-12
    • 1970-01-01
    • 2012-02-29
    • 2011-12-06
    相关资源
    最近更新 更多