Java Tutorial/JSTL/Set
Содержание
- 1 Do Calculation In Set
- 2 EL Expression Examples
- 2.1 Arithmetic Operators in Expressions
- 2.2 JSTL Set With Body
- 2.3 JSTL Set Without Body
- 2.4 Remove Variable
- 2.5 Set Comma Delimited String to Variable
- 2.6 Set Value for Variable
- 2.7 Set Variable as Session Scope
- 2.8 Set Variable Scope Page
- 2.9 Set Variable Scope to Application
- 2.10 Set Variable Scope to Request
- 2.11 Set Variable To Page Scope
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" />
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}" /> |
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>