empty and null in EL expressions

empty and null in EL expressions

Let's talk about the difference between null and empty in EL expressions, and then talk about an interesting problem that has recently appeared in the project.
Both null and empty in EL can be used to determine whether the value is empty, but there is a slight difference between the two.
Let's use a small example on the Internet to demonstrate:


test.jsp code:

<%@page pageEncoding="utf-8" %>
name值为:${param.name }
empty处理结果:${empty param.name }
==null处理结果:${param.name == null }

The first case: Input in the address bar: http://127.0.0.1:8080/test.jsp
Display:
name value:
empty Processing result: true
==null Processing result: true

The second case: Input in the address bar: http://127.0.0.1:8080/test.jsp?name=Display
: The
name value is:
empty Processing result: true
==null Processing result: false
Analysis: the first case name=null, the name=""
empty expression in the second case is the same for the identification of name=null and name="", and both return true; the
==null expression is judged to be true for name=null, and for The name="" judgment is false;
if you have to use the ==null expression to judge whether it is empty, you need to write the expression as follows:

${param.name == null && 
param.name!=""}

In the project I am working on recently, I need to parse the json data from the background on the page. I simplified the code. One of the codes is like this,

<c:if test="${not empty product.isExport}>isExportde 值:${product.isExport}</c:if>  

The purpose of the code is to output the value of isExport when isExport is not empty, and output nothing when it is empty; the
result is unbelievable, "isExportde value: null" is output on the page.
Since there is information output on the page, it means that isExport is recognized as not empty when judging, but why is the output isExport value null?
Then assuming that isExport is empty, the page should not output any information at this time. Neither case makes sense.
Later, it was speculated that there was an error in the json string. After checking the json in the background, it took a long time to see the clue. This string of json was obtained from other systems through httpClient. I don’t know what happened in the middle, and isExport became " null", is "null" instead of null, that is, the value of isExport is a "null" string... embarrassing, wasted a little time by this little detail.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325342545&siteId=291194637