1

I'm writing this:

<c:forEach var="cntr" begin="1" end="10">
 <c:set var="mycase" value="${param.mode${cntr}}" />
 <c:if test="${mycase != null}">
   <c:param name="mode${cntr}" value="${mycase}"/>
  </c:if>
</c:forEach>

The result I want is for the redirect that sits outside of this to inherit the values of param.mode1, param.mode2, etc. as if I wrote:

  <c:if test="${param.mode1 != null}">
    <c:param name="mode1" value="${param.mode1}"/>
  </c:if>
  <c:if test="${param.mode2 != null}">
    <c:param name="mode2" value="${param.mode2}"/>
  </c:if>
  <c:if test="${param.mode3 != null}">
    <c:param name="mode3" value="${param.mode1}"/>
  </c:if>
  <c:if test="${param.mode4 != null}">
    <c:param name="mode4" value="${param.mode2}"/>
  </c:if>
  <c:if test="${param.mode5 != null}">
    <c:param name="mode5" value="${param.mode1}"/>
  </c:if>
  <c:if test="${param.mode6 != null}">
    <c:param name="mode6" value="${param.mode2}"/>
  </c:if>

All help appreciated.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Charles Barouch
  • 313
  • 2
  • 12

1 Answers1

1

You've to set another variable with mode${cntr} and to get the associated value using brace notation wherein you can pass a dynamic key.

<c:forEach var="cntr" begin="1" end="10">
    <c:set var="mode" value="mode${cntr}" />
    <c:set var="mycase" value="${param[mode]}" />
    <c:if test="${mycase != null}">
        <c:param name="mode${cntr}" value="${mycase}"/>
    </c:if>
</c:forEach>
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • That isn't working on Tomcat6. ${param['mode1']} does work, but the variable version won't. – Charles Barouch Mar 25 '11 at 15:59
  • It should work. I am however not sure if `` eats that. Try putting it outside. What exactly do you need it for? To what servlet version is your `web.xml` declared? – BalusC Mar 25 '11 at 16:06
  • The task is to put up a redirector that translates some params and passes others unmolested. So, URL xxx.org:8080/?mode1=yes&mode2=no& mode6=maybe would be mangled into 'if mode1=yes then mode1=YEAH, pass all others as-is and passed through the redirect as xxx.org:8080/bob.jsp?mode1=YEAH&mode2=no&mode6=maybe – Charles Barouch Mar 25 '11 at 16:10
  • I created a local test case where I replaced `` by just `${mycase}
    ` to print it and it works fine on both Tomcat 6 with web.xml set to Servlet 2.5 and Tomcat 7 with web.xml set to Servlet 3.0. In both cases I was using JSTL 1.2. I would however replace `${mycase != null}` by `${not empty mycase}` to skip empty strings as well.
    – BalusC Mar 25 '11 at 16:14
  • @BalusC - Works! Thanks for extracting me from yet another trip into syntax hell. – Charles Barouch Mar 25 '11 at 16:53
  • What was the root cause? – BalusC Mar 25 '11 at 16:54
  • @BalusC - not sure. The 'winning' code is: – Charles Barouch Mar 25 '11 at 17:20