Java/JSTL/If
Содержание
If with Body
<%@ 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!
<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>
JSTL core tag: if
<%@ taglib uri="http://java.sun.ru/jstl/core" prefix="c" %>
<html>
<head><title>Using the Core JSTL tags</title></head>
<body>
<h2>Here are the available Time Zone IDs on your system</h2>
<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...<br /><br />" escapeXml="false" />
</c:if>
<c:set var="zones" value="${zone.availableIDs}" scope="session" />
<c:forEach var="id" items="${zones}">
<c:out value="${id}<br />" 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();
}
}
JSTL: If Else
<%@ 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.
<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>
JSTL If No Body
<%@ 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!" />
<br />
</form>
</body>
</html>
JSTL: if tag
<%@ 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>
JSTL RT If
<%@ 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>