我刚刚迁移到了Tomcat 8。以前我使用系统属性org.apache.el.parser.COERCE_TO_ZERO=false
,这样空字符串、数字、布尔值等会被视为null
。
在Tomcat 8中,EL 3.0应该是默认的,但实际上它将null
字符串转换为空字符串""
在JSF方面。
这应该是一个错误,应该得到纠正,但我无法在TomEE快照(Tomcat 8.0.27.0,MyFaces 2.2.8)中使其工作。
我刚刚迁移到了Tomcat 8。以前我使用系统属性org.apache.el.parser.COERCE_TO_ZERO=false
,这样空字符串、数字、布尔值等会被视为null
。
在Tomcat 8中,EL 3.0应该是默认的,但实际上它将null
字符串转换为空字符串""
在JSF方面。
这应该是一个错误,应该得到纠正,但我无法在TomEE快照(Tomcat 8.0.27.0,MyFaces 2.2.8)中使其工作。
这是 EL 3.0 规范中的一个错误。自从 EL 3.0 开始,由于过度热衷于修复 JSP spec issue 184,在模型值设置器被调用之前,null
将被强制转换为空字符串。基本上,javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL
不再起作用了。另请参见 JSF issue 3071 和 EL spec issue 18。
Provide a custom EL resolver like below. Make sure that you use Tomcat 8.0.16 or newer, as specified in the Tomcat issue report you found.
public class EmptyToNullStringELResolver extends ELResolver {
@Override
public Class<?> getCommonPropertyType(ELContext context, Object base) {
return String.class;
}
@Override
public Object convertToType(ELContext context, Object value, Class<?> targetType) {
if (value == null && targetType == String.class) {
context.setPropertyResolved(true);
}
return value;
}
@Override
public Iterator<FeatureDescriptor> getFeatureDescriptors(ELContext context, Object base) {
return null;
}
@Override
public Class<?> getType(ELContext context, Object base, Object property) {
return null;
}
@Override
public Object getValue(ELContext context, Object base, Object property) {
return null;
}
@Override
public boolean isReadOnly(ELContext context, Object base, Object property) {
return true;
}
@Override
public void setValue(ELContext context, Object base, Object property, Object value) {
// NOOP.
}
}
In order to get it to run, register as below in faces-config.xml
:
<application>
<el-resolver>com.example.EmptyToNullStringELResolver</el-resolver>
</application>
Or, switch to Oracle EL. They already fixed it in version 3.0.1 b05 which has been available since 7 July 2014 (just pick the newest one which is 3.0.1-b10 as of today).
Just drop javax.el.jar
file in /WEB-INF/lib
and add the below context param to web.xml
in order to instruct MyFaces to use the Oracle EL implementation instead:
<context-param>
<param-name>org.apache.myfaces.EXPRESSION_FACTORY</param-name>
<param-value>com.sun.el.ExpressionFactoryImpl</param-value>
</context-param>
In case you're using Mojarra, use param name com.sun.faces.expressionFactory
.
由于我自己有些疑惑,因此写了一篇博客来澄清:空字符串的疯狂之谜。
###另请参阅: