Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
869 views
in Technique[技术] by (71.8m points)

jsp - How to nest an EL expression in another EL expression

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?

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...