问题描述
我想使用 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>
但是,当 myField
是 String
时,这不再有效.我更喜欢第二种形式,因为它更简洁,而且看起来不像将代码放在我的表示层中.但我不想使用它,如果它是一个未记录的功能,不知何故意外工作,正如它不适用于字符串的事实所暗示的那样.
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 测试非空值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!