ZkUtils 1.2.1版,一个zk开发中的工具方法集合

ZkUtils集合了zk中常用的一些工具方法,方便开发中的使用

 

 1.2.1版没添加任何新的工具方法,

 该版本主要改进:结合本人两年多的开发经验

 给一些方法添加细致的注解,使用的同时了解其工作原理

 

 

Java代码   收藏代码
  1. package  org.zkway.common.util.web.zk;  
  2.   
  3. import  java.io.IOException;  
  4. import  java.util.Date;  
  5. import  java.util.Iterator;  
  6. import  java.util.List;  
  7. import  java.util.Map;  
  8.   
  9. import  org.zkoss.zk.au.out.AuSendRedirect;  
  10. import  org.zkoss.zk.ui.Component;  
  11. import  org.zkoss.zk.ui.Desktop;  
  12. import  org.zkoss.zk.ui.Execution;  
  13. import  org.zkoss.zk.ui.Executions;  
  14. import  org.zkoss.zk.ui.Page;  
  15. import  org.zkoss.zk.ui.Session;  
  16. import  org.zkoss.zk.ui.Sessions;  
  17. import  org.zkoss.zk.ui.WebApp;  
  18. import  org.zkoss.zk.ui.event.Event;  
  19. import  org.zkoss.zk.ui.event.EventListener;  
  20. import  org.zkoss.zk.ui.event.Events;  
  21. import  org.zkoss.zk.ui.event.ForwardEvent;  
  22. import  org.zkoss.zk.ui.event.GenericEventListener;  
  23. import  org.zkoss.zk.ui.sys.ExecutionCtrl;  
  24. import  org.zkoss.zk.ui.sys.SessionCtrl;  
  25. import  org.zkoss.zk.ui.util.Clients;  
  26. import  org.zkoss.zul.Messagebox;  
  27. import  org.zkoss.zul.impl.api.InputElement;  
  28.   
  29. /**  
  30.  * ZkUtils集合了zk中常用的一些功能,方便开发中的使用  
  31.  *   
  32.  *   
  33.  * @author sunflower  
  34.  *   
  35.  *   
  36.  *         Email:zhangxuehuaemail # gmail 点 com  
  37.  * @since zk5.0+  
  38.  */   
  39. public   class  ZkUtils {  
  40.     /**  
  41.      *   
  42.      * 获得当前Execution,Execution类似HttpServletRequest  
  43.      * <p>  
  44.      * 客户端请求的执行器(例如ServletRequest),当客户端发送一个请求后,  
  45.      * 服务端负责构造这个Execution对象,获取当前执行的信息,然后服务这个请求  
  46.      * <p>  
  47.      * 一个客户端请求, 例如, HttpServletRequest, 或许关联多个请求 request (  
  48.      * {@link org.zkoss.zk.au.AuRequest}). 但是, 这些 ZK 请求来自的页面 ({@link Page}  
  49.      * )必须具有相同的desktop  
  50.      * <p>  
  51.      * 因为一个请求也许来自http或其他协议,Execution通常一个隔离层  
  52.      *   
  53.      *   
  54.      * @return 当前execution  
  55.      * @see Execution  
  56.      */   
  57.     public   static   final  Execution getCurrentExecution() {  
  58.         return  Executions.getCurrent();  
  59.     }  
  60.   
  61.     /**  
  62.      * 返回当前Exection桌面对象  
  63.      * <p>  
  64.      * 在zk中,具有同一个dom document元素组成了desktop,注意这里的同一个document,  
  65.      * 例如iframe会创建另外一个document, 点击链接打开一个新浏览器窗口,这个窗口里的document与原窗口的document是不同的  
  66.      *   
  67.      * @return  
  68.      * @see Execution#getDesktop()  
  69.      */   
  70.     public   static   final  Desktop getCurrentDesktop() {  
  71.         return  getCurrentExecution().getDesktop();  
  72.     }  
  73.   
  74.     /**  
  75.      * 获得当前Execution所属的会话  
  76.      * <p>  
  77.      * <b>注</b>该Session不同于HttpSession,该session为zk定义的session作用域  
  78.      *   
  79.      * @return  
  80.      * @see Session  
  81.      */   
  82.     public   static   final  Session getCurrentSession() {  
  83.         return  Sessions.getCurrent();  
  84.     }  
  85.   
  86.     /**  
  87.      * 返回本地session对象,如果不可用返回null,返回的对象依赖客户端类型,如果是就http的  
  88.      * ,那么返回javax.servlet.http.HttpSession的一个实例  
  89.      * ,如果是portlet,那么返回javax.portlet.PortletSession的实例  
  90.      */   
  91.     public   static   final  Object getNativeSession() {  
  92.         return  getCurrentSession().getNativeSession();  
  93.     }  
  94.   
  95.     /**  
  96.      * 返回本地请求对象,如果不可用返回null  
  97.      *   
  98.      * @return 返回的对象依赖web容器,如果web容器是一个servlet容器,那么返回的对象为ServletRequest  
  99.      */   
  100.     public   static   final  Object getNativeRequest() {  
  101.         return  getCurrentExecution().getNativeRequest();  
  102.     }  
  103.   
  104.     /**  
  105.      * 返回本地响应对象,如果不可用返回null  
  106.      *   
  107.      * @return 返回的对象依赖web容器,如果web容器是一个servlet容器,那么返回的对象为ServletResponse  
  108.      */   
  109.     public   static   final  Object getNativeResponse() {  
  110.         return  getCurrentExecution().getNativeResponse();  
  111.     }  
  112.   
  113.     /**  
  114.      * 获得WebApp对象,类似HttpServletContext  
  115.      *   
  116.      * @return  
  117.      * @see Desktop#getWebApp()  
  118.      * @see WebApp  
  119.      */   
  120.     public   static   final  WebApp getWebApp() {  
  121.         return  getCurrentDesktop().getWebApp();  
  122.     }  
  123.   
  124.     /**  
  125.      * 返回给定虚拟路径的实际路径  
  126.      *   
  127.      */   
  128.     public   static   final  String getRealPath(String virtualPath) {  
  129.         return  getWebApp().getRealPath(virtualPath);  
  130.     }  
  131.   
  132.     /**  
  133.      * 获得当前请求来自的页面  
  134.      *   
  135.      * @return  
  136.      */   
  137.     public   static   final  Page getCurrentPage() {  
  138.         return  ((ExecutionCtrl) getCurrentExecution()).getCurrentPage();  
  139.     }  
  140.   
  141.     /**  
  142.      * 返回execution作用域内参数  
  143.      *   
  144.      * @return  
  145.      * @see Execution#getArg()  
  146.      */   
  147.     @SuppressWarnings ( "rawtypes" )  
  148.     public   static   final  Map getExecutionArgs() {  
  149.         return  getCurrentExecution().getArg();  
  150.     }  
  151.   
  152.     /**  
  153.      * 获得当前Execution作用域内的属性,类似HttpServletRequest相应方法  
  154.      *   
  155.      * @see Execution#getAttributes()  
  156.      */   
  157.     @SuppressWarnings ( "rawtypes" )  
  158.     public   static   final  Map getExectionAttributes() {  
  159.         return  getCurrentExecution().getAttributes();  
  160.     }  
  161.   
  162.     /**  
  163.      * 设置Execution请求属性值,类似HttpServletRequest相应方法  
  164.      *   
  165.      * @param name  
  166.      *            请求属性  
  167.      * @param value  
  168.      *            属性值  
  169.      */   
  170.     public   static   final   void  setExecutionAttribute(String name, Object value) {  
  171.         getCurrentExecution().setAttribute(name, value);  
  172.     }  
  173.   
  174.     /**  
  175.      * 设置Execution作用域属性值或其父作用域的值,类似HttpServletRequest相应方法  
  176.      *   
  177.      * @param name  
  178.      *            请求属性  
  179.      * @param value  
  180.      *            属性值  
  181.      * @param recurse  
  182.      *            检查父作用域是否存在该属性,如果存在将替换父作用域的值  
  183.      *   
  184.      */   
  185.     public   static   final   void  setExecutionAttribute(String name, Object value,  
  186.             boolean  recurse) {  
  187.         getCurrentExecution().setAttribute(name, value, recurse);  
  188.     }  
  189.   
  190.     /**  
  191.      * 获得Execution请求参数,类似HttpServletRequest相应方法  
  192.      *   
  193.      * @return 参数map  
  194.      */   
  195.     @SuppressWarnings ( "rawtypes" )  
  196.     public   static   final  Map getExecutionParameterMap() {  
  197.         return  getCurrentExecution().getParameterMap();  
  198.     }  
  199.   
  200.     /**  
  201.      * 获得Execution请求参数值,类似HttpServletRequest相应方法  
  202.      *   
  203.      * @param name  
  204.      *            请求参数的名字  
  205.      * @return 指定名字的参数值  
  206.      */   
  207.     public   static   final  String getExecutionParameter(String name) {  
  208.         return  getCurrentExecution().getParameter(name);  
  209.     }  
  210.   
  211.     /**  
  212.      * 获得Execution请求参数值,类似HttpServletRequest相应方法  
  213.      *   
  214.      * @param name  
  215.      *            参数的名字  
  216.      * @return 字符数组  
  217.      */   
  218.     public   static   final  String[] getExecutionParameterValues(String name) {  
  219.         return  getCurrentExecution().getParameterValues(name);  
  220.     }  
  221.   
  222.     /**  
  223.      * 获得当前请求消息头,类似HttpServletRequest相应方法  
  224.      *   
  225.      * @param name  
  226.      *            消息头名字  
  227.      * @return 消息头值  
  228.      */   
  229.     public   static   final  String getRequestHeader(String name) {  
  230.         return  getCurrentExecution().getHeader(name);  
  231.     }  
  232.   
  233.     /**  
  234.      * 返回指定请求头名字的所有值,类似HttpServletRequest相应方法  
  235.      *   
  236.      * @param name  
  237.      *            请求头的名字  
  238.      * @return  
  239.      */   
  240.     @SuppressWarnings ( "rawtypes" )  
  241.     public   static   final  Iterator getRequestHeaders(String name) {  
  242.         return  getCurrentExecution().getHeaders(name);  
  243.     }  
  244.   
  245.     /**  
  246.      * 返回所有请求头名字,类似HttpServletRequest相应方法  
  247.      *   
  248.      * @return  
  249.      */   
  250.     @SuppressWarnings ( "rawtypes" )  
  251.     public   static   final  Iterator getRequestHeaderNames() {  
  252.         return  getCurrentExecution().getHeaderNames();  
  253.     }  
  254.   
  255.     /**  
  256.      * 添加一个指定名称和值的相应头,允许相应头具有多值,类似HttpServletResponse相应方法  
  257.      *   
  258.      * @param name  
  259.      * @param value  
  260.      */   
  261.     public   static   final   void  addResponseHeader(String name, String value) {  
  262.         getCurrentExecution().addResponseHeader(name, value);  
  263.     }  
  264.   
  265.     /**  
  266.      * 添加一个指定名称和日期值的响应头,类似HttpServletResponse相应方法  
  267.      *   
  268.      * @param name  
  269.      * @param value  
  270.      */   
  271.     public   static   final   void  addResponseHeader(String name, Date value) {  
  272.         getCurrentExecution().addResponseHeader(name, value);  
  273.     }  
  274.   
  275.     /**  
  276.      * 返回接收请求的本地ip地址,类似HttpServletRequest相应方法  
  277.      *   
  278.      * @return  
  279.      */   
  280.     public   static   final  String getLocalAddr() {  
  281.         return  getCurrentExecution().getLocalAddr();  
  282.     }  
  283.   
  284.     /**  
  285.      * 获得接收请求的本地host name,类似HttpServletRequest相应方法  
  286.      *   
  287.      * @return  
  288.      */   
  289.     public   static   final  String getLocalName() {  
  290.         return  getCurrentExecution().getLocalName();  
  291.     }  
  292.   
  293.     /**  
  294.      * 获得接收请求的本地端口,类似HttpServletRequest相应方法  
  295.      *   
  296.      * @return  
  297.      */   
  298.     public   static   final   int  getLocalPort() {  
  299.         return  getCurrentExecution().getLocalPort();  
  300.     }  
  301.   
  302.     /**  
  303.      * 获得发送请求的客户端ip,类似HttpServletRequest相应方法  
  304.      *   
  305.      * @return  
  306.      */   
  307.     public   static   final  String getRemoteAddr() {  
  308.         return  getCurrentExecution().getRemoteAddr();  
  309.     }  
  310.   
  311.     /**  
  312.      * 获得发送请求的客户端的host name,类似HttpServletRequest相应方法  
  313.      *   
  314.      * @return  
  315.      */   
  316.     public   static   final  String getRemoteHost() {  
  317.         return  getCurrentExecution().getRemoteHost();  
  318.     }  
  319.   
  320.     /**  
  321.      * 设置session 属性,类似HttpSession相应方法  
  322.      *   
  323.      * @param name  
  324.      *            属性名  
  325.      * @param value  
  326.      *            属性值  
  327.      */   
  328.     public   static   final   void  setSessionAttribute(String name, Object value) {  
  329.         getCurrentSession().setAttribute(name, value);  
  330.     }  
  331.   
  332.     /**  
  333.      * 设置session或父作用域 属性,类似HttpSession相应方法  
  334.      *   
  335.      * @param name  
  336.      *            属性名  
  337.      * @param value  
  338.      *            属性值  
  339.      * @param recurse  
  340.      *            是否查询父作用域包含name名字的属性,如果包含将替换该值  
  341.      */   
  342.     public   static   final   void  setSessionAttribute(String name, Object value,  
  343.             boolean  recurse) {  
  344.         getCurrentSession().setAttribute(name, value, recurse);  
  345.     }  
  346.   
  347.     /**  
  348.      * 返回session作用域对象,类似HttpSession相应方法  
  349.      *   
  350.      * @param name  
  351.      *            属性名  
  352.      * @param recurse  
  353.      *            是否检索父作用域,如果为true, 并且当前作用域没声明这个属性,那么将搜索父作用域  
  354.      * @return  
  355.      */   
  356.     public   static   final  Object getSessionAttribute(String name,  boolean  recurse) {  
  357.         return  getCurrentSession().getAttribute(name, recurse);  
  358.     }  
  359.   
  360.     /**  
  361.      * 返回session作用域对象,类似HttpSession相应方法  
  362.      *   
  363.      * @param name  
  364.      *            属性名  
  365.      * @return  
  366.      */   
  367.     public   static   final  Object getSessionAttribute(String name) {  
  368.         return  getCurrentSession().getAttribute(name);  
  369.     }  
  370.   
  371.     /**  
  372.      * 获得所有session作用域对象,类似HttpSession相应方法  
  373.      *   
  374.      * @return map 类型的作用域所有对象  
  375.      */   
  376.     @SuppressWarnings ( "rawtypes" )  
  377.     public   static   final  Map getSessionAttributes() {  
  378.         return  getCurrentSession().getAttributes();  
  379.     }  
  380.   
  381.     /**  
  382.      * 获得会话超时时间,单位秒,类似HttpSession相应方法  
  383.      *   
  384.      * @return  
  385.      */   
  386.     public   static   final   int  getSessionMaxInactiveInterval() {  
  387.         return  getCurrentSession().getMaxInactiveInterval();  
  388.     }  
  389.   
  390.     /**  
  391.      * 指定失效事件,单位秒,负值表示永不过期,类似HttpSession相应方法  
  392.      *   
  393.      * @param interval  
  394.      */   
  395.     public   static   final   void  setSessionMaxInactiveInterval( int  interval) {  
  396.         getCurrentSession().setMaxInactiveInterval(interval);  
  397.     }  
  398.   
  399.     /**  
  400.      * 销毁当前session  
  401.      * <p>  
  402.      *   
  403.      * 表示解除绑定在session上的所有对象 这里我们要注意: 通常你使用 {@link Executions#sendRedirect}  
  404.      * 让客户端重定向另一个页面(或重加载同一页面) session并不立即销毁 ,而是在当前请求之后销毁,即重定向页面显示完毕之后  
  405.      * 如果想立即销毁((SessionCtrl)Sessions.getCurrent()).invalidateNow();  
  406.      * 在zk中这一点和通常所说的HttpSession.invalidate()有所不同  
  407.      * <p>  
  408.      * 由 天明ゞ破晓 (qq 513062844) 提出的疑问。great thanks  
  409.      */   
  410.     public   static   final   void  invalidateSession() {  
  411.         getCurrentSession().invalidate();  
  412.     }  
  413.   
  414.     /**  
  415.      * 立即销毁当前session  
  416.      * <p>  
  417.      * 非立即销毁session情况见 {@link #invalidateSession()}  
  418.      *   
  419.      */   
  420.     public   static   final   void  invlidateSessionNow() {  
  421.         ((SessionCtrl) getCurrentSession()).invalidateNow();  
  422.     }  
  423.   
  424.     /**  
  425.      * 设置页面作用域属性  
  426.      *   
  427.      * @param name  
  428.      *            属性名  
  429.      * @param value  
  430.      *            属性值  
  431.      */   
  432.     public   static   final   void  setPageAttribute(String name, Object value) {  
  433.         getCurrentPage().setAttribute(name, value);  
  434.     }  
  435.   
  436.     /**  
  437.      *   
  438.      * 设置page或父作用域属性  
  439.      *   
  440.      * @param name  
  441.      * @param value  
  442.      * @param recurse  
  443.      *            是否检索父作用域,如果为true, 并且当前作用域没声明这个属性,那么将搜索父作用域 ,并替换  
  444.      */   
  445.     public   static   final   void  setPageAttribute(String name, Object value,  
  446.             boolean  recurse) {  
  447.         getCurrentPage().setAttribute(name, value, recurse);  
  448.     }  
  449.   
  450.     /**  
  451.      * 获得当前请求来自的页面  
  452.      *   
  453.      * @return  
  454.      */   
  455.     public   static   final  String getRequestPagePath() {  
  456.         return  getCurrentPage().getRequestPath();  
  457.     }  
  458.   
  459.     /**  
  460.      * 获得桌面作用域属性  
  461.      *   
  462.      * @param name  
  463.      * @return  
  464.      */   
  465.     public   static   final  Object getDesktopAttribute(String name) {  
  466.         return  getCurrentDesktop().getAttribute(name);  
  467.     }  
  468.   
  469.     /**  
  470.      * 获得指定id的page  
  471.      * <p>  
  472.      * 需要注意到是:例如在page1中包含iframe,iframe包含的页面为page2,那么zk将为page2新建一个桌面对象desktop2,  
  473.      * 因此page1与page2属于不同的桌面, 当你在page2的一个按钮或所属的其他组件触发的事件中  
  474.      * 使用该方法获得page1的子页面的时候,当前动作请求所属桌面为desktop2,而不是page1所属的desktop1,  
  475.      * 因此你无法从desktop2中查找属于desktop1的页面  
  476.      *   
  477.      * @param pageId  
  478.      *            页面的id  
  479.      * @return 页面对戏那个  
  480.      * @see Desktop#getPage(String)  
  481.      */   
  482.     public   static   final  Page getPage(String pageId) {  
  483.         return  getCurrentDesktop().getPage(pageId);  
  484.     }  
  485.   
  486.     /**  
  487.      * 请求转发  
  488.      * <p>  
  489.      * 调用这个方法的时机:非事件处理内调用,在事件处理时必须调用{@link #sendRedirect(String)}  
  490.      *   
  491.      * @param pageUri  
  492.      * @throws IOException  
  493.      */   
  494.     public   static   final   void  forward(String pageUri)  throws  IOException {  
  495.         Executions.forward(pageUri);  
  496.     }  
  497.   
  498.     /**  
  499.      * 重定向指定页面  
  500.      * <p>  
  501.      * 该方法的行为:如果用户尚未看到响应结果,该方法发送重定向状态码! 如果用户已经看到响应,在用户事件中调用该方法时,zk向客户端发送一个  
  502.      * {@link AuSendRedirect}指令,客户端引擎调用redirect js函数(zAu.cmd0.redirect(String  
  503.      * uri,String target)),修改浏览器地址栏里的url,而不是传统我们说的重定向  
  504.      *   
  505.      * @param pageUri  
  506.      */   
  507.     public   static   final   void  sendRedirect(String pageUri) {  
  508.         Executions.sendRedirect(pageUri);  
  509.     }  
  510.   
  511.     /**  
  512.      * 向当前execution提交一个事件  
  513.      * <p>  
  514.      * 将事件提交到事件队列末尾,然后立即返回。 队列中排在前面的事件处理完毕后执行该动作提交的事件。  
  515.      *   
  516.      * @param event  
  517.      */   
  518.     public   static   final   void  postEvent(Event event) {  
  519.         Events.postEvent(event);  
  520.     }  
  521.   
  522.     /**  
  523.      * 向当前execution提交一个事件,可以设置事件的优先级  
  524.      * <p>  
  525.      * 将事件提交到事件队列末尾,然后立即返回。 队列中排在前面的事件处理完毕后执行该动作提交的事件。  
  526.      *   
  527.      * @priority  
  528.      * @param event  
  529.      */   
  530.     public   static   final   void  postEvent( int  priority, Event event) {  
  531.         Events.postEvent(priority, event);  
  532.   
  533.     }  
  534.   
  535.     /**  
  536.      * 向目标组件发送指定名称的事件  
  537.      * <p>  
  538.      * 将事件提交到事件队列末尾,然后立即返回。 队列中排在前面的事件处理完毕后执行该动作提交的事件。  
  539.      */   
  540.     public   static   final   void  postEvent(String name, Component target,  
  541.             Object data) {  
  542.         Events.postEvent(name, target, data);  
  543.     }  
  544.   
  545.     /**  
  546.      * 向当前execution发送一个事件  
  547.      * <p>  
  548.      * 事件处理线程和调用该方法的线程为同一线程,即二者为相同线程,所以必须等待事件处理完毕,该方法才会返回。  
  549.      * <p>  
  550.      * 如果目标事件的的处理器,是一个长操作,那么当前线程将长事件阻塞,而在客户端表现为:左上角一直出现"正在处理,请稍候..."等字样的提示,  
  551.      * 所以在使用前注意  
  552.      *   
  553.      * @param event  
  554.      */   
  555.     public   static   final   void  sendEvent(Event event) {  
  556.         Events.sendEvent(event);  
  557.     }  
  558.   
  559.     /**  
  560.      * 向指定组件发送事件  
  561.      * <p>  
  562.      * 事件处理线程和调用该方法的线程为同一线程,即二者为相同线程,所以必须等待事件处理完毕,该方法才会返回  
  563.      * <p>  
  564.      * 如果目标事件的的处理器,是一个长操作,那么当前线程将长事件阻塞,而在客户端表现为:左上角一直出现"正在处理,请稍候..."等字样的提示,  
  565.      * 所以在使用前注意  
  566.      *   
  567.      * @param comp  
  568.      *            目标组件  
  569.      * @param event  
  570.      */   
  571.     public   static   final   void  sendEvent(Component comp, Event event) {  
  572.         Events.sendEvent(comp, event);  
  573.     }  
  574.   
  575.     /**  
  576.      * 向目标组件发送指定名称的事件  
  577.      *   
  578.      * <p>  
  579.      * 事件处理线程和调用该方法的线程为同一线程,即二者为相同线程,所以必须等待事件处理完毕,该方法才会返回  
  580.      * <p>  
  581.      * 如果目标事件的的处理器,是一个长操作,那么当前线程将长事件阻塞,而在客户端表现为:左上角一直出现"正在处理,请稍候..."等字样的提示,  
  582.      * 所以在使用前注意  
  583.      *   
  584.      * @param name  
  585.      *            事件名称  
  586.      * @param target  
  587.      *            目标组件  
  588.      * @param data  
  589.      *            事件携带的数据,可以调用在事件监听器中使用<code>Event.getData()</code>获得该数据  
  590.      */   
  591.     public   static   final   void  sendEvent(String name, Component target,  
  592.             Object data) {  
  593.         Events.sendEvent(name, target, data);  
  594.     }  
  595.   
  596.     /**  
  597.      * 获取ForwardEvent事件的原始事件  
  598.      *   
  599.      * @param event  
  600.      * @return  
  601.      */   
  602.     public   static   final  Event getRealOrigin(ForwardEvent event) {  
  603.         return  Events.getRealOrigin(event);  
  604.     }  
  605.   
  606.     /**  
  607.      * 给指定的组件添加controller对象中定义的onXxx事件处理器,该controller是一个  
  608.      * 包含onXxx方法的POJO对象,该工具方法将onXxx方法注册给指定组件,因此你不用通过{@link EventListener}  
  609.      * 一个一个的向组件注册了  
  610.      * <p>  
  611.      * 所有在controller对象中以"on"开头的公共方法被作为事件处理器,并且相关事件同时也被监听,例如,  
  612.      * 如果controller对象有一个名字为onOk的方法,那么 onOk事件将被监听,然后当接收到onOk事件的时候, onOk方法被调用  
  613.      *   
  614.      * @param comp  
  615.      *            the component to be registered the events  
  616.      * @param controller  
  617.      *            a POJO file with onXxx methods(event handlers)  
  618.      * @since 3.0.6  
  619.      * @see GenericEventListener  
  620.      */   
  621.     public   static   final   void  addEventListeners(Component comp, Object controller) {  
  622.         Events.addEventListeners(comp, controller);  
  623.     }  
  624.   
  625.     /**  
  626.      * 检测名称是否是一个合法的zk事件名  
  627.      *   
  628.      * @param name  
  629.      * @return  
  630.      */   
  631.     public   static   final   boolean  isValidEventName(String name) {  
  632.         return  Events.isValid(name);  
  633.     }  
  634.   
  635.     /**  
  636.      * 判断一个指定事件的组件是否有事件处理器或监听器  
  637.      *   
  638.      * @param comp  
  639.      * @param evtnm  
  640.      * @param asap  
  641.      *            是否仅检测非延迟事件监听器,例如实现org.zkoss.zk.ui.event.Deferrable或  
  642.      *            org.zkoss.zk.ui.event.Deferrable.isDeferrable 返回 false的监听器  
  643.      * @return  
  644.      */   
  645.     public   static   final   boolean  isListened(Component comp, String evtnm,  
  646.             boolean  asap) {  
  647.         return  Events.isListened(comp, evtnm, asap);  
  648.     }  
  649.   
  650.     /**  
  651.      * 从uri指定的文件创建组件  
  652.      * <p>  
  653.      * 如果uri的页面内包含&lt;?page id=&quot;pageId&quot;  
  654.      * title=&quot;这个是标题&quot;?&gt;页面指令  
  655.      * ,在createComponents中该语句指令失效,createComponents方法不会因此创建Page对象, 当然  
  656.      * <code>desktop.getPage("pageId")</code的时候返回的是null  
  657.      *   
  658.      * @param uri  
  659.      * @param parent  
  660.      *            创建的组件所属的父组件F  
  661.      * @param args  
  662.      *            创建组件传递的参数  
  663.      * @return 创建的组件,该组件对象为uri页面的第一个组件(zk节点除外)  
  664.      */   
  665.     public   static   final  Component createComponents(String uri,  
  666.             Component parent, @SuppressWarnings ( "rawtypes" ) Map args) {  
  667.         return  Executions.createComponents(uri, parent, args);  
  668.     }  
  669.   
  670.     /**  
  671.      * 从zul格式字符串创建组件,创建的组件将作为第二个参数的子组件,如果第二个参数为null,  
  672.      * 那么创建的组件将作为当前Execution关联的page的子组件;  
  673.      * <p>  
  674.      * 如果content内包含&lt;?page id=&quot;pageId&quot;  
  675.      * title=&quot;这个是标题&quot;?&gt;页面指令  
  676.      * ,在createComponents中该语句指令失效,createComponents方法不会因此创建Page对象, 当然  
  677.      * <code>desktop.getPage("pageId")</code的时候返回的是null  
  678.      *   
  679.      * @param content  
  680.      *            zul格式内容的字符串  
  681.      * @param parent  
  682.      *            父组件,如果为null,那么组件所属的页面为当前页面,当前页面由execution上下文决定。  
  683.      *            另外新的组件将作为当前页面的根组件  
  684.      * @param args  
  685.      *            一个map类型的参数, 传递的参数可以使用{@link Executions#getArgs()}获得  
  686.      * @return 根据content创建的组件第一个组件  
  687.      */   
  688.     public   static   final  Component createComponentsDirectly(String content,  
  689.             Component parent, @SuppressWarnings ( "rawtypes" ) Map args) {  
  690.         return  Executions  
  691.                 .createComponentsDirectly(content, "zul" , parent, args);  
  692.     }  
  693.   
  694.     /**  
  695.      * 重绘组件  
  696.      * <p>  
  697.      * 仅允许在<b>请求处理阶段</b>和<b>事件处理阶段</b>调用, 不允许在<b>呈现阶段</b>调用  
  698.      *   
  699.      * @param comp  
  700.      */   
  701.     public   static   final   void  redraw(Component comp) {  
  702.         comp.invalidate();  
  703.     }  
  704.   
  705.     /**  
  706.      * 重绘页面  
  707.      * <p>  
  708.      * 仅允许在<b>请求处理阶段</b>和<b>事件处理阶段</b>调用, 不允许在<b>呈现阶段</b>调用  
  709.      *   
  710.      * @param page  
  711.      */   
  712.     public   static   final   void  redrawPage(Page page) {  
  713.         page.invalidate();  
  714.     }  
  715.   
  716.     /**  
  717.      * 弹出消息提示框  
  718.      *   
  719.      * @param message  
  720.      *            提示消息  
  721.      * @param title  
  722.      *            提示框标题  
  723.      */   
  724.     public   static   final   void  showInformation(String message, String title) {  
  725.         try  {  
  726.             Messagebox.show(message, title, Messagebox.OK,  
  727.                     Messagebox.INFORMATION);  
  728.         } catch  (InterruptedException e) {  
  729.             // ignore   
  730.         }  
  731.     }  
  732.   
  733.     /**  
  734.      * 弹出警告提示框  
  735.      *   
  736.      * @param message  
  737.      *            提示消息  
  738.      * @param title  
  739.      *            提示框标题  
  740.      */   
  741.     public   static   final   void  showExclamation(String message, String title) {  
  742.         try  {  
  743.             Messagebox.show(message, title, Messagebox.OK,  
  744.                     Messagebox.EXCLAMATION);  
  745.         } catch  (InterruptedException e) {  
  746.             // ignore   
  747.         }  
  748.     }  
  749.   
  750.     /**  
  751.      * 弹出消息提示框  
  752.      *   
  753.      * @param message  
  754.      *            提示消息  
  755.      * @param title  
  756.      *            提示框标题  
  757.      */   
  758.     public   static   final   void  showError(String message, String title) {  
  759.         try  {  
  760.             Messagebox.show(message, title, Messagebox.OK, Messagebox.ERROR);  
  761.         } catch  (InterruptedException e) {  
  762.             // ignore   
  763.         }  
  764.     }  
  765.   
  766.     /**  
  767.      * 询问提示框  
  768.      * <p>  
  769.      * 如果禁用事件处理线程,该方法会立即返回,返回值永远为true。 如果作为if判断语句的条件,  
  770.      * 那么else部分永远不会执行,启用和开启事件处理请查看zk.xml配置: <br />  
  771.      * &lt;system-config&gt;<br />  
  772.      * &lt;disable-event-thread&gt;false&lt;/disable-event-thread&gt;<br />  
  773.      * &lt;/system-config&gt;  
  774.      *   
  775.      * @param message  
  776.      *            提示消息 提示框标题  
  777.      * @return 禁用事件处理线程该方法永远返回true,启用事件处理相称时,如果用户点击ok按钮,返回true,反之false  
  778.      */   
  779.     public   static   final   boolean  showQuestion(String message, String title) {  
  780.         try  {  
  781.             return  Messagebox.OK == Messagebox.show(message, title,  
  782.                     Messagebox.OK | Messagebox.CANCEL, Messagebox.QUESTION);  
  783.         } catch  (InterruptedException e) {  
  784.             // ignore   
  785.             return   false ;  
  786.         }  
  787.     }  
  788.   
  789.     /**  
  790.      * 询问提示框  
  791.      * <p>  
  792.      * 该方法是一个类似 {@link #showQuestion(String, String)}  
  793.      * 的方法,但与其不同的是,当禁用事件处理线程时,该方法非常有用。  
  794.      * <p>  
  795.      *   
  796.      * <p>  
  797.      * 示例:<br />  
  798.      * <hr>  
  799.      *   
  800.      * <pre>  
  801.      * ZkUtils.showQuestion(&quot;您确定删除该记录吗?&quot;, &quot;询问&quot;, new EventListener() {  
  802.      *  &#064;Override  
  803.      *  public void onEvent(Event event) throws Exception {  
  804.      *      int clickedButton = (Integer) event.getData();  
  805.      *      if (clickedButton == Messagebox.OK) {  
  806.      *          // 用户点击的是确定按钮  
  807.      *      } else {  
  808.      *          // 用户点击的是取消按钮  
  809.      *      }  
  810.      *  }  
  811.      *   
  812.      * });  
  813.      * </pre>  
  814.      *   
  815.      * <hr>  
  816.      * <p>  
  817.      *   
  818.      * <table border="1">  
  819.      * <tr>  
  820.      * <td>按钮名称</td>  
  821.      * <td>事件名称</td>  
  822.      * </tr>  
  823.      * <tr>  
  824.      * <td>确定</td>  
  825.      * <td>onOK</td>  
  826.      * </tr>  
  827.      * <tr>  
  828.      * <td>取消</td>  
  829.      * <td>onCancel</td>  
  830.      * </tr>  
  831.      * </table>  
  832.      *   
  833.      * @param message  
  834.      * @param title  
  835.      * @param eventListener  
  836.      */   
  837.     public   static   final   void  showQuestion(String message, String title,  
  838.             EventListener eventListener) {  
  839.         try  {  
  840.             Messagebox.show(message, title, Messagebox.OK | Messagebox.CANCEL,  
  841.                     Messagebox.QUESTION, eventListener);  
  842.         } catch  (InterruptedException e) {  
  843.             // ignore   
  844.         }  
  845.   
  846.     }  
  847.   
  848.     /**  
  849.      * 给指定组件添加错误提示  
  850.      * <p>  
  851.      * 清除错误,需要使用{@link #clearWrongValue(Component)}  
  852.      *   
  853.      * @param comp  
  854.      * @param message  
  855.      *            错误提示消息  
  856.      * @see #clearWrongValue(Component)  
  857.      */   
  858.     public   static   final   void  addWrongValue(Component comp, String message) {  
  859.         Clients.wrongValue(comp, message);  
  860.     }  
  861.   
  862.     /**  
  863.      * 清除指定组件的错误提示  
  864.      * <p>  
  865.      * 例如在输入组件中指定constraint属性验证用户输入,输入错误时,弹出提示, 该 方法可以清除这个提示框  
  866.      *   
  867.      * @param comp  
  868.      */   
  869.     public   static   final   void  clearWrongValue(Component comp) {  
  870.         Clients.clearWrongValue(comp);  
  871.     }  
  872.   
  873.     /**  
  874.      * 清除列表中组件的错误提示  
  875.      *   
  876.      * @see #clearWrongValue(Component)  
  877.      */   
  878.     public   static   final   void  clearWrongValue(  
  879.             @SuppressWarnings ( "rawtypes" ) List comps) {  
  880.         Clients.clearWrongValue(comps);  
  881.   
  882.     }  
  883.   
  884.     /**  
  885.      * 设置或删除widget的事件监听器,如果已经有同样的事件监听,那么上一个将被替换 *  
  886.      *   
  887.      * <pre>  
  888.      * ZkUtils.addWidgetEventListener(txtAge, &quot;onKeyPress&quot;, &quot;&quot;  
  889.      *      + &quot;    if(event.keyCode&lt;48||event.keyCode&gt;57){         &quot;  
  890.      *      + &quot;       return false;                                 &quot; + &quot;     }   &quot;  
  891.      *      + &quot;   &quot;);  
  892.      * </pre>  
  893.      *   
  894.      * *  
  895.      * <p>  
  896.      * 与comp.addEventListener()和<component  
  897.      * onClick=""/>中的事件处理(EventHandler)不同,该事件处理运行于客户端  
  898.      *   
  899.      * @param comp  
  900.      * @param evtName  
  901.      *            事件名称,例如onClick  
  902.      * @param script  
  903.      *            javascript脚本代码,书写格式可按照html事件中js代码格式,如果为空,那么事件处理程序被删除  
  904.      */   
  905.     public   static   final   void  setWidgetEventListener(Component comp,  
  906.             String evtName, String script) {  
  907.         comp.setWidgetListener(evtName, script);  
  908.     }  
  909.   
  910.     /**  
  911.      *   
  912.      * 向指定组件事件追加事件监听器  
  913.      *   
  914.      * <pre>  
  915.      *   
  916.      * ZkUtils.addWidgetEventListener(txtAge, &quot;onKeyPress&quot;, &quot;&quot;  
  917.      *      + &quot;    if(event.keyCode&lt;48||event.keyCode&gt;57){         &quot;  
  918.      *      + &quot;       return false;                                 &quot; + &quot;     }   &quot;  
  919.      *      + &quot;   &quot;);  
  920.      * </pre>  
  921.      * <p>  
  922.      * 与comp.addEventListener()和<component  
  923.      * onClick=""/>中的事件处理(EventHandler)不同,该事件处理运行于客户端  
  924.      *   
  925.      * @param comp  
  926.      * @param evtnm  
  927.      *            事件名称,例如onClick  
  928.      * @param script  
  929.      *            javascript脚本代码,书写格式可按照html事件中js代码格式  
  930.      */   
  931.     public   static   final   void  addWidgetEventListener(Component comp,  
  932.             String evtnm, String script) {  
  933.         if  (script ==  null  ||  "" .equals(script.trim())) {  
  934.             return ;  
  935.         }  
  936.         String oldScript = comp.getWidgetListener(evtnm);  
  937.         if  (oldScript ==  null ) {  
  938.             oldScript = "" ;  
  939.         }  
  940.         comp.setWidgetListener(evtnm, oldScript + script);  
  941.   
  942.     }  
  943.   
  944.     /**  
  945.      * 验证表单  
  946.      * <p>  
  947.      * 需要input元素的constraint属性的支持  
  948.      * <p>  
  949.      *   
  950.      * 例如 年龄&lt;textbox constraint=&quot;/^[0-9]*$/:仅允许输入数字&quot;/&gt;  
  951.      *   
  952.      * @param formContainer  
  953.      *            Input元素公共组件,即需要验证的输入元素所在的公共容器组件,这个form在zk里是虚拟的,  
  954.      *            任何容器组件都可以是一个form容器  
  955.      * @return 如果验证成功返回true,否则返回false  
  956.      */   
  957.     public   static   boolean  validateForm(Component formContainer) {  
  958.         return  validateForm(formContainer,  true );  
  959.     }  
  960.   
  961.     /**  
  962.      * 验证表单  
  963.      * <p>  
  964.      * 需要input元素的constraint属性的支持  
  965.      *   
  966.      * 例如 年龄 &lt;textbox constraint=&quot;/^[0-9]*$/:仅允许输入数字&quot;/&gt;  
  967.      *   
  968.      * @param formContainer  
  969.      *            Input元素公共组件,即需要验证的输入元素所在的公共容器组件,这个form在zk里是虚拟的,  
  970.      *            任何容器组件都可以是一个form容器  
  971.      * @param showError  
  972.      *            是否显示错误提示  
  973.      * @return 如果验证成功返回true,否则返回false  
  974.      */   
  975.     public   static   boolean  validateForm(Component formContainer,  
  976.             boolean  showError) {  
  977.         try  {  
  978.             validateForm0(formContainer, showError);  
  979.             return   true ;  
  980.         } catch  (Exception e) {  
  981.             return   false ;  
  982.         }  
  983.     }  
  984.   
  985.     private   static   void  validateForm0(Component formContainer,  boolean  showError) {  
  986.         @SuppressWarnings ( "unchecked" )  
  987.         List<Component> cList = formContainer.getChildren();  
  988.         if  (cList ==  null  || cList.size() <  1 ) {  
  989.             return ;  
  990.         } else  {  
  991.             for  (Component c : cList) {  
  992.                 if  (c  instanceof  InputElement && !((InputElement) c).isValid()) {  
  993.                     if  (showError) {  
  994.                         ((InputElement) c).getText();  
  995.                     }  
  996.                     throw   new  RuntimeException( "表单输入不正确!" );  
  997.                 } else  {  
  998.                     validateForm0(c, showError);  
  999.                 }  
  1000.             }  
  1001.         }  
  1002.     }  
  1003.   
  1004.     /**  
  1005.      * 结束长操作处理  
  1006.      * <p>  
  1007.      * 一个业务操作可能要一段时间可以处理完成,在处理期间,又不想让用户操作界面,影响业务处理等,  
  1008.      * 那么可以在前台事件中调用zk.startProcessing(),此时左上角出现提示框,"正在处理,请稍候...", 那么待业务处理过后再try  
  1009.      * catch finally{}块里调用该方法,通知客户端操作完毕  
  1010.      */   
  1011.     public   static   final   void  endProcessing() {  
  1012.         Clients.evalJavaScript("zk.endProcessing();" );  
  1013.     }  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值