Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to nest an EL expression in another EL expression

Tags:

jsp

jstl

el

I'm writing JSP / JSTL, and I'm trying to iterate over several items in a database.

I currently have three columns in the database, ${image1}, ${image2} and ${image3}. I'm trying to use the following code to print out information for them:

<c:forEach begin="1" end="3" var="i">
  ${image${i}}
</c:forEach>

Is there any way I can make this work?

like image 593
Toby Avatar asked Mar 23 '13 22:03

Toby


People also ask

What are the types of expressions in El?

JSP EL allows you to create expressions both (a) arithmetic and (b) logical. Within a JSP EL expression, you can use integers, floating point numbers, strings, the built-in constants true and false for boolean values, and null.

What is the role of El expression?

EL Expressions are a common way to make certain regions, fields, or buttons visible only for users with a specific role or set of roles. Below are some examples that can be used to achieve these requirements.

How we can use EL in JSP?

JSP EL Implicit Objects Used to get the attribute value with request scope. Used to get the attribute value with session scope. Used to get the attributes value from application scope. Used to get the request param values in an array, useful when request parameter contain multiple values.

What is El in Web?

This chapter introduces the Expression Language (also referred to as the EL), which provides an important mechanism for enabling the presentation layer (web pages) to communicate with the application logic (managed beans).


1 Answers

You can't nest EL expressions like that.

You can achieve the concrete functional requirement only if you know the scope of those variables beforehand. This way you can use the brace notation while accessing the scope map directly. You can use <c:set> to create a new string variable in EL scope composed of multiple variables. You can use e.g. ${requestScope} to access the mapping of request scoped variables.

Thus, provided that you've indeed stored those variables in the request scope, then this should do:

<c:forEach begin="1" end="3" var="i">
    <c:set var="image" value="image${i}" />
    ${requestScope[image]}
</c:forEach>

For the session scope, use the ${sessionScope} map instead.

See also:

  • Our EL wiki page
  • String Concatenation in EL
like image 163
BalusC Avatar answered Oct 15 '22 23:10

BalusC