我已经从Java EE 6迁移到Java EE 7,现在使用JSF 2.2,上下文参数INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL
似乎不起作用。在JSF 2.1中我将它设置为“true”并且它完美地工作,但现在我总是得到空字符串。
<context-param>
<param-name>javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL</param-name>
<param-value>true</param-value>
</context-param>
有人可以说些什么吗?
答案 0 :(得分:6)
使用最新的Mojarra-2.2.5的glassfish 4也是如此 以及Wildfly 8 Final。 。 。我已经看到了关于此的多个错误报告,Manfried Riem说“已经确定这是一个EL问题并且已经修复了EL实现以修复此问题”,但不确定这是否意味着更新Mojarra修复它,因为它不在glassfish中我也更新了el,但也没有用。
答案 1 :(得分:3)
不幸的是,Glassfish 4中似乎存在一个错误。
请参阅:
INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL doesn't work at all
和
答案 2 :(得分:1)
Apache el实现执行空字符串(或0 int值)。您可以在org.apache.el.parser.AstValue类中找到它:
public void setValue(EvaluationContext ctx, Object value)
throws ELException {
Target t = getTarget(ctx);
ctx.setPropertyResolved(false);
ELResolver resolver = ctx.getELResolver();
// coerce to the expected type
Class<?> targetClass = resolver.getType(ctx, t.base, t.property);
if (COERCE_TO_ZERO == true
|| !isAssignable(value, targetClass)) {
resolver.setValue(ctx, t.base, t.property,
ELSupport.coerceToType(value, targetClass));
} else {
resolver.setValue(ctx, t.base, t.property, value);
}
if (!ctx.isPropertyResolved()) {
throw new PropertyNotFoundException(MessageFactory.get(
"error.resolver.unhandled", t.base, t.property));
}
}
您可以将COERCE_TO_ZERO设置为false(-Dorg.apache.el.parser.COERCE_TO_ZERO = false)。
或使用其他el impl:
<dependency>
<groupId>org.glassfish.web</groupId>
<artifactId>el-impl</artifactId>
<version>2.2</version>
</dependency>
并设置context-param:
servletContext.setInitParameter("com.sun.faces.expressionFactory", "com.sun.el.ExpressionFactoryImpl");
那是el-api方面。
另一方面是JSF。你必须为JSF设置这个context-param:
servletContext.setInitParameter("javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL", "true");
抱歉我的英文!
答案 3 :(得分:0)
现在事情变得更加令人困惑。 我正在使用JSF 2.2.8-SNAPSHOT,虽然在JSF bean验证期间该值被解释为null,但实际值集是一个空String。这意味着其他组件正在进行验证,例如JPA失败长度验证,因为String值为“”。
由于这显然需要一个spec change,我不会很快就会想到这一点。 jira还包含此post中描述的解决方法。
聚苯乙烯。这可能是正确的行为,因为documentation声明null被传递给bean验证框架,但这根本不直观。
答案 4 :(得分:0)
应用服务器有一个JVM属性,在类似情况下帮助了我。请参阅Work around for faulty INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL in Mojarra JSF 2.1
答案 5 :(得分:-1)
我在下面尝试了并且得到了工作。我们需要在web.xml
添加此条目<context-param>
<param-name>javax.faces.VALIDATE_EMPTY_FIELDS</param-name>
<param-value>true</param-value>
</context-param>