Robusto
Robusto

Reputation: 31883

Can JSTL var names be set from expressions or do they have to be literal strings?

I'm new to JSTL and I want to generalize some functionality that's used multiple times in one tag into a separate tag. My idea is to pass this tag an array of strings. That's no problem. But I also want to name some variables based on those strings so that I can reuse the results of expressions within the local scope.

Example:

<c:set var="hasFirstName" value="false"/>

I want to test for "hasFirstName" at various places within the tag. But the names will change depending upon the input. So is there any way to do something like this?

<c:forTokens var="formName" items="firstName,middleName,lastName" delims=",">
        <c:set var="has_${formName}" value="false"/>
</c:forTokens>

Upvotes: 3

Views: 2240

Answers (1)

BalusC
BalusC

Reputation: 1108577

Your approach will work, it will only be stored as has_firstName, not as hasFirstName.

You could substring and uppercase the 1st character with JSTL functions, but that's clumsy.

Upvotes: 2

Related Questions