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?
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.
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.
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.
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).
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With