使用JSTL标记连接两个变量以检查null条件



我正在尝试在ModelMap 中分别发送每个用户的图像

model.addAttribute("profileImage"+i, sb); // I have added attribute in loop

变量"i"是我为每个用户迭代的每个图像的索引。

现在我有profileImage0、profileImage1、profilemage2等等

访问时,我正在设置一些条件

<c:choose>
<c:when test="${profileImage!=null}">
<center><img src="${profileImage}"  id="${profileImage}" class="rounded-circle img-circle img-responsive" style="height:90px;width:90px;" alt="Avatar">
</c:when> 
<c:otherwise>
<center><img src="${pageContext.request.contextPath}/resources/images/defaultuserprofile.png"  id="${profileImage}" class="rounded-circle img-circle img-responsive" style="height:90px;width:90px;" alt="Avatar"></center>
</c:otherwise>
</c:choose> 

因此,在上面的代码中,我需要为变量添加一些索引,这将首先初始化变量的值,然后正确使用它,而不是像这样使用。

比如profileImage0、profileImage1等等…

您可以使用<c:forEach>标记来迭代您的变量。您可能想传入控制器中的最大值,这样您就知道需要从您的请求中提取多少值:

<c:forEach var = "i" begin = "1" end = "${max}">
<c:set var = "variableName" scope = "session" value = "profileName + ${i}"/>
<c:set var = "profileImage" scope = "session" value = "${variableName}"/>
</c:forEach>

(或者类似的东西…(

最新更新