JSTL使用入門(續1)

來源:互聯網
上載者:User

file name:ImplicitObjectScope.jsp

<%@ page contentType="text/html; charset=GBK" %>
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
<jsp:useBean id="simpleBean" scope="request" class="jsptag.SimpleBean">
</jsp:useBean>
<html>
<head>
<title>
ImplicitObjectScope
</title>
</head>
<body bgcolor="#ffffff">
<h3>
ImplicitObjectScope.jsp
</h3>
<p><strong>
 首先設定範圍分別為application/session/request/page的四個變數
</strong></p>
<c:set var="applicationVar" value="applicationVarValue" scope="application"/>
<c:set var="sessionVar" value="sessionVarValue" scope="session"/>
<c:set var="requestVar" value="requestVarValue" scope="request"/>
<c:set var="pageVar" value="pageVarValue" scope="page"/>
<%//同上等效
/*
session.setAttribute("sessionVar","sessionVarValue");
application.setAttribute("applicationVar","applicationVarValue");
request.setAttribute("requestVar","requestVarValue");
pageContext.setAttribute("pageVar","pageVarValue");
*/%>
session variable value:
<c:out value="${sessionScope.sessionVar}" default=" there is not session  variable"/>
<br />
application variable value:
<c:out value="${applicationScope.applicationVar}" >
  there is not application variable"
</c:out>
<br />
request variable value:
<c:out value="${requestScope.requestVar}">
  there is not request variable
</c:out>
<br />
page variable value:
<c:out value="${pageScope.pageVar}">
  there is not page variable
</c:out>
<p>
<form action="ImplicitObjectScope.jsp" method="post">
  <input type="hidden" name="requestName" value="this is the value of requestName"/>
  <input type="hidden" name="isForward" value="Y"/>
  <input type="submit" value="點擊此處提交到本頁,並將請求轉寄到ImplicitObjectScope1頁面"/>
</form>
<form action="ImplicitObjectScope.jsp" method="post">
  <input type="hidden" name="requestName" value="this is the value of requestName"/>
  <input type="hidden" name="isForward" value="Y"/>
  <input type="hidden" name="isForwarAgain" value="Y"/>
  <input type="submit" value="點擊此處提交到本頁,並將請求轉寄到ImplicitObjectScope1頁面,並再次將請求轉寄到destOfForward頁面"/>
</form>
<form action="ImplicitObjectScope1.jsp" method="post">
  <input type="hidden" name="requestName" value="this is the value of requestName"/>
  <input type="submit" value="點擊此處,直接提交到ImplicitObjectScope1頁面"/>
</form>
<form action="ImplicitObjectScope1.jsp" method="post">
  <input type="hidden" name="requestName" value="this is the value of requestName"/>
  <input type="hidden" name="isForwarAgain" value="Y"/>
  <input type="submit" value="點擊此處,直接提交到ImplicitObjectScope1頁面,再將請求轉寄到destOfForward頁面"/>
</form>
</p>
<c:if test="${not empty param.isForward}">
 <jsp:forward page="ImplicitObjectScope1.jsp" />
</c:if>
<p>
  以下為使用forEach 標記實現累加的範例(EL結合JavaBean的使用)。
</p>
<c:set value="0" var="count">
</c:set>
<table border="1">
 <tr>
  <td>index</td><td>number</td><td>sum</td>
 </tr>
<c:forEach items="${simpleBean.numberList}" var="number" varStatus="status">
 <tr>
  <td><c:out value="${status.index}"/></td>
  <td>
  <c:out value="${number}"/>
  <c:set var="count2" value="${count}"/>
  <c:set var="count" value="${count2+number}"/>
  </td>
  <td>count:<c:out value="${count}"/></td>
 </tr>
</c:forEach>
</body>
</html>

file name:ImplicitObjectScope1.jsp
<%@ page contentType="text/html; charset=GBK" %>
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
<jsp:useBean id="simpleBean" scope="request" class="jsptag.SimpleBean">
</jsp:useBean>
<jsp:setProperty name="simpleBean" property="*" />
<c:if test="${not empty param.isForwarAgain}">
 <jsp:forward page="destOfForward.jsp"/>
</c:if>
<html>
<head>
<title>
ImplicitObjectScope1
</title>
</head>
<body bgcolor="#ffffff">
<h3>
ImplicitObjectScope1.jsp
</h3>
<p>
 注意比較不同範圍的變數,其值有無變化
</p>
<p>
session variable value:
<c:out value="${sessionScope.sessionVar}" default=" there is not session  variable"/>
<br />
application variable value:
<c:out value="${applicationScope.applicationVar}" >
  there is not application variable"
</c:out>
<br />
request variable value:
<c:out value="${requestScope.requestVar}">
  there is not request variable
</c:out>
<br />
page variable value:
<c:out value="${pageScope.pageVar}">
  there is not page variable
</c:out>
</p>
<p>
如下,為EL結合JavaBean的例子:
<br />
output value of reqeustName:
<c:out value="${simpleBean.requestName}">
  the simpleBean.requestName is not existed.
</c:out>
</p>
</table>
</body>
</html>

file name:destOfForward.jsp
<%@ page contentType="text/html; charset=GBK" %>
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
<jsp:useBean id="simpleBean" scope="page" class="jsptag.SimpleBean"/>
<%--試比較將scope改為request有何不同--%>
<%--jsp:useBean id="simpleBean" scope="page" class="jsptag.SimpleBean"/--%>
<html>
<head>
<title>
destOfForward
</title>
</head>
<body bgcolor="#ffffff">
<h3>
destOfForward.jsp
</h3>
<p>
注意比較此處輸出的變化
<br />
request variable:
<strong>
<c:out value="${requestScope.requestVar}">
  there is not request variable
</c:out>
</strong>
</p>
<br />
output value of reqeustName:
<strong>
<c:out value="${simpleBean.requestName}">
  the simpleBean.requestName is not existed.
</c:out>
</strong>
<br />
<p>
注意:在這裡,JavaBean(SimpleBean)的scope為page,與前頁為request不同,所以輸出為不存在的資訊。
</p>
</body>
</html>
file name:SimpleBean.java
package jsptag;

/**
 * <p>Title: </p>
 * <p>Description: A Simple Java Bean</p>
 * <p>Copyright: Copyright (c) 2004</p>
 * <p>Company: </p>
 * @author zqy
 * @version 1.0
 */
import java.util.ArrayList;
public class SimpleBean {

 private String requestName = null;
 private ArrayList numberList=new ArrayList();

 public SimpleBean() {
  for(int i=1;i<=10;i++)
   this.numberList.add(String.valueOf(i));
 }

 public void setRequestName(String requestName) {
  this.requestName = requestName;
 }

 public String getRequestName() {
  return this.requestName;
 }

 public void setNumberList(ArrayList numberList)
 {
  this.numberList=numberList;
 }

 public ArrayList getNumberList()
 {
  return this.numberList;
 }
}

補:
下面這段話關於如何區別page scope和request scope:
"Objects are created within a JSP page instance that is responding to a request object. There are several scopes:
 page - Objects with page scope are accessible only within the page where they are created. All references to such an object shall be released after the response is sent back to the client from the JSP page or the request is forwarded somewhere else. References to objects with page scope are stored in the pageContext object.
 request - Objects with request scope are accessible from pages processing the same request where they were created. References to the object shall be re-leased after the request is processed. In particular, if the request is forwarded to a resource in the same runtime, the object is still reachable. References to objects with request scope are stored in the request object."

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.