Logic: the empty label determines whether the script variable is null, whether it is an empty string (the length is 0), whether it is an empty collection or map (call the isempty () method to determine ). Logic: the notempty label is similar.
<Logic: Empty name = "mybean"> The bean is missing </Logic: Empty> <Logic: notempty name = "mybean"> The bean is not missing </Logic: notempty> |
The code above indicates that when a bean named mybean does not exist in all the scopes, the bean is output as missing; If yes, the bean is not missing. This tag has three attributes: name, property, and scope. ---------------------------------------------------------------Logic: Present and logic: Empty have the same usage. The only difference is that the two have different processing methods for null strings. The code in index. jsp is as follows: 1 <logic: notpresent name = "users"> 2 notpresent 3 </logic: notpresent> 4 <logic: notempty name = "users"> 5 notempty 6 </logic: notempty> 7 <logic: Empty name = "users"> 8 empty 9 </logic: Empty> 10 <logic: present name = "users"> 11 present 12 </logic: Present> When the JSP is accessed for the first time, the output result is notpresent and empty because users is not defined and is not in any scope of page, request, session, and application. Next we will add an action to execute it before index. jsp, and then jump to index. jsp. At the same time, add the following code in the execute method of the action: 1 string username = ""; 2request. setattribute ("users", username ); 3 return New actionforward ("/index. jsp "); 4. Store the username in the request, the key is users, and then forward the request to the index. JSP, but the username value is an empty string. After forwarding, the output value is empty and present. Here we will make another change and change the code in the Action execute method: 1 string username = NULL; 2request. setattribute ("users", username ); 3 return New actionforward ("/Hello. jsp "); 4. The difference is that username is no longer a null string, but a null value. When it is forwarded to index. jsp, the output value is notpresent and empty. By comparing these changes, we can conclude that: For variables that are not defined in page, request, session, and application, or are not allocated memory space (null value), the two marking methods are consistent, this variable does not exist (notpresent) or is empty (empty ). For the Null String "" value, their processing is different. The logic: Present Flag considers that the Null String still exists. That is to say, as long as it references a variable in the memory space, logic: Present will return the present, while logic: Empty considers that the Null String is still empty. Therefore, in logic: Empty's view, variables not only need to reference a piece of memory space, the value of the address space cannot be a Null String. Otherwise, empty is returned if the variable is null. |