JSTL使用入門(續1)

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."

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章