解决 Mojarra JSF 2.1 中错误的 INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL [英] Work around for faulty INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL in Mojarra JSF 2.1

查看:13
本文介绍了解决 Mojarra JSF 2.1 中错误的 INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道有很多关于在 JSF2 中将空字符串转换为 null 的帖子.通常的处方是在 web.xml 中添加以下内容.

I know there are a number of posts about converting empty string to null in JSF2. The usual prescription is to add the following to web.xml.

<context-param>
  <description>Does not appear to work</description>
  <param-name>javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL</param-name>
  <param-value>true</param-value>
</context-param>

这似乎行不通 - 根本行不通.然后我创建了一个自定义字符串转换器来测试它是否有效.我明确地将它作为转换器添加到我的 inputText (否则它不会在空白时触发).

This just does not seem to work - at all. I then created a custom string converter to test if that would work. I explicitly added it as a converter to my inputText (otherwise it does not fire when blank).

当 INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL 设置为 true 时,转换器接收空值并且输入文本的设置器仍然接收".

When INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL is set to true the converter receives null and the setter for the input text still receives "".

当 INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL 设置为 false(或注释掉)时,转换器接收",输入文本的设置器接收"(即使在转换器返回 null 之后).

When INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL is set to false (or commented out) the converter receives "" and the setter for the input text receives "" (even after the converter returns null).

@FacesConverter(forClass=java.lang.String.class, value="emptyStringToNull")
public class StringConverter implements Converter, Serializable {
    private static final long serialVersionUID = -1121162636180944948L;
    public Object getAsObject(FacesContext context, UIComponent component, String value) {
        if (value == null || value.trim().isEmpty()) {
            return null;
        }
        return value;
    }

    public String getAsString(FacesContext context, UIComponent component, Object object) {
    if (object == null)
        return null;

    return object.toString();
    }
}

我已经尝试(但无济于事)在 getAsObject 中显式设置组件提交的值:

I've event tried (to no avail) to explicitly set the component submitted value in getAsObject:

if (component instanceof EditableValueHolder)
    ((EditableValueHolder) component).setSubmittedValue(null);

我正在使用 JBoss6(实际上是 6.1 的快照)和 JSF 2.1.1.

I'm using JBoss6 (a snapshot of 6.1 really) and JSF 2.1.1.

推荐答案

这不是 Mojarra 特有的.这是特定于 Tomcat 的(JBoss 使用 Tomcat 作为 servletcontainer).将以下 VM 参数添加到启动选项.

This is not Mojarra specific. This is Tomcat specific (JBoss uses Tomcat as servletcontainer). Add the following VM argument to startup options.

-Dorg.apache.el.parser.COERCE_TO_ZERO=false

根据我的经验,这个应该实际上只适用于 Number 属性(int、long 等),但是因为某个较晚的 Tomcat 6.0.x 版本(在至少在 6.0.20 之后)它似乎也被字符串破坏了,它依赖于上述 VM 参数.

To my experience, this one should actually only apply on Number properties (int, long, etc), however since a certain late Tomcat 6.0.x version (at least after 6.0.20) it seems to be broken for strings as well and it is relying on the above VM argument.

例如,在 GlassFish 3.x 上,它开箱即用.

On GlassFish 3.x for example it works perfectly fine out the box.

这篇关于解决 Mojarra JSF 2.1 中错误的 INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆