JSTL使用入门(续1)

该博客主要展示了JSP代码示例,包括设定作用域为application、session、request、page的变量,通过表单提交请求并进行转发,还给出了使用forEach标记实现累加的范例,以及EL结合JavaBean的使用,同时对page scope和request scope进行了区别说明。

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

内容概要:本文提出了一种基于非合作博弈理论的居民负荷分层调度模型,并结合双层鲸鱼优化算法(Two-level Whale Optimization Algorithm)进行高效求解,模型与算法均通过Matlab代码实现。研究针对电力系统中居民侧用电负荷的复杂调度问题,引入非合作博弈机制刻画各用户之间的利益竞争关系,实现负荷的分层优化分配;同时设计双层优化架构,上层优化资源配置,下层模拟用户自主决策行为,提升了模型的实用性与合理性。通过智能优化算法求解多层级、非凸非线性的博弈模型,有效提高了调度方案的收敛性与全局寻优能力,适用于现代智能电网中的需求侧管理与能源优化场景。; 适合人群:具备电力系统基础理论知识和Matlab编程能力,从事智能电网、能源优化调度、需求侧管理、博弈论应用等方向的科研人员、高校研究生及工程技术人员。; 使用场景及目标:①应用于居民区电力负荷的分层优化调度系统设计与仿真分析;②为非合作博弈在多主体能源系统建模中的应用提供方法论支持;③利用双层鲸鱼算法解决具有嵌套结构的复杂双层优化问题,提升求解效率与调度方案的可行性。; 阅读建议:建议读者结合提供的Matlab代码深入理解模型构建逻辑与算法实现流程,重点关注博弈模型的效用函数设计、纳什均衡求解思路以及双层优化结构的迭代机制,宜配合实际用电数据开展复现实验以验证模型有效性与鲁棒性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值