Struts2 s:if 测试非空值

2023-09-24Java开发问题
0

本文介绍了Struts2 s:if 测试非空值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我想使用 struts2 标记测试非空值.如果我使用以下内容,这将有效:

I am wanting to test for a non-null value using a struts2 tag. This works if I use the following:

<s:if test="myObject.myField != null">..stuff..</s:if>

如果我错过了非空部分,它似乎也可以工作,然后这样做:

It also seems to work if I miss out the not-null part, and just do:

<s:if test="myObject.myField">..stuff..</s:if>

但是,当 myFieldString 时,这不再有效.我更喜欢第二种形式,因为它更简洁,而且看起来不像将代码放在我的表示层中.但我不想使用它,如果它是一个未记录的功能,不知何故意外工作,正如它不适用于字符串的事实所暗示的那样.

However, when myField is a String, this no longer works. I prefer the second form as it is more concise, and seems less like putting code in my presentation layer. But I don't want to use it if it's an undocumented feature which is somehow working by accident, as suggested by the fact that it doesn't work for strings.

所以我的问题是,在上述测试中错过 "!= null" 是否可以?

So my question is, is it ever ok to miss out "!= null" in the above test?

推荐答案

使用 <s:if test="whatsoever whatsoever 是一个应该计算的 OGNL 表达式Boolean 值,如果你希望标签正常工作.如果你的字段是 boolean 类型,没关系,在其他情况下它就不起作用.

Using <s:if test="whatsoever the whatsoever is an OGNL expression that should evaluate to Boolean value if you want the tag is working right. If your field is of boolean type it's ok, in other case it's just doesn't work.

另外,在 if tag 类型被定义为Boolean,所以它不是原始的boolean,但值可以是原始的boolean.因此,在评估测试用例时使用不同类型时要小心.

Also, in the documentation page for the if tag the type is defined as Boolean, so it's not primitive boolean but the value could be primitive boolean. So, be careful when using different types when evaluating the test case.

这篇关于Struts2 s:if 测试非空值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

如何使用 JAVA 向 COM PORT 发送数据?
How to send data to COM PORT using JAVA?(如何使用 JAVA 向 COM PORT 发送数据?)...
2024-08-25 Java开发问题
21

如何使报表页面方向更改为“rtl"?
How to make a report page direction to change to quot;rtlquot;?(如何使报表页面方向更改为“rtl?)...
2024-08-25 Java开发问题
19

在 Eclipse 项目中使用西里尔文 .properties 文件
Use cyrillic .properties file in eclipse project(在 Eclipse 项目中使用西里尔文 .properties 文件)...
2024-08-25 Java开发问题
18

有没有办法在 Java 中检测 RTL 语言?
Is there any way to detect an RTL language in Java?(有没有办法在 Java 中检测 RTL 语言?)...
2024-08-25 Java开发问题
11

如何在 Java 中从 DB 加载资源包消息?
How to load resource bundle messages from DB in Java?(如何在 Java 中从 DB 加载资源包消息?)...
2024-08-25 Java开发问题
13

如何更改 Java 中的默认语言环境设置以使其保持一致?
How do I change the default locale settings in Java to make them consistent?(如何更改 Java 中的默认语言环境设置以使其保持一致?)...
2024-08-25 Java开发问题
13