Java Tutorial/JSTL/If
Версия от 17:44, 31 мая 2010; (обсуждение)
Содержание
JSTL If Else 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"}">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 Statement
<%@ 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 If Statement without 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 Statement with True Value
<%@ 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 If With Logical Operator And/Or
<%@ taglib prefix="c" uri="http://java.sun.ru/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.ru/jstl/fmt" %>
<html>
<head>
<title>EL Expression Examples</title>
</head>
<body>
<h1>EL Expression Examples</h1>
<h2>Logical Operators</h2>
<c:set var="guess" value="12"/>
<b>Your guess is </b>
<c:out value="${guess}"/>
<br/>
<c:if test="${(guess >= 10) && (guess <= 20)}">
<b>You"re in range!</b><br/>
</c:if>
<c:if test="${(guess < 10) || (guess > 20)}">
<b>Try again!</b><br/>
</c:if>
</body>
</html>
NULL value And Boolean
<%@ taglib prefix="c" uri="http://java.sun.ru/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.ru/jstl/fmt" %>
<html>
<head>
<title>EL Expression Examples</title>
</head>
<body>
<h1>EL Expression Examples</h1>
<h2>Boolean and Null Values</h2>
<c:set var="StrVar" value="true"/>
<c:if test="${StrVar}">
equal!
</c:if><br/>
null == null
<c:out value="${null == null}"/>
</body>
</html>
Set Test Result to Variable
<!--
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 Plugin Examples: if</title>
</head>
<body>
<h1>Tag Plugin Examples - <c:if></h1>
<hr>
<font color="#000000"/>
</br>
<%@ taglib uri="http://java.sun.ru/jstl/core" prefix="c" %>
<h3>Set the test result to a variable</h3>
<c:if test="${1==1}" var="theTruth" scope="session"/>
The result of testing for (1==1) is: ${theTruth}
<h3>Conditionally execute the body</h3>
<c:if test="${2>0}">
It"s true that (2>0)!
</c:if>
</body>
</html>