`

zk EL表达式中函数的使用与自定义

    博客分类:
  • ZK
阅读更多

  可使用函数列表将在最后列出,首先我们通过一个例子来了解如何使用EL函数与自定义EL函数


 下面是一个国际化的例子,在代码开始部分有段代码 title="${c:l('zkway.title')}" ,

代码的作用是输出国际化了的zkway.title,那么在使用${c:l('zkway.title')}之前

,我们要像使用jsp里的 jstl 的 c 标签一样,先用taglib指令c标签导入进来,在zk中是像代码开始部分那样,

 

<?taglib uri="http://www.zkoss.org/dsp/web/core" prefix="c" ?>

 

   那么在其他地方就可以诸如${c:l('zkway.title')}使用c标签了

  你可以在ZUML页面的任何部分使用EL表达式,但除了属性的名字(names of attributes),元素(elements)和处理指令(processing instruction)。

 

<?xml version="1.0" encoding="utf-8"?>
<?taglib uri="http://www.zkoss.org/dsp/web/core" prefix="c" ?>
<?page id="indexPage" title="${c:l('zkway.title')}" cacheable="false" language="xul/html" zscriptLanguage="Java" contentType="text/html;charset=UTF-8"?>
<?meta name="keywords" content="zkway,zk"?>
<?link rel="shortcut icon" type="image/x-icon" href="logo.png"?>
<?component name="leftmenu"  inline="true"  macroURI="/WEB-INF/content/menu.zul"?>
<window id="indexWin" xmlns:h="http://www.w3.org/1999/xhtml"
	xmlns:n="http://www.zkoss.org/2005/zk/native" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:w="http://www.zkoss.org/2005/zk/client">
	<script type="text/javascript" src="/js/zkway.js">
	</script>
	<style src="/css/global.css">
	</style>
	<!-- borderlayout如果有父节点,那么必须设置height属性,否则不显示 -->
	<borderlayout height="1800px">
		<!-- 北(上) -->
		<north height="100px" style="background-image:url(images/bg.png);">
		</north>
		<!-- 西(左) -->
		<west width="200px" collapsible="true" title="${c:l('menubar')}">
			<div>
				<leftmenu />
			</div>
		</west>
		<!-- 中 -->
		<center>
			<div>
				<h:table width="100%" cellspacing="0" borde="0" height="25px"
					style="background-color:#f5f9fa;border-bottom:1px solid #9ECAD8;">
					<h:tr>
						<h:td style="background:url('images/home.png') no-repeat ;">
							<label value="欢迎您" style="margin-left:30px;" />
							<space></space>
							<!-- 此处使用el表达式,是为了方便使用数据库时,动态显示登录用户 -->
							<label value="${'sunflower'}"></label>
						</h:td>
						<h:td align="right">
							<label style="margin-right:10px;">
								<attribute name="onCreate">
									{
										self.value=org.zkway.util.DateUtils.formatNow("yyyy年MM月dd日");
									}
								</attribute>
							</label>
						</h:td>
					</h:tr>
				</h:table>
				<include id="centerInclude" src="welcome.zul" sclass="inc_center"></include>
			</div>
		</center>
		<!-- 南(下) -->
		<south height="40px"></south>
		<!-- 东(右) 
			<east width="80px"></east>
		-->
	</borderlayout>
</window>


 在zk标签库里还有${c:int('123')},${c:string(1234)},${c:cat('我是','sunflower')},${c:cat('我是','sunflower')},


${c:isExplorer7()}等等,本文最后将列举了所有可用的函数,


我们如何自定义自己的函数呢?


首先我们像引用taglib指令那样,在开始部分添加xel-method指令,我们以org.zkway.util.DateUtils.formatNowformatNow方法为例(DateUtils具体见这里),formatNow是一个接受字符串日期模式的函数,它格式化当前时间,返回一个String类型的日期,参数与java.text.SimpleDateFormat(String pattern)的构造器是一样的,


<?xel-method prefix="c" name="formatNow" class="org.zkway.util.DateUtils"  signature="java.lang.String formatNow(java.lang.String)"?>


我解释一下这个指令的意思,prefix与jsp里的taglib指令的prefix是一样的,表示使用时的前缀,name是具体使用时的函数名字,例如(${c:formatNow("yyyy年MM月dd日")}),class指定哪一个类,signature指定了方法签名,联合起来就是说把class="org.zkway.util.DateUtils" 中方法签名为java.lang.String formatNow(java.lang.String)的方法注册到当前环境中来,el函数名字为formatNow,引用前缀为c(当然也可以不使用,本例仅为了说明可以和taglib指令的prefix的名字相同)

 

注意xel-method指令注册的方法必须是public static方法


上部的例子可以修改为

 

 

<?xml version="1.0" encoding="utf-8"?>
<?taglib uri="http://www.zkoss.org/dsp/web/core" prefix="c" ?>
<?page id="indexPage" title="${c:l('zkway.title')}" cacheable="false" language="xul/html" zscriptLanguage="Java" contentType="text/html;charset=UTF-8"?>
<?meta name="keywords" content="zkway,zk"?>
<?link rel="shortcut icon" type="image/x-icon" href="logo.png"?>
<?component name="leftmenu"  inline="true"  macroURI="/WEB-INF/content/menu.zul"?>
<?xel-method prefix="c" name="formatNow" class="org.zkway.util.DateUtils"  signature="java.lang.String formatNow(java.lang.String)"?>
<window id="indexWin" xmlns:h="http://www.w3.org/1999/xhtml"
	xmlns:n="http://www.zkoss.org/2005/zk/native" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:w="http://www.zkoss.org/2005/zk/client">
	<script type="text/javascript" src="/js/zkway.js">
	</script>
	<style src="/css/global.css">
	</style>
	<!-- borderlayout如果有父节点,那么必须设置height属性,否则不显示 -->
	<borderlayout height="1800px">
		<!-- 北(上) -->
		<north height="100px" style="background-image:url(images/bg.png);">
		
		</north>
		<!-- 西(左) -->
		<west width="200px" collapsible="true" title="${c:l('menubar')}">
			<div>
				<leftmenu />
			</div>
		</west>
		<!-- 中 -->
		<center>
			<div>
				<h:table width="100%" cellspacing="0" borde="0" height="25px"
					style="background-color:#f5f9fa;border-bottom:1px solid #9ECAD8;">
					<h:tr>
						<h:td style="background:url('images/home.png') no-repeat ;">
							<label value="欢迎您" style="margin-left:30px;" />
							<space></space>
							<!-- 此处使用el表达式,是为了方便使用数据库时,动态显示登录用户 -->
							<label value="${'sunflower'}"></label>
						</h:td>
						<h:td align="right">
							<label style="margin-right:10px;" value="${c:formatNow('yyyy年MM月 HH:mm:ss')}">
							</label>
						</h:td>
					</h:tr>
				</h:table>
				<include id="centerInclude" src="welcome.zul" sclass="inc_center"></include>
			</div>
		</center>
		<!-- 南(下) -->
		<south height="40px"></south>
		<!-- 东(右) 
			<east width="80px"></east>
		-->
	</borderlayout>
</window>
 

 

 

以下是zk中可用的el函数(别忘了引用taglib指令哦)

 

 

<!--                 -->
<!-- Class Utilities -->
<!--                 -->
<function>
	<name>boolean</name>
	<function-class>org.zkoss.xel.fn.CommonFns</function-class>
	<function-signature>boolean toBoolean(java.lang.Object)</function-signature>
	<description>
	Converts the specified object to a boolean.
	</description>
</function>
<function>
	<name>number</name>
	<function-class>org.zkoss.xel.fn.CommonFns</function-class>
	<function-signature>java.lang.Number toNumber(java.lang.Object)</function-signature>
	<description>
	Converts the specified object to a number.
	</description>
</function>
<function>
	<name>int</name>
	<function-class>org.zkoss.xel.fn.CommonFns</function-class>
	<function-signature>int toInt(java.lang.Object)</function-signature>
	<description>
	Converts the specified object to an integer.
	</description>
</function>
<function>
	<name>decimal</name>
	<function-class>org.zkoss.xel.fn.CommonFns</function-class>
	<function-signature>java.math.BigDecimal toDecimal(java.lang.Object)</function-signature>
	<description>
	Converts the specified object to a (big) decimal.
	</description>
</function>
<function>
	<name>string</name>
	<function-class>org.zkoss.xel.fn.CommonFns</function-class>
	<function-signature>java.lang.String toString(java.lang.Object)</function-signature>
	<description>
	Converts the specified object to a string.
	</description>
</function>
<function>
	<name>char</name>
	<function-class>org.zkoss.xel.fn.CommonFns</function-class>
	<function-signature>char toChar(java.lang.Object)</function-signature>
	<description>
Converts the specified object to a character.
	</description>
</function>
<function>
	<name>class</name>
	<function-class>org.zkoss.lang.Classes</function-class>
	<function-signature>java.lang.Class forNameByThread(java.lang.String)</function-signature>
	<description>
Returns the class of the specified class name.
	</description>
</function>
<function>
	<name>isInstance</name>
	<function-class>org.zkoss.xel.fn.CommonFns</function-class>
	<function-signature>boolean isInstance(java.lang.Object, java.lang.Object)</function-signature>
	<description>
Tests whether an object (the second argument) is an instance of a class (the first argument).
You could specify a class or the class name as the first argument.
	</description>
</function>
<function>
	<name>length</name>
	<function-class>org.zkoss.xel.fn.CommonFns</function-class>
	<function-signature>int length(java.lang.Object)</function-signature>
	<description>
Returns the length of a string, array, collection or map.
	</description>
</function>
<function>
	<name>new</name>
	<function-class>org.zkoss.xel.fn.CommonFns</function-class>
	<function-signature>java.lang.Object new_(java.lang.Object)</function-signature>
	<description>
Instantiates the specified class.
The argument could be either a string (class name) or a Class instance.
	</description>
</function>

<function>
	<name>property</name><!-- since 3.5.1 -->
	<function-class>org.zkoss.lang.Library</function-class>
	<function-signature>java.lang.String getProperty(java.lang.String)</function-signature>
	<description>
Returns the library property.
	</description>
</function>
<function><!-- backward compatible -->
	<name>getProperty</name>
	<function-class>org.zkoss.lang.Library</function-class>
	<function-signature>java.lang.String getProperty(java.lang.String)</function-signature>
	<description>
Returns the library property.
	</description>
</function>

<!--                  -->
<!-- String Utilities -->
<!--                  -->
<function>
	<name>eatQuot</name>
	<function-class>org.zkoss.xel.fn.StringFns</function-class>
	<function-signature>
java.lang.String eatQuot(java.lang.String)
	</function-signature>
	<description>
Eliminates single and double quotations to avoid JavaScript injection.
	</description>
</function>
<function>
	<name>cat</name>
	<function-class>org.zkoss.xel.fn.StringFns</function-class>
	<function-signature>
java.lang.String cat(java.lang.String, java.lang.String)
	</function-signature>
	<description>
Catenates two strings. Note: null is considered as empty.
	</description>
</function>
<function>
	<name>cat3</name>
	<function-class>org.zkoss.xel.fn.StringFns</function-class>
	<function-signature>
java.lang.String cat3(java.lang.String, java.lang.String, java.lang.String)
	</function-signature>
	<description>
Catenates three strings. Note: null is considered as empty.
	</description>
</function>
<function>
	<name>cat4</name>
	<function-class>org.zkoss.xel.fn.StringFns</function-class>
	<function-signature>
java.lang.String cat4(java.lang.String, java.lang.String, java.lang.String, java.lang.String)
	</function-signature>
	<description>
Catenates four strings. Note: null is considered as empty.
	</description>
</function>
<function>
	<name>cat5</name>
	<function-class>org.zkoss.xel.fn.StringFns</function-class>
	<function-signature>
java.lang.String cat5(java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String)
	</function-signature>
	<description>
Catenates five strings. Note: null is considered as empty.
	</description>
</function>
<function>
	<name>replace</name>
	<function-class>org.zkoss.xel.fn.StringFns</function-class>
	<function-signature>
java.lang.String replace(java.lang.String, java.lang.String, java.lang.String)
	</function-signature>
	<description>
Replaces all occurenances of the second argument with the third argument.
	</description>
</function>

<function>
	<name>l</name>
	<function-class>org.zkoss.xel.fn.CommonFns</function-class>
	<function-signature>java.lang.String getLabel(java.lang.String)</function-signature>
	<description>
	Returns the label of the specified key.
	</description>
</function>
<function>
	<name>l2</name>
	<function-class>org.zkoss.xel.fn.CommonFns</function-class>
	<function-signature>java.lang.String getLabel(java.lang.String, java.lang.Object[])</function-signature>
	<description>
	Returns the label of the specified key, and formats with the specified
	argument.
	</description>
</function>

<!--                    -->
<!-- XML/HTML Utilities -->
<!--                    -->
<function>
	<name>attr</name>
	<function-class>org.zkoss.xel.fn.XmlFns</function-class>
	<function-signature>
java.lang.String attr(java.lang.String, java.lang.Object)
	</function-signature>
	<description>
Generates an attribute for HTML/XML, name="value".
If value is null or empty (if String), "" is generated.
	</description>
</function>

<!--                -->
<!-- HTTP Utilities -->
<!--                -->
<function>
	<name>encodeURL</name>
	<function-class>org.zkoss.web.fn.ServletFns</function-class>
	<function-signature>
java.lang.String encodeURL(java.lang.String)
	</function-signature>
	<description>
Encoding URL to prefix the context path and to provide session info,
if necessary
If URI contains "*", it is resolved to the current Locale and
the browser code.
	</description>
</function>
<function>
	<name>encodeURIComponent</name>
	<function-class>org.zkoss.web.servlet.http.Encodes</function-class>
	<function-signature>
java.lang.String encodeURIComponent(java.lang.String)
	</function-signature>
	<description>
Encoding a string to be used as a query name or value.
	</description>
</function>

<function>
	<name>escapeXML</name>
	<function-class>org.zkoss.xml.XMLs</function-class>
	<function-signature>
java.lang.String escapeXML(java.lang.String)
	</function-signature>
	<description>
Encodes a string that special characters are quoted to be compatible
with HTML/XML.
	</description>
</function>

<function>
	<name>browser</name>
	<function-class>org.zkoss.web.fn.ServletFns</function-class>
	<function-signature>
boolean isBrowser(java.lang.String)
	</function-signature>
	<description>
Whether the current request is coming from the browser of the specified
type.
@param type the type of the browser.
	Allowed values include "robot", "ie", "ie6", "ie6-", "ie7", "ie8",
	"ie7-", "gecko", "gecko2", "gecko3", "gecko2-",
	"opara", "safari",
	"mil", "hil", "mil-".<br/>
	Note: "ie6-" means Internet Explorer 6 only; not Internet Explorer 7
	or other.
	</description>
</function>
<function>
	<name>isExplorer</name>
	<function-class>org.zkoss.web.fn.ServletFns</function-class>
	<function-signature>
boolean isExplorer()
	</function-signature>
	<description>
Whether the current request is coming from Internet Explorer.
	</description>
</function>
<function>
	<name>isExplorer7</name>
	<function-class>org.zkoss.web.fn.ServletFns</function-class>
	<function-signature>
boolean isExplorer7()
	</function-signature>
	<description>
Whether the current request is coming from Internet Explorer 7 or later.
	</description>
</function>
<function>
	<name>isGecko</name>
	<function-class>org.zkoss.web.fn.ServletFns</function-class>
	<function-signature>
boolean isGecko()
	</function-signature>
	<description>
Whether the current request is coming from a Gecko-based browser,
such as Mozilla, Firefox and Camino.
	</description>
</function>
<function>
	<name>isGecko3</name>
	<function-class>org.zkoss.web.fn.ServletFns</function-class>
	<function-signature>
boolean isGecko3()
	</function-signature>
	<description>
Whether the current request is coming from a Gecko 3-based browser,
such as Firefox 3.
	</description>
</function>
<function>
	<name>isSafari</name>
	<function-class>org.zkoss.web.fn.ServletFns</function-class>
	<function-signature>
boolean isSafari()
	</function-signature>
	<description>
Whether the current request is coming from Safari.
	</description>
</function>
<function>
	<name>isOpera</name>
	<function-class>org.zkoss.web.fn.ServletFns</function-class>
	<function-signature>
boolean isOpera()
	</function-signature>
	<description>
Whether the current request is coming from Opera.
	</description>
</function>

<function>
	<name>render</name>
	<function-class>org.zkoss.web.fn.ServletFns</function-class>
	<function-signature>
void render(org.zkoss.web.servlet.dsp.action.ActionContext)
	</function-signature>
	<description>
Renders a DSP fragment.
	</description>
</function>

<function>
	<name>getCurrentLocale</name>
	<function-class>org.zkoss.util.Locales</function-class>
	<function-signature>
java.util.Locale getCurrent()
	</function-signature>
	<description>
Returns the locale for the current request.
	</description>
</function>
<function>
	<name>testCurrentLocale</name>
	<function-class>org.zkoss.util.Locales</function-class>
	<function-signature>
boolean testCurrent(java.lang.String, java.lang.String)
	</function-signature>
	<description>
Returns whether the current locale belongs to the specified
language and/or country.

@param lang the language code, e.g., en and zh. Ignored if null.
@param country the country code, e.g., US. Ignored if null.
	If empty, it means no country code at all.
	</description>
</function>
 

 

 

 

 

 

 

 

 

 

 

 

2
0
分享到:
评论
2 楼 name='大虾'?name:大虾 2012-09-26  
虽然表达的一般,但身为IT民工,不能辜负我的头衔,理解能力还是有的 
1 楼 tsinglongwu 2011-06-14  
分析的很细致,很受用。。

相关推荐

    JAVA上百实例源码以及开源项目源代码

     Java非对称加密源程序代码实例,本例中使用RSA加密技术,定义加密算法可用 DES,DESede,Blowfish等。  设定字符串为“张三,你好,我是李四”  产生张三的密钥对(keyPairZhang)  张三生成公钥(publicKeyZhang...

    java开源包8

    开发它是用于在UTF-8 Oracle实例中使用ASCII编码的Oracle 数据库中来正确的传输非ASCII字符。 Java模板语言 Beetl Beetl,是Bee Template Language的缩写,它绝不是简单的另外一种模板引擎,而是新一代的模板引擎,...

    java开源包1

    开发它是用于在UTF-8 Oracle实例中使用ASCII编码的Oracle 数据库中来正确的传输非ASCII字符。 Java模板语言 Beetl Beetl,是Bee Template Language的缩写,它绝不是简单的另外一种模板引擎,而是新一代的模板引擎,...

    JAVA上百实例源码以及开源项目

     Java非对称加密源程序代码实例,本例中使用RSA加密技术,定义加密算法可用 DES,DESede,Blowfish等。  设定字符串为“张三,你好,我是李四”  产生张三的密钥对(keyPairZhang)  张三生成公钥(publicKeyZhang...

    java开源包11

    开发它是用于在UTF-8 Oracle实例中使用ASCII编码的Oracle 数据库中来正确的传输非ASCII字符。 Java模板语言 Beetl Beetl,是Bee Template Language的缩写,它绝不是简单的另外一种模板引擎,而是新一代的模板引擎,...

    java开源包2

    开发它是用于在UTF-8 Oracle实例中使用ASCII编码的Oracle 数据库中来正确的传输非ASCII字符。 Java模板语言 Beetl Beetl,是Bee Template Language的缩写,它绝不是简单的另外一种模板引擎,而是新一代的模板引擎,...

    java开源包3

    开发它是用于在UTF-8 Oracle实例中使用ASCII编码的Oracle 数据库中来正确的传输非ASCII字符。 Java模板语言 Beetl Beetl,是Bee Template Language的缩写,它绝不是简单的另外一种模板引擎,而是新一代的模板引擎,...

    java开源包6

    开发它是用于在UTF-8 Oracle实例中使用ASCII编码的Oracle 数据库中来正确的传输非ASCII字符。 Java模板语言 Beetl Beetl,是Bee Template Language的缩写,它绝不是简单的另外一种模板引擎,而是新一代的模板引擎,...

    java开源包5

    开发它是用于在UTF-8 Oracle实例中使用ASCII编码的Oracle 数据库中来正确的传输非ASCII字符。 Java模板语言 Beetl Beetl,是Bee Template Language的缩写,它绝不是简单的另外一种模板引擎,而是新一代的模板引擎,...

    java开源包10

    开发它是用于在UTF-8 Oracle实例中使用ASCII编码的Oracle 数据库中来正确的传输非ASCII字符。 Java模板语言 Beetl Beetl,是Bee Template Language的缩写,它绝不是简单的另外一种模板引擎,而是新一代的模板引擎,...

    java开源包4

    开发它是用于在UTF-8 Oracle实例中使用ASCII编码的Oracle 数据库中来正确的传输非ASCII字符。 Java模板语言 Beetl Beetl,是Bee Template Language的缩写,它绝不是简单的另外一种模板引擎,而是新一代的模板引擎,...

    java开源包7

    开发它是用于在UTF-8 Oracle实例中使用ASCII编码的Oracle 数据库中来正确的传输非ASCII字符。 Java模板语言 Beetl Beetl,是Bee Template Language的缩写,它绝不是简单的另外一种模板引擎,而是新一代的模板引擎,...

    java开源包9

    开发它是用于在UTF-8 Oracle实例中使用ASCII编码的Oracle 数据库中来正确的传输非ASCII字符。 Java模板语言 Beetl Beetl,是Bee Template Language的缩写,它绝不是简单的另外一种模板引擎,而是新一代的模板引擎,...

    java开源包101

    开发它是用于在UTF-8 Oracle实例中使用ASCII编码的Oracle 数据库中来正确的传输非ASCII字符。 Java模板语言 Beetl Beetl,是Bee Template Language的缩写,它绝不是简单的另外一种模板引擎,而是新一代的模板引擎,...

    Java资源包01

    开发它是用于在UTF-8 Oracle实例中使用ASCII编码的Oracle 数据库中来正确的传输非ASCII字符。 Java模板语言 Beetl Beetl,是Bee Template Language的缩写,它绝不是简单的另外一种模板引擎,而是新一代的模板引擎,...

Global site tag (gtag.js) - Google Analytics