Using the JSP Expression Language
The JSP Expression Language (EL) also
supports access to bean properties. Example 6-2
shows how an EL expression can be used to the same effect as the
<jsp:getProperty> action used in Example 6-1.
<html>
<head>
<title>A dose of Dilbert</title>
</head>
<body bgcolor="white">
<h1>A dose of Dilbert</h1>
<jsp:useBean id="cartoon"
class="com.ora.jsp.beans.motd.CartoonBean" />
<img src="images/${cartoon.fileName}">
</body>
</html>The bean created by the <jsp:useBean> action
can be used as a variable in an EL expression. The EL interprets a
name of a bean variable followed by a dot and a property name as a
request to get the value of the property from the bean. Here the
expression is used directly in the template text as part of the HTML
<img> element’s
src attribute value, retrieving the
fileName property value from the
carton bean when the JSP page is requested. Example 6-2 shows this notation in its simplest form, but
you can also access properties of properties by adding more elements
to the expression:
${someBean.aProperty.aPropertyOfTheProperty}In this case, the value of aProperty is a bean
that has a property named aPropertyOfTheProperty.
You can add as many property names as needed, without limit. Also
note that you can use this bean property syntax for any JSP action
attribute that permits EL expression values.
Whether to use <jsp:getProperty> or an EL expression to read a bean property ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access