Udemy

JSTL Core Tag -<choose>

알아가자 2021. 8. 7. 16:22

JSTL core Tag <c:choose></c:choose>태그를 써보자!

choose Tag는 조건문 중 하나이다. 

<c:choose>

<c:when test = "넘어온 특정 인수가 참 or 거짓"> (여기서는 boolean 타입을 받기 때문에 참 거짓)

(만약 String 타입이 넘어온다면 이런 조건문 ="${tempStudent.firstName eq mohamad}" 성이 모하메드인 인수가 넘어오면 true 아니면 false 하고 다음 조건문으로 넘어가게 된다.

<c:when>

<c:otherwise>

위의 조건문에서 걸리지 않았을 경우 

</c:otherwise>

</c:choose>

 

여러 개의 c:when test 를 써서 조건을 if else 처럼 조건문을 달 수 있다. 

 

예제

https://programmingbeginner.tistory.com/19

자바 코드는 여기서 볼 수 있다.

 

JSP Core Tag (Looping with forEach)

1. HTML 테이블을 만들어 볼 것이다. 2. forEach 태그를 사용하여 정보를 표시할 것이다. 3. 이클립스로 고고 1) 다이나믹 웹 프로젝트의 Java Resources/src에 새로운 패키지 생성 Student class 생성 package c..

programmingbeginner.tistory.com

 

<%@page import="java.util.ArrayList"%>
<%@page import="com.luv2code.jsp.tagdemo.Student"%>
<%@page import="java.util.List"%>
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

<%
	List<Student> data = new ArrayList<>();
data.add(new Student("mohamad", "sala", false));
data.add(new Student("sadio", "mane", false));
data.add(new Student("diogo", "jose", true));

pageContext.setAttribute("myStudents", data);
%>

<html>
<body>
	<table border = "1">
		<tr>
			<th>First Name</th>
			<th>Last Name</th>
			<th>Gold Customer</th>
		</tr>
		<c:forEach var = "tempStudent" items = "${myStudents }">
		<tr>
			<td>${tempStudent.firstName }</td>		
			<td>${tempStudent.lastName }</td>		
			<td>
				<c:choose>
				
					<c:when test = "${tempStudent.goldCustomer }">
					Special Discount
					</c:when>
					<c:otherwise>
						no soup for you!
					</c:otherwise>
				
				</c:choose>
			</td>		
		</tr>
		</c:forEach>

	
</body>
</html>

간단하게 list 안의 값을 tempStudent로 옮기고 EL 표현식으로 goldCustomer의 상태가 참, 거짓인지 판단

참이면 Special Discount 나머지의 값(false)는 no soup for you! 이 화면에 출력된다.