Expression Language (EL
)
What? Another language?
Definition and Syntax
- EL is a simple way to access variables in a JSP page in a Java Web Application.
- Syntax:
${some variable or attribute name here}
- Syntax:
- Examples
- For a variable named “pageTitle”, you would access it like this:
${pageTitle}
- A variable named “employee” with a field named “firstName” would be accessed like this:
${employee.firstName}
- An element in a List or array is accessed like this:
${color[3]}
- A value from a Map with a given key:
${states["WI"]}
- For a variable named “pageTitle”, you would access it like this:
- We use these variables in our JSP code so that we can substitute values in a template.
EL
Demos
Demo 1
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<meta name="generator" content="HTML Tidy, see www.w3.org">
<title>${pageTitle}</title>
</head>
<body>
<h3>Nothing to see here, look at the title.</h3>
<a href="/java112/java112/">Home</a>
</body>
</html>
Demo 2
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<meta name="generator" content="HTML Tidy, see www.w3.org">
<title>EL Demo 2</title>
</head>
<body>
<h3>The full list</h3>
<ul><li>${colors}</li></ul>
<h3>One list element</h3>
<ul><li>${colors[2]}</li></ul>
<a href="/java112/java112/">Home</a>
</body>
</html>
Demo 3
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<meta name="generator" content="HTML Tidy, see www.w3.org">
<title>EL Demo 3</title>
</head>
<body>
<h3>Employee ID</h3>
<ul><li>${employee.id}</li></ul>
<h3>Employee Name</h3>
<ul><li>${employee.firstName} ${employee.lastName}</li></ul>
<a href="/java112/java112/">Home</a>
</body>
</html>