12

I was wondering if there was anyway of using JSP in <c:if> statement.

E.g.

<c:if test="${ param.variable1 == 'Add' <% JSP variable clause %>}">

So I want my JSP variable to checked against as well.

Any suggestions? I have tried ignorantly just sticking in the clause, obviously it did not work.

Thanks

BalusC
  • 1,040,783
  • 362
  • 3,548
  • 3,513
urema
  • 723
  • 4
  • 11
  • 21

1 Answers1

21

So you want to evaluate a scriptlet variable in EL? Store it as a request attribute.

<%
    String var = "some";
    request.setAttribute("var", var);
%>

<c:if test="${param.variable1 == 'Add' && var == 'some'}">

However, this makes no sense. You should avoid scriptlets altogether and use JSTL/EL to prepare this variable. So if you make the functional requirement more clear, e.g. "How do I do this (insert scriptlet code snippet) using JSTL/EL?", then we'll be able to suggest the right approach.

For example, you could use <c:set> to set a variable in EL scope.

<c:set var="var" value="some" scope="request" />

See also:

Community
  • 1
  • 1
BalusC
  • 1,040,783
  • 362
  • 3,548
  • 3,513
  • Just trying to experiment I tried *not* adding it to the request scope figuring it would be available in the page scope anyways. So I explicitly requested it from the page scope using in the EL: `pageScope.var`. Sure enough, it didn't work. Weirdly, when simply using `var` in the EL it *does* work without having to place the variable in the scriptlet code in request scope. What am I missing? – Marcus Junius Brutus Nov 05 '16 at 22:55
  • Why use request context? Simpler, more local, is `` – Per Lindberg Jan 31 '19 at 10:11