gpt4 book ai didi

jsf - Tomcat 8(和 9)强制行为,空字符串被错误地设置为空字符串

转载 作者:行者123 更新时间:2023-12-01 18:04:01 25 4
gpt4 key购买 nike

我刚刚迁移到 Tomcat 8。我曾经使用系统属性 org.apache.el.parser.COERCE_TO_ZERO=false 因此空字符串、数字、 bool 值等被视为

在 Tomcat 8、EL 3.0 中,它应该是默认值,但实际上它在 JSF 端将 null 字符串转换为空字符串 ""

它应该是 bug它应该被纠正,但我无法让它在 TomEE 快照(Tomcat 8.0.27.0,MyFaces 2.2.8)中工作。

最佳答案

这是 EL 3.0 规范中的一个错误。自 EL 3.0 以来,由于过度热心修复 JSP spec issue 184 , null 在调用模型值 setter 之前将被强制返回空字符串。基本上,javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL不再有任何影响。另请参阅JSF issue 3071EL spec issue 18 .

基本上,要解决这个问题,您需要提供一个自定义 EL 解析器,或者将 EL 实现(或者只是整个服务器,因为它是实际提供开箱即用的 EL 的服务器)替换/升级到具有错误修复的版本。 Tomcat issue 57309您链接的内容与这个特定的 EL 3.0 不必要的从 null 到空字符串的强制转换问题无关,它仅与 Tomcat 对自定义 EL 解析器的无知有关。

您可以通过两种方式解决此问题:

  1. 提供如下所示的自定义 EL 解析器。确保您使用 Tomcat 8.0.16 或更高版本,如您发现的 Tomcat 问题报告中所指定。

     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.
    }

    }

    为了让它运行,请在faces-config.xml中进行如下注册:

     <application>
    <el-resolver>com.example.EmptyToNullStringELResolver</el-resolver>
    </application>
    <小时/>
  2. 或者,切换到 Oracle EL。他们已经将其修复在 version 3.0.1 b05该版本自 2014 年 7 月 7 日起可用(只需选择 newest,即今天起的 3.0.1-b10)。

    只需将 javax.el.jar 文件放入 /WEB-INF/lib 中,并将以下上下文参数添加到 web.xml 中为了指示 MyFaces 使用 Oracle EL 实现:

     <context-param>     
    <param-name>org.apache.myfaces.EXPRESSION_FACTORY</param-name>
    <param-value>com.sun.el.ExpressionFactoryImpl</param-value>
    </context-param>

    如果您使用 Mojarra,请使用参数名称 com.sun.faces.expressionFactory

由于我自己也一时感到困惑,我在博客上澄清了这一点:The empty String madness .

###另请参阅:

关于jsf - Tomcat 8(和 9)强制行为,空字符串被错误地设置为空字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33334192/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com