JSTL if 예제들
ITWeb/개발일반 2010. 3. 15. 18:27JSTP core tag:if
<%@ taglib uri="http://java.sun.com/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.java2s.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/java2s
//ZoneWrapper.java
package com.java2s;
import java.util.TimeZone;
public class ZoneWrapper {
public ZoneWrapper() {
}
public String[] getAvailableIDs() {
return TimeZone.getAvailableIDs();
}
}
<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.java2s.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/java2s
//ZoneWrapper.java
package com.java2s;
import java.util.TimeZone;
public class ZoneWrapper {
public ZoneWrapper() {
}
public String[] getAvailableIDs() {
return TimeZone.getAvailableIDs();
}
}
JSTL RT If
<%@ 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>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>
<%@ taglib uri="http://java.sun.com/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>
JSTL If Else
<%@ 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'}">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>
<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.com/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>
<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>
If with Body
<%@ taglib uri="http://java.sun.com/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>
<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>