当期望值为NULL时,绑定到bean String类型的空表单输入字段被接收为空字符串("")。
这是一个老问题,似乎每个版本的Oracle EL、Apache或Tomcat都会重新创建这个问题。
@BalusC已经写了很多遍了。空弦疯狂,Tomcat 8(和9)胁迫行为,空字符串被错误地设置为空字符串。就是几个例子。
我尝试过以前所有的解决办法,但都没有成功。
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
发布于 2019-05-23 20:14:08
根据空弦疯狂的文章,EmptyToNullStringELResolver和Oracle不需要同时进行。对于Tomcat 8.0.16或更高版本,列出以下内容:
JF:添加
javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL=true上下文param。 ER:注册EmptyToNullStringELResolver,或者只做UE。 UE:迁移/升级到Oracle实现版本3.0.1B05或更高版本。
注意“交替”这个词。因此,只做一件或另一件是没问题的。
对于观察到的NullPointerException,当ResourceHandler需要流可能包含EL表达式的资源时,就会发生这种情况。默认情况下,这只发生在CSS资源上,以便能够支持如何将JSF图像资源引用为CSS背景图像url。ValueExpressionFilterInputStream表示MyFaces的内部类,用于在读取InputStream时计算EL表达式。然而,它显然有一个错误,它不期望返回null的EL表达式,而不是一个具体的对象。这个问题实际上与“空弦疯狂”的解决方案无关。尽管如此,它还是会发生的。您至少应该扫描您的自定义CSS文件,以查找错误返回null并修复它们的EL表达式。
https://stackoverflow.com/questions/56279977
复制相似问题