练习 3.2:使用内置 EGL 函数

EGL 包括若干个内置函数的库。下面是对其中一些库的简要描述。每个库都有一个相关联的帮助主题。

mathLib
数学和科学函数
strLib
字符串处理函数
javaLib
处理 Java 代码的函数
sysLib
通用函数

另外,EGL 包括了系统变量的 sysVar 库,会在运行时更新这些系统变量。

例如,以下是这些库中一些最常用的函数和变量的描述:

sysLib.size()

此函数返回数据表中的行数或数组中的元素个数。该数组可以是结构项数组、数据项或记录的静态数组或者数据项或记录的动态数组。

sysVar.sessionID

在 Web 应用程序中,此系统变量包含特定于 Web 应用程序服务器会话的标识。可以将此值用作键值来访问在程序之间共享的文件或数据库信息。

strLib.findStr()
此函数搜索子串在字符串中的第一次出现。
VGVar.currentGregorianDate
此变量包含长格里历格式的当前系统日期。程序每次引用此系统变量时,值都会自动地更新。
sysLib.setError()
此函数将消息与 PageHandler 中的某项或 PageHandler 整体相关联。该消息被放在 JSP 中的 JSF 消息标记的位置处,并且在显示相关 Web 页面时显示该消息。
mathLib.sqrt()

此函数返回数字的平方根。它对任何大于等于零的数字执行运算。

J2EELib.getSessionAttr()
此函数使用指定的键来将会话对象的自变量检索到指定的变量中。在 PageHandler 中以及在运行于 Web 应用程序中的程序中,此函数很有用。如果使用指定的键找不到对象,则目标变量不更改。

在此练习中,将创建一个使用其中一些内置函数和变量的页面。

创建页面

  1. 创建一个新的 Faces JSP 文件,创建方法与在该教程中创建其它页面的方法一样。将新的 Faces JSP 文件命名为:

    SystemLibraries

  2. 在新的页面中,删除文本 Place your page content here
  3. 在删除文本的位置,输入 EGL system library examples,然后按 Enter 键。
  4. 右键单击页面,然后单击编辑页代码
  5. 删除页代码文件中的所有代码并用以下代码替换它:
    package pagehandlers;
    
    import data.*;
    
    PageHandler SystemLibraries
      {view = "SystemLibraries.jsp", onPageLoadFunction = onPageLoad}
    
    fields fieldsForDisplay;
    customers customer[];
    
      Function onPageLoad()
        CustomerLib.getAllCustomers(customers);
        J2EELib.setSessionAttr("sess",fields.setSessionAttrValue);
      End
    
    //This is the main function of the PageHandler.
    //It calls the system functions.
      function callEGLSystemFunctions() 
        fields.tableValue = sysLib.Size(Customers);
        fields.sqrtResult = mathLib.sqrt(fields.sqrtValue);
        fields.currentDateValue = VGVar.currentGregorianDate;
        J2EELib.getSessionAttr("sess",fields.getSessionAttrValue);
        fields.sessionIDValue = sysVar.sessionID;
    
        fields.findPositionInString = 1;
        fields.findSuccessFail = strLib.findStr(fields.stringValue,
          fields.findPositionInString,fields.findStringLength,
          fields.findStringValue);
    
      end
    
       End
    
    //Structure of variables used in system calls
    Record fieldsForDisplay type basicRecord 
      tableValue int;
      setSessionAttrValue int {value=1111};
      getSessionAttrValue int;
      sqrtValue int {value=111};
      sqrtResult decimal(7,2);
      currentDateValue char(10);
      stringValue char(222) {value="This is my full character variable."};
      findStringValue char(5) {value="full"};
      findPositionInString int {value=1};
      findStringLength int {value=222};
      findSuccessFail int;
      sessionIDValue char(8);
    end

    以下是一些有关刚才插入的代码的技术说明:

  6. 保存并关闭该文件。
  7. 返回到 SystemLibraries.jsp 页面。
  8. 从“页数据”视图中,将 fields 记录拖到页面的底部。
  9. 在“插入控件”窗口中,单击更新现有记录
  10. 单击选项
  11. 清除删除按钮复选框。
  12. 选择提交按钮复选框。
  13. 提交按钮标签字段中,输入运行内置函数
  14. 单击确定
  15. 单击完成
  16. 在页面上的字段下面添加一个新的空白行。
  17. 从“页数据”视图中,将 customers 记录拖到新的空白行上。
  18. 在“插入列表控件”窗口中,单击
  19. 列名列中,选择 first_namelast_name 字段的复选框。
  20. 单击完成
  21. 从“页数据”视图中,将 callEGLSystemFunctions() 函数拖到页面上的“运行内置函数”按钮上。
  22. 保存该页面。
  23. 在服务器上运行该页面。当单击“运行内置函数”按钮时,页面就会显示 EGL 系统函数和变量创建的值。

    该页面看起来应如下所示:

    Web 浏览器中 SystemLibraries.jsp 页面的外观

可以研究帮助主题中的其它 EGL 系统函数。

继续练习 3.3:跟踪会话变量

反馈
(C) Copyright IBM Corporation 2000, 2005. All Rights Reserved.