【问题标题】:JSF 2 custom composite component with f:ajax带有 f:ajax 的 JSF 2 自定义复合组件
【发布时间】:2012-12-06 20:06:54
【问题描述】:

我正在尝试使用这个 jQuery 插件 http://www.eyecon.ro/colorpicker/ 创建一个自定义复合组件 colorPicker。

我希望能够附加一个 jsf 标记 f:ajax,并在选择颜色时对服务器执行 ajax 调用。我一直在测试这个功能,一切似乎都是正确的,但显然我错过了一些东西,因为从未调用过监听器。

这是我的组件代码:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:c="http://java.sun.com/jsp/jstl/core"
      xmlns:composite="http://java.sun.com/jsf/composite">
<composite:interface>
<composite:attribute name="label" />
<composite:clientBehavior name="customEvent" event="change" targets="#{cc.clientId}"/>
</composite:interface>
<composite:implementation>
    <h:outputStylesheet library="css" name="colorpicker/colorpicker.css" />
      <h:outputStylesheet library="css" name="colorpicker/layout.css" />
      <h:outputScript library="js" name="colorpicker/jquery.js" target="head"/>
      <h:outputScript library="js" name="colorpicker/colorpicker.js" target="head"/>
      <h:outputScript library="js" name="colorpicker/eye.js" target="head"/>
      <h:outputScript library="js" name="colorpicker/utils.js" target="head"/>
      <h:outputScript library="js" name="colorpicker/layout.js" target="head"/>
      <h:outputScript library="js" name="colorpicker/hex.js" target="head"/>


    <div id="#{cc.clientId}" class="colorSelector">
        <div style="background-color: #0000FF;"></div>
    </div>

    <script>


    //jQuery(document).ready(function() {

      jQuery('##{cc.clientId}').ColorPicker({
        color: '#0000ff',
        onShow: function (colpkr) {
          jQuery(colpkr).fadeIn(2000);
          alert('onchange1');
        launchEvent(document.getElementById('#{cc.clientId}'));
        alert('onchange2');
          //return false;
        },
        onHide: function (colpkr) {


          jQuery(colpkr).fadeOut(2000);
          return false;
        },
        onChange: function (hsb, hex, rgb) {


        }
      });

    //});

    /* <![CDATA[ */
    function launchEvent(fieldName) {
      alert('launchEvent1');
        if ("fireEvent" in fieldName) {
          alert('launchEvent2');
          fieldName.fireEvent("onchange");
          alert('launchEvent3');
    } else {
      alert('launchEvent4');
    var evt = document.createEvent("HTMLEvents");
    alert('launchEvent5');
      evt.initEvent("change", false, true);
      alert('launchEvent6');
      fieldName.dispatchEvent(evt);
      alert('launchEvent7');
    }
        /* ]]> */
}

  </script>

</composite:implementation>
</html>

这是页面实现:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html  
xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:r="http://richfaces.org/rich"
      xmlns:a="http://richfaces.org/a4j"
      xmlns:s="http://jboss.org/seam/faces"
      xmlns:cp="http://java.sun.com/jsf/composite/component">

<h:head>
   <title>Test</title>  
</h:head>
<h:body>
  <f:view>
    <h:form prependId="false">
      <cp:colorpicker id="colorSelector">
        <f:ajax event="customEvent" listener="#{themeBean.changeColor1}" onevent="alert('event raised');"/>
      </cp:colorpicker>
      <h:inputText value="#{themeBean.color1}"></h:inputText>
    </h:form>
  </f:view>
</h:body>       

</html>

【问题讨论】:

    标签: ajax jsf-2 event-handling custom-component composite-component


    【解决方案1】:

    &lt;f:ajax&gt; 只能附加到 ClientBehaviorHolder,例如 &lt;h:inputText&gt;。纯 HTML &lt;div&gt; 不是这样一个组件。您基本上需要有一个 HTML &lt;input&gt; 元素,而不是 &lt;div&gt; 元素。更重要的是,您将如何在 bean 中设置提交的值?

    <cc:interface>
        ...
        <cc:clientBehavior name="customEvent" targets="input" event="valueChange" />
    </cc:interface>
    <cc:implementation>
        ...    
        <h:inputText id="input" value="#{cc.attrs.value}" />
    
        <h:outputScript>
            jQuery("[id='#{cc.clientId}:input']").ColorPicker({ 
                // ...
            });
        </h:outputScript>
    </cc:implementation>
    

    (请注意,我也修复了 jQuery 选择器;这样 JSF 客户端 ID 分隔符 : 将被正确考虑,而不是使 CSS 选择器畸形)


    与具体问题无关,您对&lt;f:ajax onevent&gt; 的使用是错误的。它应该指向一个函数引用,它不应该包含一些函数调用。正确的用法是

    <f:ajax ... onevent="functionName" />
    
    ...
    
    <h:outputScript>
        function functionName(data) {
            alert("Event " + data.status + " raised");
        }
    </h:outputScript>
    

    另见:

    【讨论】:

      猜你喜欢
      • 2014-02-21
      • 2011-07-28
      • 2012-01-29
      • 2011-11-08
      • 2013-05-06
      • 2013-06-07
      • 2012-07-07
      • 2012-10-02
      • 1970-01-01
      相关资源
      最近更新 更多