Struts2 s:如果测试非空值
我想使用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
标签类型定义为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.