Java Tutorial/JSTL/Set

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

Do Calculation In Set

   <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

Arithmetic Operators in Expressions

<c:set var="appleCount" value="${1 + 2 * 4 - 6 / 2}"/>

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

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





JSTL Set With Body

   <source lang="java">

<%@ taglib uri="http://java.sun.ru/jstl/core" prefix="c" %> <html>

 <head>
   <title>Set Examples</title>
 </head>
 <body>

Set With Body

 <c:set var="str">Hello, Again World</c:set>
 str = 
 <c:out value="${str}" />
 
 </body>

</html></source>





JSTL Set Without Body

   <source lang="java">

<%@ taglib uri="http://java.sun.ru/jstl/core" prefix="c" %> <html>

 <head>
   <title>Set Examples</title>
 </head>
 <body>

Set With No Body

 <c:set var="str" value="Hello World" />
 str = 
 <c:out value="${str}" />
 
 </body>

</html></source>





Remove Variable

   <source lang="java">

<%@ taglib uri="http://java.sun.ru/jstl/core" prefix="c" %> <html>

 <head>
   <title>Set Examples</title>
 </head>
 <body>

Remove Example

 <c:set var="test" value="Hello World" scope="page" />
 The value in the variable test before remove is 
 <c:out value="${test}" />
 
<c:remove var="test" scope="page" /> The value in the variable test after remove is <c:out value="${test}" />
</body>

</html></source>





Set Comma Delimited String to Variable

   <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>Count to 10 Example(tracking even and odd)</title>
 </head>
 <body>
   <c:set var="days" value="A,B,C,D,E,F,G" />
<c:forEach var="i" items="${days}" varStatus="status"> <jsp:useBean id="status" type="javax.servlet.jsp.jstl.core.LoopTagStatus" /> <c-rt:choose> <c-rt:when test="<%=status.getCount()%2==0%>"> <c:set var="color" value="#cccccc" /> </c-rt:when> <c-rt:otherwise> <c:set var="color" value="#dddddd" /> </c-rt:otherwise> </c-rt:choose> <td width="200" bgcolor="<c:out value="${color}"/>"> <c:out value="${i}" /> </td> </c:forEach> </table> </body> </html></source>

Set Value for Variable

   <source lang="java">

<%@ taglib uri="http://java.sun.ru/jstl/core" prefix="c" %>

Scoped Variable Current Value
   Page Scope
(scopeVarPage)
 
   <c:out value="${scopeVarPage}" />
   Request Scope
(scopeVarRequest)
 
   <c:out value="${scopeVarRequest}" />
   Session Scope
(scopeVarSession)
 
   <c:out value="${scopeVarSession}" />
   Application Scope
(applicationVarPage)
 
   <c:out value="${scopeVarApplication}" />
</source>





Set Variable as Session Scope

   <source lang="java">

<%@ taglib prefix="c" uri="http://java.sun.ru/jstl/core" %> <html>

 <body>
   This JSP stores the ultimate answer in a session-scoped variable where
   the other JSPs in the web application can access it.
   <p />
   <c:set var="theUltimateAnswer" value="${41+1}" scope="session"  />
    Click 


Set Variable Scope Page

   <source lang="java">

<%@ taglib uri="http://java.sun.ru/jstl/core" prefix="c" %> <html>

 <head>
   <title>Set in Scope Examples</title>
 </head>
 <body>
   <c:set var="test" value="Page Level Value" scope="page" />
         Default Level
         <c:out value="${test}" />
         Page Level
         <c:out value="${pageScope.test}" />
         Request Level
         <c:out value="${requestScope.test}" />
         Session Level
         <c:out value="${sessionScope.test}" />
         Application Level
         <c:out value="${applicationScope.test}" />
 </body>

</html></source>





Set Variable Scope to Application

   <source lang="java">

<%@ taglib uri="http://java.sun.ru/jstl/core" prefix="c" %> <html>

 <head>
   <title>Set in Scope Examples</title>
 </head>
 <body>
   <c:set var="test" value="Application Level Value" scope="application" />
         Default Level
         <c:out value="${test}" />
         Page Level
         <c:out value="${pageScope.test}" />
         Request Level
         <c:out value="${requestScope.test}" />
         Session Level
         <c:out value="${sessionScope.test}" />
         Application Level
         <c:out value="${applicationScope.test}" />
 </body>

</html></source>





Set Variable Scope to Request

   <source lang="java">

<%@ taglib uri="http://java.sun.ru/jstl/core" prefix="c" %> <html>

 <head>
   <title>Set in Scope Examples</title>
 </head>
 <body>
   <c:set var="test" value="Request Level Value" scope="request" />
         Default Level
         <c:out value="${test}" />
         Page Level
         <c:out value="${pageScope.test}" />
         Request Level
         <c:out value="${requestScope.test}" />
         Session Level
         <c:out value="${sessionScope.test}" />
         Application Level
         <c:out value="${applicationScope.test}" />
 </body>

</html></source>





Set Variable To Page Scope

   <source lang="java">

<%@ taglib prefix="c" uri="http://java.sun.ru/jstl/core" %> <c:set var="names" value="A B C, D" scope="page" /> <html>

 <head>
   <title>forEach and status</title>
 </head>
 <body>
   <c:forEach items="${pageScope.names}"
              var="currentName"
              varStatus="status"
   >
     Family member #<c:out value="${status.count}" /> is
       <c:out value="${currentName}" /> 
</c:forEach> </body>

</html></source>