수업(국비지원)/JSP

[JSP] EL(표현식),JSTL - JSTL core 태그: 조건문 (if, choose)

byeolsub 2023. 4. 18. 23:57
  • if 조건문
if : 단일 조건문을 주로 처리. else 문은 없음.
  
  속성 - test : if문에서 사용 할 조건을 지정, EL(${})사용 가능.boolean타입
       - var : 조건의 결과를 저장할 변수명을 지정
       - scope : 변수가 저장 될 영역을 지정  

 

📌 jstlcore2.jsp - if 조건문

c.tld의 if 설명
<tag>
    <description>
	Simple conditional tag, which evalutes its body if the
	supplied condition is true and optionally exposes a Boolean
	scripting variable representing the evaluation of this condition
    </description>
		<name>if</name>
		<tag-class>org.apache.taglibs.standard.tag.rt.core.IfTag</tag-class>
		<body-content>JSP</body-content>
		<attribute>
		<description>
	The test condition that determines whether or
	not the body content should be processed.
		</description>
		<name>test</name> //test 속성 이어야 한다.
		<required>true</required> //true일때 반환된다
		<rtexprvalue>true</rtexprvalue>
		<type>boolean</type> //boolean타입
		</attribute>
		<attribute>

 

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%-- /jsp3/src/main/webapp/jstl/jstlcore2.jsp --%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>core 태그 : 조건문 태그(if, choose)</title>
</head>
<body>
<h3>조건문 태그(if, choose)</h3>
<c:if test="${5<10 }">
<h4>5는 10보다 작다</h4>
</c:if>
<c:if test="${5>10 }">
<h4>5는 10보다 크다</h4>
</c:if>
</body>
</html>

 


  • choose 조건문
choose : 다중 조건을 처리할 때 사용. 
         하위에 when-otherwise 태그(<c:when>, <c:otherwise>)가 있음
          <c:when>에서 조건을 비교하기 때문에 if/else if/else와 같은 구조이다.
   
  속성 - test : if문에서 사용 할 조건을 지정, EL(${})사용 가능.boolean타입
       - var : 조건의 결과를 저장할 변수명을 지정
       - scope : 변수가 저장 될 영역을 지정             

📌 jstlcore2.jsp - choose 조건문

**choose 태그 설명 : if else 써야할때 사용
when과 otherwise로 이루어져있다.**

<tag>
    <description>
	Simple conditional tag that establishes a context for
	mutually exclusive conditional operations, marked by
	&lt;when&gt; and &lt;otherwise&gt;//**when과 otherwise로 이루어짐.**
    </description>
    <name>choose</name>
    <tag-class>org.apache.taglibs.standard.tag.common.core.ChooseTag</tag-class>
    <body-content>JSP</body-content>
  </tag>

when태그 설명 : if, else if 역할 하는 부분
<tag>
    <description>
	Subtag of &lt;choose&gt; that includes its body if its
	condition evalutes to 'true'
    </description>
    <name>when</name>
    <tag-class>org.apache.taglibs.standard.tag.rt.core.WhenTag</tag-class>
    <body-content>JSP</body-content>
    <attribute>
        <description>
The test condition that determines whether or not the
body content should be processed.
        </description>
        <name>test</name>
        <required>true</required>
        <rtexprvalue>true</rtexprvalue>
	<type>boolean</type>
    </attribute>
  </tag>

otherwise태그 설명 : else 역할 하는부분
<tag>
    <description>
        Subtag of &lt;choose&gt; that follows &lt;when&gt; tags
        and runs only if all of the prior conditions evaluated to
        'false'
    </description>
    <name>otherwise</name>
    <tag-class>org.apache.taglibs.standard.tag.common.core.OtherwiseTag</tag-class>
    <body-content>JSP</body-content>
  </tag>

 

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%-- /jsp3/src/main/webapp/jstl/jstlcore2.jsp --%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>core 태그 : 조건문 태그(if, choose)</title>
</head>
<body>
<h3>조건문 태그(if, choose)</h3>
<c:if test="${5<10 }">
<h4>5는 10보다 작다</h4>
</c:if>
<c:if test="${5>10 }">
<h4>5는 10보다 크다</h4>
</c:if>
<hr>
<c:choose>
	<c:when test="${5+10 == 25}">
		<h4>5+10은 25다</h4>
	</c:when>
	<c:when test="${5+10 == 16}">
		<h4>5+10은 16다</h4>
	</c:when>
	<c:when test="${5+10 == 510}">
		<h4>5+10은 510다</h4>
	</c:when>
	<c:otherwise>
		<h4>5+10은 모른다</h4>
	</c:otherwise>
</c:choose>
</body>
</html>