본문 바로가기

WEB_Programming/JSTL

4. JSTL Choose 예제

1. choose 태그 기본
<html>
  <head>
    <title>Tag Examples - choose</title>
  </head>
  <body>
    <h1>Tag Plugin Examples - &lt;c:choose></h1>

    <font color="#000000"/>
    </br>

    <%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>

    <c:forEach var="index" begin="0" end="4">
      #<c:out value="${index}"/> :
      <c:choose>
        <c:when test="${index == 1}">
          One!</br>
        </c:when>
        <c:when test="${index == 4}">
          Four!</br>
        </c:when>
        <c:when test="${index == 3}">
          Three!</br>
        </c:when>
        <c:otherwise>
          Huh?</br>
        </c:otherwise>
      </c:choose>
    </c:forEach>
  </body>
</html>

2. if에 대한 Choose 에서 예외상황 처리
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>

<%
  int i= (int) (Math.random() * 10);
  pageContext.setAttribute("signalStrength", new Integer(i), PageContext.PAGE_SCOPE);
%>

<html>
  <head>
    <title>The c:catch action</title>
  </head>
  <body>
    <c:if test="${pageScope.signalStrength < 5}">
      <c:set var="signalFailure" value="true" scope="page" />
    </c:if>

    <c:choose>
      <c:when test="${pageScope.signalFailure == true}">
        <h3>Exception!</h3>
        Refresh the page in your web browser to try again.
      </c:when>
      <c:otherwise>
        <h3>No Exception.</h3>
        Refresh the page in your web browser to make another call.
      </c:otherwise>
    </c:choose>
  </body>
</html>

3. xml 데이터 비교
3.1 데이터 처리 jsp
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
<%@ taglib uri=
"http://java.sun.com/jstl/xml" prefix="x" %>
<html>
  <head>
    <title>For Each Examples</title>
  </head>

  <body>
    <c:import var="students" url="students.xml" />

    <x:parse var="doc" xml="${students}" />

    <table border="1">
      <tr>
        <TH>First</th>

        <TH>Last</th>

        <TH>Points</th>

        <TH>Letter</th>

        <TH>Note</th>
      </tr>

      <x:forEach var="student" select="$doc/students/student">
        <tr>
          <td>
            <x:out select="name/first" />
          </td>

          <td>
            <x:out select="name/last" />
          </td>

          <td>
            <x:out select="grade/points" />
          </td>

          <td>
            <x:out select="grade/letter" />
          </td>

          <td>
            <x:choose>
              <x:when select="grade/points>90">You did
              great!</x:when>

              <x:when select="grade/points>80">You did
              good!</x:when>

              <x:when select="grade/points>75">You did
              ok.</x:when>

              <x:when select="grade/points>70">Well, you
              passed.</x:when>

              <x:otherwise>You failed</x:otherwise>
            </x:choose>
          </td>
        </tr>
      </x:forEach>
    </table>
  </body>
</html>

3.2 xml 데이터
<?xml version="1.0" encoding="ISO-8859-1"?>
<students>
   <student id="1">
      <name>
         <first>A</first>
         <last>B</last>
         <middle>T</middle>
      </name>
      <grade>
         <points>88</points>
         <letter>B</letter>
      </grade>
   </student>
   <student id="2">
      <name>
         <first>C</first>
         <last>D</last>
         <middle>K</middle>
      </name>
      <grade>
         <points>92</points>
         <letter>A</letter>
      </grade>
   </student>
   <student id="3">
      <name>
         <first>E</first>
         <last>F</last>
         <middle>A</middle>
      </name>
      <grade>
         <points>72</points>
         <letter>C</letter>
      </grade>
   </student>
   
</students>

4. Choose When 구문
<%@ 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'}">You entered:
   
    <c:choose>
      <c:when test="${param.enter=='1'}">One
      <br />
      </c:when>

      <c:when test="${param.enter=='2'}">Two
      <br />
      </c:when>

      <c:when test="${param.enter=='3'}">Three
      <br />
      </c:when>

      <c:when test="${param.enter=='4'}">Four
      <br />
      </c:when>

      <c:when test="${param.enter=='5'}">Five
      <br />
      </c:when>

      <c:otherwise>
        <c:out value="${param.enter}" />

        <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>

5. Choose와 When, otherwise
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jstl/core-rt" prefix="c-rt" %>
<html>
  <head>
    <title>Count to 10 Example(tracking even and odd)</title>
  </head>

  <body>
    <table border="0">
      <c:forEach var="i" begin="1" end="10" varStatus="status">
        <jsp:useBean id="status" type="javax.servlet.jsp.jstl.core.LoopTagStatus" />

        <c-rt:choose>
          <c-rt:when test="<%=status.getCount()%2==0%>">
            <c:set var="color" value="#eeeeee" />
          </c-rt:when>

          <c-rt:otherwise>
            <c:set var="color" value="#dddddd" />
          </c-rt:otherwise>
        </c-rt:choose>

        <tr>
          <td width="200" bgcolor="<c:out value="${color}"/>">
          <c:out value="${i}" />
          </td>
        </tr>
      </c:forEach>
    </table>
  </body>
</html>

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

6. JSTL foreach 문 처리  (0) 2008.06.20
5. JSTL ForTokens 예제  (0) 2008.06.20
3. JSTL IF 조건문  (0) 2008.06.20
2. JSTL 연산자  (0) 2008.06.20
1. JSTL Out 사용 예제  (0) 2008.06.20