Java Tutorial/JSTL/Choose

Материал из Java эксперт
Перейти к: навигация, поиск

Choose When and Otherwise

   <source lang="java">

<%@ taglib uri="http://java.sun.ru/jstl/core" prefix="c" %> <%@ taglib uri="http://java.sun.ru/jstl/core-rt" prefix="c-rt" %> <html>

 <head>
   <title>Count to 10 Example(tracking even and odd)</title>
 </head>
 <body>
<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> <td width="200" bgcolor="<c:out value="${color}"/>"> <c:out value="${i}" /> </td> </c:forEach> </table> </body> </html></source>

Choose When Statement

   <source lang="java">

<%@ taglib uri="http://java.sun.ru/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
     
</c:when> <c:when test="${param.enter=="2"}">Two
</c:when> <c:when test="${param.enter=="3"}">Three
</c:when> <c:when test="${param.enter=="4"}">Four
</c:when> <c:when test="${param.enter=="5"}">Five
</c:when> <c:otherwise> <c:out value="${param.enter}" />
</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" />
</form> </body>

</html></source>





Choose XML Data with Comparision

JSTL code



   <source lang="java">

<%@ taglib uri="http://java.sun.ru/jstl/core" prefix="c" %><%@ taglib uri="http://java.sun.ru/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}" />
<x:forEach var="student" select="$doc/students/student"> </x:forEach>
First Last Points Letter Note
           <x:out select="name/first" />
           <x:out select="name/last" />
           <x:out select="grade/points" />
           <x:out select="grade/letter" />
           <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>
 </body>

</html></source>





Combine If Chooser to Report Exception

   <source lang="java">

<%@ taglib uri="http://java.sun.ru/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}">

Exception!

       Refresh the page in your web browser to try again.
     </c:when>
     <c:otherwise>

No Exception.

       Refresh the page in your web browser to make another call.
     </c:otherwise>
   </c:choose>
 </body>

</html></source>





Tag Examples - choose

   <source lang="java">

<html>

 <head>
   <title>Tag Examples - choose</title>
 </head>
 <body>

Tag Plugin Examples - <c:choose>

   <font color="#000000"/>
   </br>
   <%@ taglib uri="http://java.sun.ru/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></source>