Java Tutorial/JSTL/Set

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

Do Calculation In Set

<%@ 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>Arithmetic Operators in Expressions</h2>
<c:set var="appleCount" value="${1 + 2 * 4 - 6 / 2}"/>

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





JSTL Set With Body

<%@ taglib uri="http://java.sun.ru/jstl/core" prefix="c" %>
<html>
  <head>
    <title>Set Examples</title>
  </head>
  <body>
  <h3>Set With Body</h3>
  <c:set var="str">Hello, Again World</c:set>
  str = 
  <c:out value="${str}" />
  <br />

  </body>
</html>





JSTL Set Without Body

<%@ taglib uri="http://java.sun.ru/jstl/core" prefix="c" %>
<html>
  <head>
    <title>Set Examples</title>
  </head>
  <body>
  <h3>Set With No Body</h3>
  <c:set var="str" value="Hello World" />
  str = 
  <c:out value="${str}" />
  <br />

  </body>
</html>





Remove Variable

<%@ taglib uri="http://java.sun.ru/jstl/core" prefix="c" %>
<html>
  <head>
    <title>Set Examples</title>
  </head>
  <body>
  <h3>Remove Example</h3>
  <c:set var="test" value="Hello World" scope="page" />
  The value in the variable test before remove is 
  <c:out value="${test}" />
  <br />
  <c:remove var="test" scope="page" />
  The value in the variable test after remove is 
  <c:out value="${test}" />
  <br />
  </body>
</html>





Set Comma Delimited String to Variable

<%@ 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" />
    <table border="0">
      <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>
        <tr>
          <td width="200" bgcolor="<c:out value="${color}"/>"> 
          <c:out value="${i}" />
          </td>
        </tr>
      </c:forEach>
    </table>
  </body>
</html>





Set Value for Variable

<%@ taglib uri="http://java.sun.ru/jstl/core" prefix="c" %>
<table border="1">
  <tr>
    <TH>Scoped Variable</th>
    <TH>Current Value</th>
  </tr>
  <tr>
    <td>
    <b>Page Scope</b>
    (scopeVarPage)</td>
    <td>&#160; 
    <c:out value="${scopeVarPage}" />
    </td>
  </tr>
  <tr>
    <td>
    <b>Request Scope</b>
    (scopeVarRequest)</td>
    <td>&#160; 
    <c:out value="${scopeVarRequest}" />
    </td>
  </tr>
  <tr>
    <td>
    <b>Session Scope</b>
    (scopeVarSession)</td>
    <td>&#160; 
    <c:out value="${scopeVarSession}" />
    </td>
  </tr>
  <tr>
    <td>
    <b>Application Scope</b>
    (applicationVarPage)</td>
    <td>&#160; 
    <c:out value="${scopeVarApplication}" />
    </td>
  </tr>
</table>





Set Variable as Session Scope

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






   
  <!-- start source code -->
   
    <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" />
    <table border="1">
    <tr>
        <td>
          <b>Default Level</b>
        </td>
        <td>
          <c:out value="${test}" />
        </td>
      </tr>
      <tr>
        <td>
          <b>Page Level</b>
        </td>
        <td>
          <c:out value="${pageScope.test}" />
        </td>
      </tr>
      <tr>
        <td>
          <b>Request Level</b>
        </td>
        <td>
          <c:out value="${requestScope.test}" />
        </td>
      </tr>
      <tr>
        <td>
          <b>Session Level</b>
        </td>
        <td>
          <c:out value="${sessionScope.test}" />
        </td>
      </tr>
      <tr>
        <td>
          <b>Application Level</b>
        </td>
        <td>
          <c:out value="${applicationScope.test}" />
        </td>
      </tr>
    </table>
  </body>
</html>





Set Variable Scope to Application

<%@ 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" />
    <table border="1">
    <tr>
        <td>
          <b>Default Level</b>
        </td>
        <td>
          <c:out value="${test}" />
        </td>
      </tr>
      <tr>
        <td>
          <b>Page Level</b>
        </td>
        <td>
          <c:out value="${pageScope.test}" />
        </td>
      </tr>
      <tr>
        <td>
          <b>Request Level</b>
        </td>
        <td>
          <c:out value="${requestScope.test}" />
        </td>
      </tr>
      <tr>
        <td>
          <b>Session Level</b>
        </td>
        <td>
          <c:out value="${sessionScope.test}" />
        </td>
      </tr>
      <tr>
        <td>
          <b>Application Level</b>
        </td>
        <td>
          <c:out value="${applicationScope.test}" />
        </td>
      </tr>
    </table>
  </body>
</html>





Set Variable Scope to Request

<%@ 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" />
    <table border="1">
    <tr>
        <td>
          <b>Default Level</b>
        </td>
        <td>
          <c:out value="${test}" />
        </td>
      </tr>
      <tr>
        <td>
          <b>Page Level</b>
        </td>
        <td>
          <c:out value="${pageScope.test}" />
        </td>
      </tr>
      <tr>
        <td>
          <b>Request Level</b>
        </td>
        <td>
          <c:out value="${requestScope.test}" />
        </td>
      </tr>
      <tr>
        <td>
          <b>Session Level</b>
        </td>
        <td>
          <c:out value="${sessionScope.test}" />
        </td>
      </tr>
      <tr>
        <td>
          <b>Application Level</b>
        </td>
        <td>
          <c:out value="${applicationScope.test}" />
        </td>
      </tr>
    </table>
  </body>
</html>





Set Variable To Page Scope

<%@ 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}" /> <br />
    </c:forEach>
  </body>
</html>