JSP

JSTL(JSP Standard Tag Library) - 4. forEach 사용

에어팟맥스 2022. 8. 31. 23:29

실행 파일

 

RequestDispatcher dispatcher = request.getRequestDispatcher("05_forEach_Array_List_view_02.jsp");
dispatcher.forward(request, response);

을 사용할 때

"05_forEach_Array_List_view_02.jsp" 파일만 request 영역에 저장되어있는 arr_friend_name 배열을 꺼낼 수 있다.
결과물은 05_forEach_Array_List_view_02.jsp 가 나오지만 URL 에서는 05_forEach_Array_List_execute_02.jsp 로 보인다.
   -> 보안성이 뛰어나다.

 

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

<%@ page import="java.util.*, chap03.PersonDTO" %>  
<%
	String[] arr_friend_name = {"임선우", "진민지", "서영학", "진혜린"};

	request.setAttribute("arr_friend_name", arr_friend_name);
	
	////////////////////////////////////////////////////////////////////
	
	List<PersonDTO> personList = new ArrayList<>(); 
	
	PersonDTO person1 = new PersonDTO();
	person1.setName("김태희");
    person1.setSchool("대졸");
    person1.setColor("red");
    person1.setFood("초콜릿,마이쮸,월드콘".split("\\,")); 
    
    PersonDTO person2 = new PersonDTO();
    person2.setName("아이유");
    person2.setSchool("초대졸");
    person2.setColor("blue");
    person2.setFood("육회비빔밥,광어회,조개구이,참이슬".split("\\,"));
    
    PersonDTO person3 = new PersonDTO();
    person3.setName("박보영");
    person3.setSchool("대학원졸");
    person3.setColor("green");
    person3.setFood("라면,떡볶이,순대,피자".split("\\,"));
    
    
    personList.add(person1);
    personList.add(person2);
    personList.add(person3);
	
    request.setAttribute("personList", personList);
    
	
	///////////////////////////////////////////////////////////////////
	
	RequestDispatcher dispatcher = request.getRequestDispatcher("05_forEach_Array_List_view_02.jsp");
	dispatcher.forward(request, response);
	
	/* "05_forEach_Array_List_view_02.jsp" 파일만 request 영역에 저장되어있는 arr_friend_name 배열을 꺼낼 수 있다.
	   결과물은 05_forEach_Array_List_view_02.jsp 가 나오지만 URL 은 05_forEach_Array_List_execute_02.jsp 가 됨
	   -> 보안성이 뛰어나다.*/
	
%>

 

view 파일

 

<c:forEach var="friend_nameitems="${requestScope.arr_friend_name}"> 
                                          <!-- items 에는 배열 아니면 리스트만 들어올 수 있다. -->
    <li>${friend_name}</li>
</c:forEach>

 

<c:forEach var="psdtoitems="${requestScope.personList}"> <!-- requestScope. 은 생략 가능 -->
    <ul>
        <li>성명: ${psdto.name}</li> <!-- DTO 의 getName()의 name -->
        <li>학교: ${psdto.school}</li>
        <li>색상: ${psdto.color}</li>
        <li>음식: ${psdto.strFood}</li>
    </ul>
</c:forEach>

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

<%-- ==== JSTL(JSP Standard Tag Library) 사용하기 ==== --%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>친구명단 출력하기</title>
</head>
<body>

	<c:if test="${empty requestScope.arr_friend_name}">
		<div>
			<span style="color: red;">친구명단이 없습니다.</span>
		</div>
	</c:if>
	
	<c:if test="${not empty requestScope.arr_friend_name}">
		<div>
			<ol>
				<c:forEach var="friend_name" items="${requestScope.arr_friend_name}"> 
				                        <!-- items 에는 배열 아니면 리스트만 들어올 수 있다. -->
					<li style="color: blue;">${friend_name}</li>
				</c:forEach>
			</ol>
		</div>
	</c:if>
	
	<hr style="border: solid 1px red; margin: 20px 0;">
	
	<c:if test="${empty requestScope.personList}">
		<div>
			<span style="color: red;">회원명단이 없습니다.</span>
		</div>
	</c:if>
	
	<c:if test="${not empty requestScope.personList}">
		<div>
			<c:forEach var="psdto" items="${requestScope.personList}"> <!-- requestScope. 은 생략 가능 -->
				<ul>
					<li>성명: ${psdto.name}</li> <!-- DTO 의 getName()의 name -->
					<li>학교: ${psdto.school}</li>
					<li>색상: ${psdto.color}</li>
					<li>음식: ${psdto.strFood}</li>
				</ul>
			</c:forEach>
		</div>
	</c:if>
	
</body>
</html>