본문 바로가기

개발하자/JSP&Servlet

EL)varStatus와 forEach이용한 예제

첫번째 방법(if사용)

<%@page import="java.util.ArrayList"%>
<%@ page language="java" contentType="text/html; charset=EUC-KR" pageEncoding="EUC-KR"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
String[] array={"aa","bb","cc","dd","ee","ff"};
%>
<c:forEach var="str" items="<%=array%>" varStatus="a">
<c:if test="${a.count==1}">
일번:${str}
</c:if>
<c:if test="${a.count!=1&&a.count!=6}">
${a.count}:${str}
</c:if>
<c:if test="${a.count==6}">
마지막:${str}
</c:if>
 <br/>
</c:forEach>

 

 

 

두번째 방법(choose사용)

<%@ page language="java" contentType="text/html; charset=EUC-KR" pageEncoding="EUC-KR"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
String[] array ={"aa","bb","cc","dd","ee","ff"};
%>
<c:forEach var="str" items="<%=array%>" varStatus="a">
<c:choose>
<c:when test="${a.first}">일번 : ${str}<br> </c:when>
<c:when test="${a.last }">마지막 : ${str}<br> </c:when>
<c:otherwise> ${a.count} : ${str} <br/></c:otherwise>
</c:choose>

</c:forEach>