Java/JSTL/If
Содержание
If with Body
<source lang="java">
<%@ taglib uri="http://java.sun.ru/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!
</c:if> <c:if test="${param.guess!="5"}">You did not guess my number!
</c:if> </c:if> <form method="post">Guess what number I am thinking of? <input type="text" name="guess" /> <input type="submit" value="Try!" />
</form> </body>
</html>
</source>
JSTL core tag: if
<source lang="java">
<%@ taglib uri="http://java.sun.ru/jstl/core" prefix="c" %> <html> <head><title>Using the Core JSTL tags</title></head> <body>
Here are the available Time Zone IDs on your system
<jsp:useBean id="zone" class="com.jexp.ZoneWrapper" /> <jsp:useBean id="date" class="java.util.Date" /> <c:if test="${date.time != 0}" >
<c:out value="Phew, time has not stopped yet...
" escapeXml="false" />
</c:if> <c:set var="zones" value="${zone.availableIDs}" scope="session" />
<c:forEach var="id" items="${zones}"> <c:out value="${id}
" escapeXml="false" />
</c:forEach>
</body> </html> // Save the ZoneWrapper.class into WEB-INF/classes/com/jexp //ZoneWrapper.java package com.jexp;
import java.util.TimeZone; public class ZoneWrapper {
public ZoneWrapper() { } public String[] getAvailableIDs() { return TimeZone.getAvailableIDs(); }
}
</source>
JSTL: If Else
<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"}">Ok, we"ll send <c:out value="${param.enter}" /> <c:choose> <c:when test="${param.enter=="1"}">pizza.
</c:when> <c:otherwise>pizzas.
</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>
JSTL If No Body
<source lang="java">
<%@ taglib uri="http://java.sun.ru/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!" />
</form> </body>
</html>
</source>
JSTL: if tag
<source lang="java">
<%@ taglib uri="http://java.sun.ru/jstl/core" prefix="c" %> <HTML>
<HEAD><TITLE>JSTL "if" tag</TITLE></HEAD> <BODY> <c:if test="true">Hello world!</c:if> </BODY>
</HTML>
</source>
JSTL RT If
<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>If Caseless</title> </head> <body> <c:set var="str" value="jStL" /> <jsp:useBean id="str" type="java.lang.String" /> <c-rt:if test="<%=str.equalsIgnoreCase("JSTL")%>"> They are equal</c-rt:if> </body>
</html>
</source>