30.53. <w:script>

Script构件对应HTML中的<script>,用来包含一段脚本。与标准Html中的<script>不同,<w:script>的src属性允许使用“/”号指定使用相对于应用的Context Root路径的资源。此外,包含在<w:script>中的脚本中允许使用“@{构件id}”的形式来引用构件的clientId所对应的元素。

30.53.1. 构件信息

表 30.129. 构件信息

Component Typeorg.operamasks.faces.component.html.HtmlScript
Component Familyjavax.faces.Output
Component Classorg.operamasks.faces.component.html.HtmlScript
Tag Classorg.operamasks.faces.webapp.widget.HtmlScriptTag
Renderer Typeorg.operamasks.faces.component.html.HtmlScript
Renderer Class(AJAX)org.operamasks.faces.render.widget.ajax.AjaxHtmlScriptRenderer

继承体系

+java.lang.Object

++javax.faces.component.UIComponent

+++javax.faces.component.UIComponentBase

++++javax.faces.component.UIOutput

+++++org.operamasks.faces.component.html.HtmlScriptBase

++++++org.operamasks.faces.component.html.HtmlScript

30.53.2. 属性

表 30.130. 属性

binding 定义类 javax.faces.component.UIComponentBase
类型 javax.el.ValueExpression(javax.faces.component.UIComponent)
是否必须 延时求值
一个值表达式,用于把该组件链接到一个ManagedBean的某个属性。
id 定义类 javax.faces.component.UIComponentBase
类型 java.lang.String
是否必须 延时求值
这个组件的组件标识符。这个值在最近的命名容器类型的父组件范围内,必须是唯一的。
language 定义类 org.operamasks.faces.component.html.HtmlScript
类型 javax.el.ValueExpression(java.lang.String)
是否必须 延时求值
当前脚本编写用的语言,取值可为:javascript,livescript,vbscript,other。(不推荐使用,建议使用type属性代替)
rendered 定义类 javax.faces.component.UIComponentBase
类型 boolean(boolean)
是否必须 延时求值
一个标志,指出该组件是否要在任何随后的form提交过程中被渲染或处理。 这个属性的缺省值是true。
src 定义类 org.operamasks.faces.component.html.HtmlScript
类型 javax.el.ValueExpression(java.lang.String)
是否必须 延时求值
包含有源代码或数据的外部文件的 URL。若URL以“/”开头,则是相对于应用的Context Root的路径。
type 定义类 org.operamasks.faces.component.html.HtmlScript
类型 javax.el.ValueExpression(java.lang.String)
是否必须 延时求值
关联的脚本引擎的 MIME 类型,取值可以为:text/ecmascript,text/javascript,application/ecmascript,application/javascript,text/vbscript.

30.53.3. 构件API

30.53.4. 示例

下面例子非常简单,用w:script来包含一段javascript代码:

<w:script>
    function test(){
        var text = "hello";
        alert(text);
    }
    test();
</w:script>

在script中的脚本可以使用“@{构件id}”的形式来引用构件对应的页面html元素(即通过构件的clientId查找对应的html元素,注意与jsvar的区别)。

以下代码中的textField将显示“通过构件id引用页面元素测试成功。”。请注意脚本中的@{txt}是对w:textField底层的<input>元素的引用。而w:textField的jsvar(如果已设置),则是对一个Ext.TextField对象的引用。

<w:form id="form">
    <w:textField id="txt" width="400" value="通过构件id引用页面元素测试失败。"/>
</w:form>

<w:script>
    <!--
    Ext.onReady(function(){
        @{txt}.value='通过构件id引用页面元素测试成功。';
    });
     -->
</w:script>