`

zk工具类

    博客分类:
  • ZK
阅读更多

package org.sunflower.demo.web.zk.util;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Map;

import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Desktop;
import org.zkoss.zk.ui.Execution;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.Session;
import org.zkoss.zk.ui.Sessions;
import org.zkoss.zk.ui.WebApp;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zk.ui.util.Clients;
import org.zkoss.zul.Label;
import org.zkoss.zul.Messagebox;

/**
 * ZkUtils集合了zk中常用的一些功能,方便开发中的使用
 * 
 * @author sunflower Email:zhangxuehuaemail # gmail dot com
 * 
 */
public class ZkUtils {
	/**
	 * 获取当前
	 * 
	 * @return
	 */
	public static Execution getExecution() {
		Label lbl = new Label();
		lbl.getValue();
		return Executions.getCurrent();
	}

	public static Desktop getDesktop() {
		return getExecution().getDesktop();
	}

	/**
	 * 返回当前桌面的webapp
	 * 
	 * @return
	 */
	public static WebApp getWebApp() {
		return getDesktop().getWebApp();
	}

	/**
	 * 返回指定key的webapp作用域内的对象
	 * 
	 * @param key
	 * @return
	 */
	public static Object getWebAppAttr(String key) {
		return getWebApp().getAttribute(key);
	}

	/**
	 * 设置指定key的webapp作用域对象
	 * 
	 * @param key
	 * @param value
	 */
	public static void setAppAttr(String key, Object value) {
		getWebApp().setAttribute(key, value);
	}

	public URL getResource(String path) {
		return getWebApp().getResource(path);
	}

	/**
	 * 返回给定路径资源的
	 * 
	 * <p>
	 * Notice that, since 3.6.3, this method can retreive the resource starting
	 * with "~./". If the path contains the wildcard ('*'), you can use
	 * {@link Execution#locate} to convert it to a proper string first.
	 */
	public InputStream getResourceAsStream(String path) {
		return getWebApp().getResourceAsStream(path);
	}

	/**
	 * 返回 path虚拟路径的实际路径 .例如, the path "/index.html" returns the absolute file
	 * path on the server's filesystem would be served by a request for
	 * "http://host/contextPath/index.html", where contextPath is the context
	 * path of this {@link WebApp}.
	 * 
	 * <p>
	 * Notice that ZK don't count on this method to retrieve resources. If you
	 * want to change the mapping of URI to different resources, override
	 * {@link org.zkoss.zk.ui.sys.UiFactory#getPageDefinition} instead.
	 */
	public String getRealPath(String path) {
		return getWebApp().getRealPath(path);
	}

	/**
	 * 返回指定文件的mimetype类型 Returns the MIME type of the specified file, or null if
	 * the MIME type is not known. The MIME type is determined by the
	 * configuration of the Web container.
	 * <p>
	 * Common MIME types are "text/html" and "image/gif".
	 */
	public String getMimeType(String file) {
		return getWebApp().getMimeType(file);
	}

	/**
	 * 获得当前请求的session
	 * 
	 * @return
	 */
	public static Session getSession() {
		return Sessions.getCurrent();
	}

	/**
	 * 设置指定key的对象到session作用域
	 * 
	 * @param name
	 * @param value
	 */
	public static void setSessionAttr(String key, Object value) {
		getSession().setAttribute(key, value);
	}

	/**
	 * 获得从当前request的session中取出指定key的对象
	 * 
	 * @param name
	 * @return
	 */
	public static Object getSessionAttr(String key) {
		return getSession().getAttribute(key);
	}

	/**
	 * 获得当前session作用域所有变量
	 * 
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static Map getSessionAttrs() {
		return getSession().getAttributes();
	}

	/**
	 * 获得指定名称的请求参数
	 * 
	 * @param name
	 *            参数名
	 * @return 参数值
	 */
	public static Object getParameter(String name) {
		return getExecution().getParameter(name);
	}

	/**
	 * 获得所有请求参数
	 * 
	 * @return 参数map
	 */
	public static Object getParamMap() {
		return getExecution().getParameterMap();
	}

	/**
	 * 获得指定名称的请求作用域对象
	 * 
	 * @param name
	 *            请求作用域对象名称
	 * @return 作用域对象
	 */
	public static Object getRequestAttr(String name) {
		return getExecution().getAttribute(name);
	}

	/**
	 * 将指定key的变量设置的到request scope
	 * 
	 * @param name
	 * @param value
	 */
	public static void setRequestAttr(String name, Object value) {
		getExecution().setAttribute(name, value);
	}

	/**
	 * 获得请求作用域所有对象
	 * 
	 * @param name
	 *            请求作用域对象名称
	 * @return 作用域对象
	 */
	@SuppressWarnings("unchecked")
	public static Map getRequestAttrs() {
		return getExecution().getAttributes();
	}

	/**
	 * Sends a temporary redirect response to the client using the specified
	 * redirect location URL.
	 * 
	 * It is the same as sendRedirect(url, null).
	 * 
	 * After calling this method, the caller shall end the processing
	 * immediately (by returning). All pending requests and events will be
	 * dropped.
	 * 
	 * Parameters: uri the URI to redirect to, or null to reload the same page
	 */
	public static void sendRedirect(String uri) {
		getExecution().sendRedirect(uri);
	}

	/**
	 * 请求重定向
	 * 
	 * @param uri
	 *            定向uri
	 * @param target
	 *            显示uri内容的目标窗口,如果target=null,则在当前窗口显示
	 */
	public static void sendRedirect(String uri, String target) {
		getExecution().sendRedirect(uri, target);
	}

	public static void forward(String uri) {
		try {

			getExecution().forward(uri);

		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * Sends the event to the specified component and process it immediately.
	 * This method can only be called when processing an event. It is OK to send
	 * event to component from another page as long as they are in the same
	 * desktop.
	 * <p>
	 * 详细中文解释见<a href="http://zh.zkoss.org/doc/devguide/ch05s03.html">这里</a>
	 */
	public static void sendEvent(Component comp, Event event) {
		Events.sendEvent(comp, event);
	}

	/**
	 * Sends the event the target specified in the event. *
	 * <p>
	 * 详细中文解释见<a href="http://zh.zkoss.org/doc/devguide/ch05s03.html">这里</a>
	 * <p>
	 * Note: {@link Event#getTarget} cannot be null.
	 */
	public static void sendEvent(Event event) {
		Events.sendEvent(event);
	}

	/**
	 * Posts an event to the current execution.
	 * <p>
	 * The priority of the event is assumed to be 0. Refer to
	 * {@link #postEvent(int, Event)}.
	 * 
	 * <p>
	 * On the other hand, the event sent by {@link #sendEvent} is processed
	 * immediately without posting it to the queue.
	 * 
	 * <p>
	 * Note: if the target of an event is not attached to the page yet, the
	 * event is ignored silently.
	 * <p>
	 * 详细中文解释见<a href="http://zh.zkoss.org/doc/devguide/ch05s03.html">这里</a>
	 * 
	 * @see #sendEvent
	 * @see #echoEvent
	 * @see #postEvent(int, Event)
	 */
	public static final void postEvent(Event event) {
		getExecution().postEvent(event);
	}

	/**
	 * Posts an instance of {@link Event} to the current execution.
	 * <p>
	 * The priority of the event is assumed to be 0. Refer to
	 * {@link #postEvent(int, String, Component, Object)}. 详细中文解释见<a
	 * href="http://zh.zkoss.org/doc/devguide/ch05s03.html">这里</a>
	 * 
	 * @see #postEvent(Event)
	 * @see #postEvent(int, String, Component, Object)
	 */
	public static final void postEvent(String name, Component target,
			Object data) {
		postEvent(0, name, target, data);
	}

	/**
	 * Posts an event to the current execution with the specified priority.
	 * 
	 * <p>
	 * The posted events are processed from the higher priority to the lower
	 * one. If two events are posted with the same priority, the earlier the
	 * event being posted is processed earlier (first-in-first-out).
	 * 
	 * <p>
	 * The priority posted by posted by {@link #postEvent(Event)} is 0.
	 * Applications shall not use the priority higher than 10,000 and lower than
	 * -10,000 since they are reserved for component development.
	 * 
	 * @param priority
	 *            the priority of the event.
	 * @since 3.0.7
	 */
	public static final void postEvent(int priority, Event event) {
		getExecution().postEvent(priority, event);
	}

	/**
	 * Posts an instance of {@link Event} to the current execution with the
	 * specified priority.
	 * 
	 * <p>
	 * The posted events are processed from the higher priority to the lower
	 * one. If two events are posted with the same priority, the earlier the
	 * event being posted is processed earlier (first-in-first-out).
	 * 
	 * <p>
	 * The priority posted by posted by {@link #postEvent(Event)} is 0.
	 * Applications shall not use the priority higher than 10,000 and lower than
	 * -10,000 since they are reserved for component development.
	 * 
	 * @param priority
	 *            the priority of the event.
	 * @since 3.0.7
	 */
	public static final void postEvent(int priority, String name,
			Component target, Object data) {
		Events.postEvent(priority, name, target, data);
	}

	/**
	 * Echos an event. By echo we mean the event is fired after the client
	 * receives the AU responses and then echoes back. In others, the event
	 * won't be execute in the current execution. Rather, it executes after the
	 * client receives the AU responses and then echoes back the event back.
	 * 
	 * <p>
	 * It is usually if you want to prompt the user before doing a long
	 * operartion. A typical case is to open a hightlighted window to prevent
	 * the user from clicking any button before the operation gets done.
	 * 
	 * @since 3.0.2
	 * @see #sendEvent
	 * @see #echoEvent
	 * @param name
	 *            the event name, such as onSomething
	 * @param target
	 *            the component to receive the event (never null).
	 * @param data
	 *            the extra information, or null if not available. It will
	 *            become {@link Event#getData}.
	 */
	public static final void echoEvent(String name, Component target,
			String data) {
		Events.echoEvent(name, target, data);
	}

	/**
	 * 消息提示框
	 * <p>
	 * <b style="color:red;"/>注意</b>:自从zk5.0以后,默认禁用本地事件进程,见zk.xml配置文件
	 * &lt;disable-event-thread&gt;true&lt;/disable-event-thread&gt;启用事件处理线程,
	 * 请将true改为false
	 * ,如果此行注释掉或者没有此行,请添加配置&lt;disable-event-thread&gt;false&lt;/disable
	 * -event-thread
	 * &gt;当禁用事件线程时,如果您使用了messagebox的返回值作为判断的话,那么if语句内的代码永远都不会执行。这两个的区别的详细说明见 <a
	 * href="http://sunflowers.iteye.com/blog/686243">这里<a>
	 * 
	 * @param message
	 *            消息内容
	 * @param title
	 *            窗口标题
	 */
	public static void showInformationBox(String message, String title) {
		show(message, title, Messagebox.INFORMATION);
	}

	/**
	 * 询问提示框
	 * <p>
	 * <b style="color:red;"/>注意</b>:自从zk5.0以后,默认禁用本地事件进程,见zk.xml配置文件
	 * &lt;disable-event-thread&gt;true&lt;/disable-event-thread&gt;启用事件处理线程,
	 * 请将true改为false
	 * ,如果此行注释掉或者没有此行,请添加配置&lt;disable-event-thread&gt;false&lt;/disable
	 * -event-thread
	 * &gt;当禁用事件线程时,如果您使用了messagebox的返回值作为判断的话,那么if语句内的代码永远都不会执行。这两个的区别的详细说明见 <a
	 * href="http://sunflowers.iteye.com/blog/686243">这里<a>
	 * 
	 * @param message
	 *            提示内容
	 * @param title
	 *            窗口标题
	 * @return boolean 类型,true确认,false否
	 */
	public static boolean showQuestionBox(String message, String title) {
		try {
			int flag = Messagebox
					.show(message, title, Messagebox.OK | Messagebox.CANCEL,
							Messagebox.QUESTION, Messagebox.CANCEL);
			return flag == Messagebox.OK;
		} catch (InterruptedException e) {
		}
		return false;
	}

	/**
	 * 警告提示框
	 * <p>
	 * <b style="color:red;"/>注意</b>:自从zk5.0以后,默认禁用本地事件进程,见zk.xml配置文件
	 * &lt;disable-event-thread&gt;true&lt;/disable-event-thread&gt;启用事件处理线程,
	 * 请将true改为false
	 * ,如果此行注释掉或者没有此行,请添加配置&lt;disable-event-thread&gt;false&lt;/disable
	 * -event-thread
	 * &gt;当禁用事件线程时,如果您使用了messagebox的返回值作为判断的话,那么if语句内的代码永远都不会执行。这两个的区别的详细说明见 <a
	 * href="http://sunflowers.iteye.com/blog/686243">这里<a>
	 * 
	 * @param message
	 *            警告内容
	 * @param title
	 *            窗口标题
	 */
	public static void showExclamationBox(String message, String title) {
		show(message, title, Messagebox.EXCLAMATION);
	}

	/**
	 * 错误提示框
	 * <p>
	 * <b style="color:red;"/>注意</b>:自从zk5.0以后,默认禁用本地事件进程,见zk.xml配置文件
	 * &lt;disable-event-thread&gt;true&lt;/disable-event-thread&gt;启用事件处理线程,
	 * 请将true改为false
	 * ,如果此行注释掉或者没有此行,请添加配置&lt;disable-event-thread&gt;false&lt;/disable
	 * -event-thread
	 * &gt;当禁用事件线程时,如果您使用了messagebox的返回值作为判断的话,那么if语句内的代码永远都不会执行。这两个的区别的详细说明见 <a
	 * href="http://sunflowers.iteye.com/blog/686243">这里<a>
	 * 
	 * @param message
	 *            提示内容
	 * @param title
	 *            窗口标题
	 */
	public static void showErrorBox(String message, String title) {
		show(message, title, Messagebox.ERROR);
	}

	/**
	 * 显示Information提示框
	 * 
	 * @param message
	 *            提示内容
	 * @param title
	 *            窗口标题
	 * @param icon
	 *            窗口图标:Messagebox.INFORMATION,Messagebox.QUESTION,Messagebox.EXCLAMATION
	 *            ,Messagebox.ERROR
	 */
	private static void show(String message, String title, String icon) {
		try {

			Messagebox.show(message, title, Messagebox.OK, icon);
		} catch (InterruptedException e) {
			// do nothing
		}
	}

	/**
	 * Returns the fully qualified name of the client or the last proxy that
	 * sent the request. If the engine cannot or chooses not to resolve the
	 * hostname (to improve performance), this method returns the dotted-string
	 * form of the IP address.
	 */
	public static String getRemoteHost() {
		return getExecution().getRemoteHost();
	}

	/**
	 * 
	 * 返回请求客户端的IP地址
	 */
	public String getRemoteAddr() {
		return getExecution().getRemoteAddr();
	}

	/**
	 * 返回本地请求对象(即ServletRequest),如果不可用返回null
	 * 
	 * <p>
	 * The returned object depends on the Web container. If it is based Java
	 * servlet container, an instance of javax.servlet.ServletRequest is
	 * returned.
	 */
	public static Object getNativeRequest() {
		return getExecution().getNativeRequest();
	}

	/**
	 * 返回本地响应对象(即ServletResponse),如果不可用返回null
	 * 
	 * <p>
	 * The returned object depends on the Web container. If it is based Java
	 * servlet container, an instance of javax.servlet.ServletResponse is
	 * returned.
	 */
	public static Object getNativeResponse() {
		return getExecution().getNativeResponse();
	}

	/**
	 * 开始处理
	 * <p>
	 * 在浏览器左上角,会出现一个"处理中,请稍后..."提示框 并禁用桌面上所有组件的行为,用户无法操作
	 * 
	 * @since 5.0
	 */
	public static void startProcessing() {
		Clients.evalJavaScript("zk.startProcessing(1)");
	}

	/**
	 * 结束处理
	 * <p>
	 * 隐藏浏览器左上角的"处理中,请稍后..."提示框 ,并启用用桌面上所有组件的行为,允许用户操作
	 * 
	 * @since 5.0
	 */
	public static void hideStartProcessing() {
		Clients.evalJavaScript("zk.endProcessing()");
	}
}
 
3
0
分享到:
评论

相关推荐

    zk工具类 ZkUtils 1.1版本

    NULL 博文链接:https://sunflowers.iteye.com/blog/710734

    ZK自己总结的工具类

    自己总结ZK开发工具类,里面都是一些ZK经常用到的方法

    zoopeeper工具类,操作zk服务的工具

    该jar是用java编写的一个操作zookeeper的工具类,能快速的将远程zk服务节点中的数据导出来,也能快速将数据上传到zk中去!

    dubbo以及ZK的demo

    之前的上传资源中有dubbo,这个保重包含zookeeper的安装包,以及测试demo

    zookeeper工具

    zookeeper Dubbo,zookeeper Dubbozookeeper Dubbozookeeper Dubbo

    zk客户端curator2.11

    客户端是Curator Framework,是Apache的项目,它主要的功能...可以总结Curator主要解决以下三类问题: 封装ZK Client与Server之间的连接处理; 提供了一套Fluent风格的操作API; 提供ZK各种应用场景的抽象封装;

    zookeeperUtils---.zip

    该jar是用java编写的一个操作zookeeper的工具类,能快速的将远程zk服务节点中的数据导出来,也能快速将数据上传到zk中去!

    JAVA实现zookeeper节点批量删除工具类.rar

    使用JAVA代码实现zookeeper服务的节点删除功能,代码带有注释简单易懂

    Zookeeper工具

    Zookeeper工具,常用工具,可用于分布式;Zookeeper工具,常用工具,可用于分布式;Zookeeper工具,常用工具,可用于分布式

    分布式协调工具-ZooKeeper实现动态负载均衡

    管理人员在控制台作的一些操作,实际上是修改了ZK上某些节点的状态,而ZK就把这些变化通知给他们注册Watcher的客户端,即推送系统,于是,作出相应的推送任务。 3. 另一种工作汇报模式:一些类似于任务分发系统,子...

    eclipse工具包

    eclipse工具压缩包,不用安装,下载解压可直接使用,方便,不更改电脑注册比安装版的更好用

    Zk跨子量子点的热功率和热电功率因数

    特别是,r = vn / vc越小,功率因数的不对称性就越大,再加上由于中性模的多重性而导致的电导峰的热展宽,可以为我们提供最终的工具来找出竞争的量子霍尔中的哪个 在实验中确实实现了通用性类。 我们对Z3和Z4差铁...

    zookeeperUtils.zip

    该jar是用java编写的一个操作zookeeper的工具类,能快速的将远程zk服务节点中的数据导出来,也能快速将数据上传到zk中去!

    xjsnark:开发高效zk-SNARK电路的高级框架

    xJsnark 这是用于开发zk-SNARK的应用程序的高级框架... 它只是在此存储库中以带有类文件的jar形式发布,但是某些技术的实现可以在中提供的低级小工具(例如RSA和AES小工具)中找到。 xJsnark产生的电路使用与相同的格式

    Java+八股文面经宝典+资源合集

    JDK是Java开发工具包,包括了Java运行环境JRE、Java工具和Java基础类库;JRE是运行Java程序所必须的环境的集合,包含JVM标准实现及Java核心类库;JVM是Java虚拟机的缩写,是整个Java实现跨平台的最核心的部分,能够...

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

     Java二进制IO类与文件复制操作实例,好像是一本书的例子,源代码有的是独立运行的,与同目录下的其它代码文件互不联系,这些代码面向初级、中级Java程序员。 Java访问权限控制源代码 1个目标文件 摘要:Java源码,...

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

    日历表格面板 [ConfigLine.java] 控制条类 [RoundBox.java] 限定选择控件 [MonthMaker.java] 月份表算法类 [Pallet.java] 调色板,统一配色类 Java扫雷源码 Java生成自定义控件源代码 2个目标文件 Java实现HTTP连接...

    C#支付宝扫码支付代码完整版

    支付宝工具类 using System; using System.Collections.Generic; using System.Linq; using System.Security.Cryptography; using System.Text; using System.Web; namespace ZK_ECommerce.Shop.pay.alipay { ...

    java开源包1

    PortGroper 是一款java写的开源拒绝服务测试工具,它不是僵尸网络类的ddos,而是使用大量的代理作为bots发起DDOS。Port Groper可以与用测试防火墙,干扰web 统计脚本的跟踪,为网站增加流量..往好了用什么都能干,就是...

    java开源包11

    PortGroper 是一款java写的开源拒绝服务测试工具,它不是僵尸网络类的ddos,而是使用大量的代理作为bots发起DDOS。Port Groper可以与用测试防火墙,干扰web 统计脚本的跟踪,为网站增加流量..往好了用什么都能干,就是...

Global site tag (gtag.js) - Google Analytics