수업(국비지원)/JSP

[JSP] EL(표현식),JSTL - JSTL core 태그: 반복문(forEach) - List 객체, Map객체, 배열 객체

byeolsub 2023. 4. 18. 23:59
  • forEach 반복문
<c:forEach> : 반복문
   
  속성  - var="변수명" 
        - begin="시작값" 
        - end="마지막값" 
        - step="증가값"

 

📌 - forEach 반복문

forEach 반복문 태그 설명 : 기본적인 반복문 태그
<tag>
    <description>
	The basic iteration tag, accepting many different
        collection types and supporting subsetting and other
        functionality
    </description>
    <name>forEach</name>
    <tag-class>org.apache.taglibs.standard.tag.rt.core.ForEachTag</tag-class>
    <tei-class>org.apache.taglibs.standard.tei.ForEachTEI</tei-class>
    <body-content>JSP</body-content>
    <attribute>
        <description>
Collection of items to iterate over.
        </description>
	<name>items</name>
	<required>false</required>
	<rtexprvalue>true</rtexprvalue>
	<type>java.lang.Object</type>
        <deferred-value>
	    <type>java.lang.Object</type>
        </deferred-value>
    </attribute>
    <attribute>
        <description>
If items specified:
Iteration begins at the item located at the
specified index. First item of the collection has
index 0.
If items not specified:
Iteration begins with index set at the value
specified.
        </description>
	<name>begin</name>
	<required>false</required>
	<rtexprvalue>true</rtexprvalue>
	<type>int</type>
    </attribute>
    <attribute>
        <description>
If items specified:
Iteration ends at the item located at the
specified index (inclusive).
If items not specified:
Iteration ends when index reaches the value
specified.
        </description>
	<name>end</name>
	<required>false</required>
	<rtexprvalue>true</rtexprvalue>
	<type>int</type>
    </attribute>
    <attribute>
        <description>
Iteration will only process every step items of
the collection, starting with the first one.
        </description>
	<name>step</name>
	<required>false</required>
	<rtexprvalue>true</rtexprvalue>
	<type>int</type>
    </attribute>
    <attribute>
        <description>
Name of the exported scoped variable for the
current item of the iteration. This scoped
variable has nested visibility. Its type depends
on the object of the underlying collection.
        </description>
	<name>var</name>
	<required>false</required>
	<rtexprvalue>false</rtexprvalue>
    </attribute>
    <attribute>
        <description>
Name of the exported scoped variable for the
status of the iteration. Object exported is of type
javax.servlet.jsp.jstl.core.LoopTagStatus. This scoped variable has nested
visibility.
        </description>
	<name>varStatus</name>
	<required>false</required>
	<rtexprvalue>false</rtexprvalue>
    </attribute>
  </tag>

 

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%-- /jsp3/src/main/webapp/jstl/jstlcore3.jsp --%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>JSTL core 태그 : forEach 반복 관련 태그</title>
</head>
<body>
<h3>forEach 태그를 이용하여 1부터 100까지의 합 구하기</h3>
<c:set var="sum" value="${0}"/>
<c:forEach var="i" begin="1" end="100">
	<c:set var="sum" value="${sum+i}"/>
</c:forEach>
1부터 100까지의 합 : ${sum}<br>

<h3>forEach 태그를 이용하여 1부터 100까지의 짝수 합 구하기</h3>
<c:set var="sum" value="${0}"/>
<c:forEach var="i" begin="1" end="100">
	<c:if test="${i%2 == 0}"/>
		<c:set var="sum" value="${sum+i}"/>
</c:forEach>
1.1부터 100까지의 짝수 합: ${sum}<br>--if조건문사용
<c:set var="sum" value="${0}"/>
<c:forEach var="i" begin="2" end="100" step="2">
		<c:set var="sum" value="${sum+i}"/>
</c:forEach>
2.1부터 100까지의 짝수 합: ${sum}<br> --step 사용

<h3>forEach 태그를 이용하여 1부터 100까지의 홀수 합 구하기</h3>
<c:set var="sum" value="${0}"/>
<c:forEach var="i" begin="1" end="100">
	<c:if test="${i%2 != 2}"/>
		<c:set var="sum" value="${sum+i}"/>
</c:forEach>
1.1부터 100까지의 홀수 합: ${sum}<br>--if조건문사용
<c:set var="sum" value="${0}"/>
<c:forEach var="i" begin="1" end="100" step="2">
		<c:set var="sum" value="${sum+i}"/>
</c:forEach>
2.1부터 100까지의 홀수 합: ${sum}<br> --step사용
<br/ ><%-- empty tag: 하위태그없는 태그 --%>
</body>
</html>

 


 📌 List 객체 이용하여 출력

List 객체 이용하여 출력하는 부분

<%
	List<Integer> list = new ArrayList<>();
	for(int i=1;i<=10;i++) list.add(i*10);
	pageContext.setAttribute("list",list);
%>
<h3>forEach 태그를 이용하여 List객체 출력하기</h3>
<%-- itmes="${list} : list이름을 가진 속성의 값 --%>
<c:forEach var="i" items="${list}"><%-- 1~10까지 가지고있는 list객체가 된다. i : 10>20>30... --%>
	${i} &nbsp;&nbsp;&nbsp;
</c:forEach>

<h3>List객체 요소의 합을 출력하기</h3>
<c:set var="sum" value="${0}"/>
<c:forEach var="i" items="${list}"><%--list요소가 int형 이기때문에 가능 --%>
	<c:set var="sum" value="${sum+i}"/>
</c:forEach>
list 객체의 요소의 합 : ${sum}

<h3>forEach 태그를 이용하여 List객체를 인덱스: 값의 형태로 출력하기</h3>
<%-- varStatus="변수명" => 반복중인 요소의 상태 정보 
	  변수명.index : 0부터 시작하는 첨자
	  변수명.count : 1부터 시작하는 갯수
--%>
<c:forEach var="i" items="${list}" varStatus="stat">
	${stat.index}:${i} &nbsp;&nbsp;&nbsp;
</c:forEach>

<h3>forEach 태그를 이용하여 List객체를 한줄에 2개씩 출력하기</h3>
<c:forEach var="i" items="${list}" varStatus="stat">
	${i}&nbsp;&nbsp;&nbsp;
	<c:if test="${stat.index%2==1}"><br></c:if>
</c:forEach>
<hr>
<c:forEach var="i" items="${list}" varStatus="stat">
	${i}&nbsp;&nbsp;&nbsp;
	<c:if test="${stat.count%2==0}"><br></c:if>
</c:forEach>

 

 

📌 Map객체 출력

<%
	Map<String, Object> map = new HashMap<>();
	map.put("name","홍길동");
	map.put("today",new Date());
	map.put("age",20);
	map.put("list",list);
	pageContext.setAttribute("map",map);
%>
<h3>forEach 태그를 이용하여 Map객체 출력하기</h3>
<%-- m : (key,value)인 객체 --%>
<c:forEach var="m" items="${map}" varStatus="stat">
	${stat.count} : ${m.key} = ${m.value}<br>
</c:forEach>
<h3>key의 이름으로 Map객체 출력하기</h3>
name : ${map.name}><br>
today : ${map.today}<br>
age : ${map.age}<br>
list : ${map.list }<br>
list[0] : ${map.list[0] }<br>

 

 

📌 배열 객체 출력

<h3>forEach태그를 이용하여 배열 객체 출력하기</h3>
<%
	int[]arr ={10,20,30,40,50};
	pageContext.setAttribute("arr",arr);
%>
<c:forEach var="a" items="${arr}" varStatus="stat">
	arr[${stat.index}] = ${a}<br>
</c:forEach>

<h3>forEach태그를 이용하여 배열 객체 첨자로 출력하기</h3>
<c:forEach var="i" begin="0" end="4" varStatus="stat">
	arr[${stat.index}] = ${arr[i]}<br>
</c:forEach>