Using an Expression Element to Set an Attribute
In all examples so far, dynamic action attribute values are set using EL expressions, but an alternative is using a Java expression. In either case, the custom action attribute must accept what’s formally called a request-time attribute value.
Here is an example of how a Java expression can be used to set the
value
attribute of the standard
<jsp:param>
action:
<jsp:forward page="prodInfo.jsp"> <jsp:param name="id" value='<%= request.getParameter("prodId") %>' /> </jsp:forward>
The value
attribute is set to the value of a
request parameter. The container evaluates the request-time attribute
value when the page is requested, and the corresponding attribute is
set to the result of the expression. The Java type for the result of
the expression must match the type of the attribute you set this way.
In the <jsp:param>
value attribute case, the
expression must be of type String
, but other
action attributes may be of any type, including custom classes. This
is in contrast to when an EL expression used; the container always
tries to convert the EL expression evaluation result type to the
attribute type, but with Java expressions it’s up to
you to make sure they match.
Another difference between using Java and EL expressions when assigning an attribute value is that with a Java expression, you can’t combine expressions and static text as you can with EL expressions. For instance, this is illegal:
<jsp:param name="ranking" value='Ranking: <%= ...
Get JavaServer Pages, 3rd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.