I would like to get something like the next code generated in JSTL
<c:choose>
<c:when test="${random number is even}">
<div class="redlogo">
</c:when>
<c:otherwise>
<div class="greenlogo">
</c:otherwise>
</c:choose>
I would like to get something like the next code generated in JSTL
<c:choose>
<c:when test="${random number is even}">
<div class="redlogo">
</c:when>
<c:otherwise>
<div class="greenlogo">
</c:otherwise>
</c:choose>
Hope it helps! random taglib
Also you may try $Math.random function.
I just want to point out, that if you are using EL 2.2 (or above), you can directly call any method in EL (see this question), so probably the quickest method is to initialize a bean
and then directly invoke
nextInt()
or any other method from java.util.Random inside page:or with parameter:
You could wrap
java.util.Random
in a bean and make use ofjsp:useBean
....so that you can use it in your JSP as follows:
(note that I optimized the
c:choose
away with help of the ternary operator).This one is a bit ugly but it works...
Later you can check for
${rand mod 2 == 0}
and${rand mod 2 == 1}
to get your desired output.