본문 바로가기

WEB_Programming/JSTL

3. JSTL IF 조건문

1. IF 문장 기본
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
<html>
  <head>
    <title>If with Body</title>
  </head>

  <body>
    <c:if test="${pageContext.request.method=='POST'}">
      <c:if test="${param.guess=='5'}">You guessed my number!
      <br />
      <br />
      <br />
      </c:if>

      <c:if test="${param.guess!='5'}">You did not guess my number!
      <br />
      <br />
      <br />
      </c:if>
    </c:if>

    <form method="post">Guess what number I am thinking of?
    <input type="text" name="guess" />

    <input type="submit" value="Try!" />

    <br />
    </form>
  </body>
</html>


2. if ~ else 문
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
<html>
  <head>
    <title>Using Choose,Otherwise and When</title>
  </head>

  <body>
    <c:if test="${pageContext.request.method=='POST'}">Ok, we'll
    send
    <c:out value="${param.enter}" />

    <c:choose>
      <c:when test="${param.enter=='1'}">pizza.
      <br />
      </c:when>

      <c:otherwise>pizzas.
      <br />
      </c:otherwise>
    </c:choose>
    </c:if>

    <form method="post">Enter a number between 1 and 5:
    <input type="text" name="enter" />

    <input type="submit" value="Accept" />

    <br />
    </form>
  </body>
</html>

3. 논리 연산자 And와 Or을 이용한 if
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jstl/fmt" %>

<html>
<head>
<title>EL Expression Examples</title>
</head>
<body>
<h1>EL Expression Examples</h1>

<h2>Logical Operators</h2>

<c:set var="guess" value="12"/>
<b>Your guess is </b>
<c:out value="${guess}"/>

<br/>

<c:if test="${(guess >= 10)  && (guess <= 20)}">
   <b>You're in range!</b><br/>
</c:if>
<c:if test="${(guess < 10)  || (guess > 20)}">
   <b>Try again!</b><br/>
</c:if>

</body>
</html>

4. true 값과 if문의 처리
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>

<HTML>
  <HEAD><TITLE>JSTL 'if' tag</TITLE></HEAD>
  <BODY>
    <c:if test="true">Hello world!</c:if>
  </BODY>
</HTML>

5. 본문이 없이 if구문 처리하는 예제
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
<html>
  <head>
    <title>If with NO Body</title>
  </head>

  <body>
    <c:if test="${pageContext.request.method=='POST'}">
    <c:if test="${param.guess=='5'}" var="result" />

    I tested to see if you picked my number, the result was
    <c:out value="${result}" />
    </c:if>

    <form method="post">Guess what number I am thinking of?
    <input type="text" name="guess" />

    <input type="submit" value="Try!" />

    <br />
    </form>
  </body>
</html>

6. 테스트 결과를 변수에 넣기, 이렇게 해서 결과를 재 사용할 수 있다.
<html>
  <head>
    <title>Tag Plugin Examples: if</title>
  </head>
  <body>
    <h1>Tag Plugin Examples - &lt;c:if></h1>

    <hr>

    <font color="#000000"/>
    </br>
    <%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>

    <h3>Set the test result to a variable</h3>
    <c:if test="${1==1}" var="theTruth" scope="session"/>
    The result of testing for (1==1) is: ${theTruth}

    <h3>Conditionally execute the body</h3>
    <c:if test="${2>0}">
      It's true that (2>0)!
    </c:if>
  </body>
</html>

7. 널과 Boolean
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jstl/fmt" %>

<html>
<head>
<title>EL Expression Examples</title>
</head>
<body>
<h1>EL Expression Examples</h1>

<h2>Boolean and Null Values</h2>

<c:set var="StrVar" value="true"/>
<c:if test="${StrVar}">
  equal!
</c:if><br/>

null == null
<c:out value="${null == null}"/>

</body>
</html>

'WEB_Programming > JSTL' 카테고리의 다른 글

6. JSTL foreach 문 처리  (0) 2008.06.20
5. JSTL ForTokens 예제  (0) 2008.06.20
4. JSTL Choose 예제  (8) 2008.06.20
2. JSTL 연산자  (0) 2008.06.20
1. JSTL Out 사용 예제  (0) 2008.06.20