Java jsf 表达式语言中的空检查

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2207266/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 04:44:59  来源:igfitidea点击:

null check in jsf expression language

javajsfel

提问by crazyTechie

Please see this Expression Language

请看这个表达语言

styleClass="#{obj.validationErrorMap eq null ? ' ' :  
     obj.validationErrorMap.contains('key')?'highlight_field':'highlight_row'}"

Even if the map is null, highlight_rowstyle is getting applied.

即使地图为空,highlight_row也会应用样式。

So I changed to

所以我改为

styleClass="#{empty obj.validationErrorMap ? ' ' :  
     obj.validationErrorMap.contains('key')?'highlight_field':'highlight_row'}"

Even then, highlight_rowis getting applied.
if the map is empty OR nullI dont want any style to be applied.

即便如此,highlight_row正在得到应用。
如果地图是empty OR null我不想应用任何样式。

Any help? and reasons for this behaviour?

有什么帮助吗?以及这种行为的原因?

采纳答案by BalusC

Use empty(it checks both nullness and emptiness) and group the nested ternary expression by parentheses (EL is in certain implementations/versions namely somewhat problematic with nested ternary expressions). Thus, so:

使用empty(它检查空性和空性)并通过括号对嵌套的三元表达式进行分组(EL 在某些实现/版本中,即嵌套三元表达式有些问题)。因此,所以:

styleClass="#{empty obj.validationErrorMap ? ' ' :  
 (obj.validationErrorMap.contains('key') ? 'highlight_field' : 'highlight_row')}"

If still in vain (I would then check JBoss EL configs), use the "normal" EL approach:

如果仍然无效(然后我会检查 JBoss EL 配置),请使用“正常”EL 方法:

styleClass="#{empty obj.validationErrorMap ? ' ' :  
 (obj.validationErrorMap['key'] ne null ? 'highlight_field' : 'highlight_row')}"

Update: as per the comments, the Mapturns out to actually be a List(please work on your naming conventions). To check if a Listcontains an item the "normal" EL way, use JSTLfn:contains(although not explicitly documented, it works for Listas well).

更新:根据评论,Map结果实际上是一个List(请遵守您的命名约定)。要检查 a 是否List包含“正常” EL 方式的项目,请使用JSTLfn:contains(虽然没有明确记录,但它也适用List)。

styleClass="#{empty obj.validationErrorMap ? ' ' :  
 (fn:contains(obj.validationErrorMap, 'key') ? 'highlight_field' : 'highlight_row')}"