What is equivalent to strut 2 logic struts 1: empty tag?

What is the equivalent of Strut 2 to Struts 1 logic: an empty tag?

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

Thanks.

+4
source share
4 answers

There is no struts2 tag for this, OGNL has more features and more expressiveness than struts1 tags, however, there seems to be no way to check a string for either null or empty string as concisely.

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 is based on a short cycle, so the zero test cannot be changed using the equality test.

If the need to check for this often arises, a design problem may arise. With proper validation, you should not have uninitialized objects for which the view depends, but I suppose there are always exceptions.

+7
source

To add the answer to Quaternion:

  • You can always add a method to your action to check certain conditions, for example MyAction.isPropertyXEmpty() a put it in the condition <if test=...>

  • Recall that Struts2 properties are richer in type / expressive expressions than Struts. Do not use Strings if another type is more suitable. And you can initialize them with non-zero values ​​(e.g. blank lines) to avoid null problems.

+3
source

To expand the Steven comment, you can import using

 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 

then use:

 <c:if test="${empty myStruts2Var}"> 

or

 <c:if test="${not empty myStruts2Var}"> 
+1
source

For strings, I would use:

 <s:if test="myString in {null, ''}"> 

For collections, I would use:

 <s:if test="null == myCollection || myCollection.empty"> 

We hope this is useful for readability.

0
source

Source: https://habr.com/ru/post/1334142/


All Articles