EL: how to print static variables?
I have the following JSP page:
<%@taglib uri="http://java.sun.com/jstl/core" prefix="c"%>
test #1: value of PI is <c:out value="${java.lang.Math.PI}" />.
test #2: value of PI is ${java.lang.Math.PI}.
test #3: value of PI is <%= java.lang.Math.PI %>.
Somehow, only tes开发者_StackOverflow中文版t #3 has output. why doesn't EL print out values of static variables?
For each of your examples, this is what is happening:
<c:out value="${java.lang.Math.PI}" />
This is looking for the variable or bean named java and trying to execute a method on it called lang. There is probably no variable or bean in your JSP page called Java so there is no output.
${java.lang.Math.PI}
This is the same as above, just written using EL only. It's the same in that it's looking for a variable or bean named java.
<%= java.lang.Math.PI %>
What this is doing is during the JSP compile, java.lang.Math.PI is being calculated and written into the JSP. If you look at the compiled JSP you will see the value written there.
The third example is evaluating the expression as if you were in a Java class. The first two examples expect 'java' to be a variable name.
精彩评论