Http://www.blogjava.net/Werther/archive/2009/11/10/301808.html logic: Present and logic: Empty labels
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
Here, the username is saved in the request, the key is users, and then the request is forwarded 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 to: 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.