Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the Strut's 2 equivalent of the Struts 1 logic:empty tag?

Tags:

struts2

What's the Strut's 2 equivalent of the Struts 1 logic:empty tag?

<logic:empty name="foo">
   Foo is null or the empty string
</logic:empty>

Thanks.

like image 476
tpdi Avatar asked Dec 10 '22 11:12

tpdi


2 Answers

There is no struts2 tag to do this, there are more possibilities and more expressiveness with OGNL than the struts1 tags, however there does not seem to be a way to check a string for both null and the empty string as succinctly.

The following works:

<s:if test="(myString == null || myString.equals(''))">
  myString is blank or null
</s:if>
<s:else>
  The value is <s:property value="myString"/>
</s:else>

The test relies on short circuting, so test of null can not be changed with the test for equality.

If the need to test for this comes up often there may be a design issue. With proper validation in place you should not have uninitialized objects for which the view depends but I suppose there are always exceptions.

like image 125
Quaternion Avatar answered Feb 23 '23 01:02

Quaternion


To add to Quaternion's answer:

  1. You can always add a method in your action for checking particular conditions, eg MyAction.isPropertyXEmpty() an put it in the <if test=...> condition

  2. Recall that in Struts2 properties are more type-rich/expressive than in Struts. Don't use Strings if another type is more appropiate. And you can initialize them to non-null values (eg., empty strings) to avoid the null problems.

like image 41
leonbloy Avatar answered Feb 23 '23 00:02

leonbloy