0

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>

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.

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

Roman C
  • 49,761
  • 33
  • 66
  • 176
user155631
  • 85
  • 2
  • 9

1 Answers1

0

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.

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.

Roman C
  • 49,761
  • 33
  • 66
  • 176
  • Thanks, testing my code shows that it does work though, in that I get the same output with or without the "!= null", in both the null and the non-null case. – user155631 Mar 07 '14 at 11:34
  • Now it's time to accept and upvote the answer. The checkbox is on the left of the answer. – Roman C Jul 20 '22 at 18:25