Java Tutorial/JSTL/Choose
Содержание
Choose When and Otherwise
<%@ 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>
<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>
Choose When Statement
<%@ 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
<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>
Choose XML Data with Comparision
JSTL code
<%@ 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}" />
<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>
Combine If Chooser to Report Exception
<%@ 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}">
<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>
Tag Examples - choose
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<html>
<head>
<title>Tag Examples - choose</title>
</head>
<body>
<h1>Tag Plugin Examples - <c:choose></h1>
<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>