ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [JSTL] if, when, forEach
    WEB/JSTL 2022. 9. 9. 20:16

    if

    -c:if test 조건절이 참이면 /c:if까지의 내용을 웹페이지에 적용 또는 표시하고

    아니면 표시, 적용 없이 지나감.

    -test 안의 조건식은 EL 문법을 이용함.

    <c:if test="${비교연산}"></c:if>
    <c:if test="${param.color==1}">
    	<span style="color:red; font-size:180%; font-weight:bold;">빨강</span>
    </c:if>
    <c:if test="${param.color==2}">
    	<span style="color:green; font-size:180%; font-weight:bold;">
    	초록</span>
    </c:if>
    <c:if test="${param.color==3}">
    	<span style="color:blue; font-size:180%; font-weight:bold;">
    	파랑</span>
    </c:if>


    When

    <c:choose>
    	<c:when test="${param.fruit==1}"><!-- if -->
    		<span style="color:red; font-size:180%; font-weight:bold;">
    		사과</span>
    	</c:when>
    	<c:when test="${param.fruit==2}"><!-- else if -->
    		<span style="color:green; font-size:180%; font-weight:bold;">
    		멜론</span>
    	</c:when>
    	<c:when test="${param.fruit==3}"><!-- else if -->
    		<span style="color:yellow; font-size:180%; font-weight:bold;">
    		바나나</span>
    	</c:when>
    	<c:otherwise><!--  else -->
    		<span style="color:silver; font-size:180%; font-weight:bold;">
    		기타</span>
    	</c:otherwise> 
    </c:choose>


    forEach

    배열 저장

    <%
    String [] movieList={"타이타닉","시네마 천국","혹성 탈출","킹콩"};
    request.setAttribute("mList",movieList);
    // Attribute는 HashMap 형식의 리스트이므로 
    // 키 값만 존재하면 어떤 자료들도 보관 가능.
    // ArrayList 등도 위의 배열과 같이 손쉽게 보관하거나 전달 가능.
    %>

    1. JSP 문법으로 출력

    <%
    	String [] mlist=(String[])request.getAttribute("mList");
    	for(String s:mlist)
    		out.print(s+"<br>");
    %>

    2. EL과 JSTL을 이용해 출력(배열 요소를 이용)

    <c:forEach items="${mList}" var="movie">
    	${movie}<br/>
    </c:forEach>
    <!-- 
    items="" : 반복 실행에 이용할 리스트 또는 배열을 지정
    var="": 배열 또는 리스트의 값을 한번에 하나씩 저장할 때 사용할 변수 이름 
    -->

     

     

    Table에 forEach 사용하기

    <%
    String [] movieList={"타이타닉","시네마 천국","혹성 탈출","킹콩"};
    request.setAttribute("mList",movieList);
    %>
    <table border="1" style="width:700px; text-align:center">
    	<tr>
    		<th>index</th><th>count</th><th>title</th>
    	</tr>
    	<c:forEach items="${mList}" var="movie" varStatus="state">
    		<tr><td>${state.index}</td><td>${state.count}</td>
    		<td>${movie }</td></tr>
    	</c:forEach>
    </table>
    <!-- 
    varStatus: 반복실행의 상태값을 갖고 있는 클래스
    status: 현재 반복 순서 객체 변수
    ${status.count): 1부터 시작한 반복의 현재 아이템
    ${status,index}: 0부터 시작한 반복의 현재 아이템
     -->

     

    -${status.first}: 현재 루프가 처음이면 true 리턴

     <ul>
     	<c:forEach var="movie" items="${mList}" 
     		varStatus="status">
     		<c:choose>
     			<c:when test="${status.first}">
     					<li style="font-weigth:bold; color:red;">
     						${movie}
     					</li>
    			</c:when>
     			<c:otherwise>
     				<li>${movie}</li>
    			</c:otherwise>
    		</c:choose>
     	</c:forEach>
     </ul>

     

    -${status.last}: 현재 루프가 마지막이라면 true 리턴

     <c:forEach var="movie" items="${mList}" 
     	varStatus="status">
     		${movie}
     	<c:if test="${not status.last }">, </c:if>
     </c:forEach>

     

    -${status.current}: 현재 아이템

    -${status.begin}: 시작값

    -${status.end}: 끝값

    -${status.step}: 증가값

    <c:forEach var="cnt" begin="1" end="10" varStatus="status">
     		${cnt}<c:if test="${not status.last }">, </c:if>
    </c:forEach>

    <table border="1" style="width:50%; text-align:center;"
     			align="left">
     	<tr><th>index</th><th>count</th><th>cnt</th></tr>
     	 <c:forEach var="cnt" begin="7" end="10" 
     	 					varStatus="status">
     		<tr><td>${status.index}</td>
     				<td>${status.count}</td>
     				<td>${cnt}</td></tr> 
     	</c:forEach>
     </table>

    댓글

Designed by Tistory.