Link to home
Start Free TrialLog in
Avatar of cofactor
cofactor

asked on

doubts on JSP Expression Language

I know , EL = Expression Language .. why it is called Expression Language ? Is there any reason or just a random naming ?

I have following doubts.

(Q1) Does JSTL uses EL or  JSTL is the EL ?

(Q2)There is also another  language called Object-Graph Navigation Language (OGNL)
Do we call OGNL is the EL  or OGNL uses  EL ?

Q3)I see both of them uses object.property  notation.  whats the point of saying Object-Graph then while both the language can navigate java componenets?
Avatar of for_yan
for_yan
Flag of United States of America image



I think there is a good brief general explanation of these issues about EL, OGNLS, JSTL here:
http://stackoverflow.com/questions/341464/what-are-the-alternatives-to-jstl
Avatar of rrz
why it is called Expression Language ?  
Expression Language (EL) is a scripting language that is used for embedding expressions into web pages.
See  
http://en.wikipedia.org/wiki/Expression_Language   
http://en.wikipedia.org/wiki/Unified_Expression_Language   

>Q1) Does JSTL uses EL  
yes

>Do we call OGNL is the EL  or OGNL uses  EL ?
No, they are two different expression languages.  

>whats the point of saying Object-Graph then while both the language can navigate java componenets?
The java components  are in a object graph.  The expression languages use the object graph to find objects( or data).
Avatar of cofactor
cofactor

ASKER

>>>Expression Language (EL) is a scripting language that is used for embedding expressions into web pages.

Are you trying to say  JSTL is an implementation of Expression Language (EL) ?

Are you trying to say  JSTL is an example of Expression Language (EL) ?

>>>No, they are two different expression languages.  
Again , confused. Are you trying to say  OGNL is an example of Expression Language (EL) ?

>>>The java components  are in a object graph.  The expression languages use the object graph to find objects( or data).

Is not both JSTL and OGNL are cabable of finding  objects( or data) ? I think all Expression languages can find objects( or data).
>Are you trying to say  JSTL is an implementation of Expression Language (EL) ?
No.  JSTL is a tag library which uses EL.  
>Are you trying to say  OGNL is an example of Expression Language (EL) ?
No. ONGL is one expression language. EL(really now known as  Unified Expression Language) is another.  Others are SpEL, JBoss EL, and MVEL.  
>Is not both JSTL and OGNL are cabable of finding  objects( or data) ?  
yes  
>I think all Expression languages can find objects( or data).  
yes
Can I call this a  EL(expression language)  ?

<jsp:useBean id="cart" scope="session" class="session.Carts" />
<jsp:setProperty name="cart" property="*" />

<jsp:useBean id="checking" scope="session" class="bank.Checking" >
<jsp:setProperty name="checking" property="balance" value="0.0" />
</jsp:useBean> 

Open in new window


 Is this a EL ?

I think , this is a EL because  this does not look like  Java code in  JSP . The web-content designer can still enjoy the scripting flavour having  little or practically no knowledge of the Java Language.

So, in my opinion the above code is an EL.  Please comment.
>Can I call this a  EL(expression language)  ?  
>Is this a EL ?
No, those are what is known as "JSP action tags" or more generally "JSP action elements".
JSP action elements represent dynamic actions that take place at runtime.
JSP defines three types of elements: directives, scripting elements, and action elements.    

From
http://commons.apache.org/el/ 
The JSP Standard Tag Library (JSTL), version 1.0, introduced the concept of an Expression Language (EL), whose main goal is to provide page authors with an easy way to access and manipulate application data without requiring the use of scriptlets
JSP 2.0 adopted the EL specification from JSTL, and expanded its scope: EL expressions are no longer limited to JSTL action attributes, but may be used in any standard or custom action attribute declared to accept a runtime expression. In addition, EL expressions may now also be used directly in template text outside of any actions. JSP 2.0 also added an important feature to the EL specification: EL functions, which allow page authors to invoke static methods in Java classes from EL expressions. Additionally, JSP 2.0 allows programmatic access and customization of the EL evaluator through a set of standard interfaces and abstract classes.
To understand the last sentence in the above quote, take a look at the API.
http://download.oracle.com/docs/cd/E17802_01/products/products/jsp/2.1/docs/jsp-2_1-pfd2/javax/el/package-summary.html   
For a demonstration(not really of much practical value) of that API,  copy and paste this JSP.
<%@ page import="java.util.Arrays,javax.el.*" %>
<jsp:useBean id="list" class="java.util.ArrayList" />
<jsp:useBean id="newList" class="java.util.ArrayList" />
<%
  list.addAll(Arrays.asList("red","green","blue"));
  newList.addAll(Arrays.asList("brown", "yellow", "purple"));
  JspFactory jspFactory= JspFactory.getDefaultFactory();
  ExpressionFactory expFactory = jspFactory.getJspApplicationContext(application).getExpressionFactory();
  ELContext elContext =  pageContext.getELContext();
  ValueExpression valueEx = expFactory.createValueExpression(elContext, "${list}",String.class);
%>
My favorite colors are ${list[0]}, ${list[1]}, and ${list[2]}.<br/>
<% valueEx.setValue(elContext, newList); %>
No, I changed my mind. I like ${list[0]}, ${list[1]}, and ${list[2]}.

Open in new window

Ok. thanks . But still not clear. Here are some hiccups
Could you please show me an working example  using Oracle's EL expression to get java components in JSP ?

I  DONT wish to use OGNL,SpEL, JBoss EL, and MVEL.  These  EL does not come with Oracle's  package .

How does one uses Oracle's  EL to find  java components in JSP  ?

N.B: I'm not interested in using Oracle's  JSTL library but interested to use Oracle's  EL directly.


You can read
http://download.oracle.com/javaee/1.4/tutorial/doc/JSPIntro7.html 
http://download.oracle.com/javaee/5/tutorial/doc/bnahq.html   

First test to see if EL is working in your container. Create a JSP(call it something like test.jsp) and put the line
${"EL"}  is good.
If the output in your browser is
EL is good.  
then EL is working in your browser.
Here is example JSP.
<%@ page import="java.util.*" %>
<jsp:useBean id="list" class="java.util.ArrayList" />
<%
   //first let's make some scoped objects that EL can access.
   request.setAttribute("test1", "EL is fun");
   HashMap map = new HashMap();
   map.put("zero", "0");
   map.put("one", "1");
   session.setAttribute("myMap", map);
   //now populate the javabean created above
   list.add("red");
   list.add("blue");
%>
<html>
<body>
${test1}.<br/>
Five minus five is ${myMap["zero"]}.<br/>
JSP is number ${myMap["one"]}.<br/>
My favorite color is ${list[1]}.
</body>
</html>

Open in new window

The output should be
EL is fun.
Five minus five is 0.
JSP is number 1.
My favorite color is blue.
Yup . Now that really looks like EL.

I found EL has this features
Source: Expression Language
EL is, both syntactically and semantically, similar to JavaScript expressions:

there is no typecasting
type conversions are usually done implicitly
double and single quotes are equivalent
object.property has the same meaning as object['property']

EL also liberates the programmer from having to know the particularities of how the values are actually accessed: object.property can mean (depending on what the object is) either object.get("property") or object.getProperty("property") or object.getProperty() etc.

I see you accesed  the java components exactly the same way as EL definitions says here.You accesed   ${test1} , ${myMap["zero"]} , ${list[1]}  ....Ah..These are EL indeed .


Now, look at this snippet ..

<c:if test="${sessionScope.cart.numberOfItems > 0}"> 
  ...
</c:if> 

Open in new window


which part is EL here ?  I guess  <c:if  is  not EL. ... ${sessionScope.cart.numberOfItems > 0}  is the EL here.   Is that correct ?  because this exactly matches what the EL definition says.


One side note, you missed  to write  session.setAttribute("list", list); in your example ..am I right ?




>${sessionScope.cart.numberOfItems > 0}  is the EL here.
Yes.  By the way, sessionScope is a Implicit Object. See link I posted above here.  
>One side note, you missed  to write  session.setAttribute("list", list); in your example ..am I right ?  
No.   Did you run it ?  Did you get the output that I posted ?  
I used the <jsp:useBean> tag to create the list and put the list into scope. That tag makes the list variable a scripting variable and a scoped object(in this case a page-scope because that is the default).
>>>I used the <jsp:useBean> tag to create the list and put the list into scope. That tag makes the list variable a scripting variable and a scoped object(in this case a page-scope because that is the default).

Are you trying to say  by default
<jsp:useBean id="list" class="java.util.ArrayList" />>

is similar to ..
<jsp:useBean id="list" scope= "page"   class="java.util.ArrayList" />

ASKER CERTIFIED SOLUTION
Avatar of rrz
rrz
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Excellent