Java Tutorial/JSTL/If

Материал из Java эксперт
Перейти к: навигация, поиск

JSTL If Else Statement

   <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 Statement

   <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 If Statement without 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 Statement with True Value

   <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 If With Logical Operator And/Or

   <source lang="java">

<%@ 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>

EL Expression Examples

Logical Operators

<c:set var="guess" value="12"/> Your guess is <c:out value="${guess}"/>
<c:if test="${(guess >= 10) && (guess <= 20)}">

  You"re in range!

</c:if> <c:if test="${(guess < 10) || (guess > 20)}">

  Try again!

</c:if> </body> </html></source>





NULL value And Boolean

   <source lang="java">

<%@ 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>

EL Expression Examples

Boolean and Null Values

<c:set var="StrVar" value="true"/> <c:if test="${StrVar}">

 equal!

</c:if>
null == null <c:out value="${null == null}"/>

</body> </html></source>





Set Test Result to Variable

   <source lang="java">

<html>

 <head>
   <title>Tag Plugin Examples: if</title>
 </head>
 <body>

Tag Plugin Examples - <c:if>


   <font color="#000000"/>
   </br>
   <%@ taglib uri="http://java.sun.ru/jstl/core" prefix="c" %>

Set the test result to a variable

   <c:if test="${1==1}" var="theTruth" scope="session"/>
   The result of testing for (1==1) is: ${theTruth}

Conditionally execute the body

   <c:if test="${2>0}">
 It"s true that (2>0)!
   </c:if>
 </body>

</html></source>