XPages CSJS被执行而SSJS不被执行

时间:2018-11-06 10:05:52

标签: javascript xpages ssjs rendered-attribute

我有一个用于将文件上传到页面的自定义控件。而且效果很好,但仅当它不在 xp.this.rendered 属性中时。如果是这样,它将给我带来非常意外的结果。这是我使用的div的代码:

        <div style="height:0px;overflow:hidden">
            <input type="file"
                id="${javascript:compositeData.ID+'_files_input'}"
                onchange="#{javascript:
      var currentCustomID = compositeData.ID; 

      var filesInput = '\'' + currentCustomID + '_files_input' + '\'';
      var filesUpload = '\'' + currentCustomID + '_files_upload' + '\'';
      var filesButton = '\'' +  currentCustomID + '_files_button' + '\'';
      var filesProgress = '\'' +  currentCustomID + '_files_progress' + '\'';

      return 'files_onchange(' + filesInput + ',' + filesUpload + ',' + filesButton + ',' + filesProgress + ')';
      }"
                multiple="true" uploadOnSelect="true" name="uploadedfile" />

            <xp:fileUpload
                id="${javascript:compositeData.ID+'_files_upload'}"
                useUploadname="true">
                <xp:this.value><![CDATA[#{doc_source[compositeData.FieldName]}]]></xp:this.value>
            </xp:fileUpload>

<xp:button value="Refresh"
        id="${javascript:compositeData.ID+'_files_button'}">
        <xp:eventHandler event="onclick" submit="true"
          refreshMode="partial"
          refreshId="${javascript:compositeData.ID+'refresh'}"
          disableValidators="true">
          <xp:this.action>


            <xp:actionGroup>
              <xp:actionGroup>

              <xp:executeScript
                script="#{javascript:print('But server side executes only without RENDERED')}">
              </xp:executeScript>

                <xp:saveDocument></xp:saveDocument>
                <xp:executeScript>
                  <xp:this.script><![CDATA[#{javascript:
if (compositeData.postUpload!=null)
{
compositeData.postUpload.getScript().invoke(facesContext, null)
}
//print("сука");
}]]></xp:this.script>
                </xp:executeScript>
              </xp:actionGroup>

            </xp:actionGroup>
          </xp:this.action>

          <xp:this.script>
            <xp:executeClientScript
              script="console.log('CSJS works so well')">
            </xp:executeClientScript>
          </xp:this.script>
          </xp:eventHandler>
      </xp:button>
        </div>

在上面的代码中,有一个刷新按钮,该按钮应该是saveDocument,以便文档保存刚收到的文件。

此外,还有一些代码发送带有表单数据的XHR请求:

function files_onchange(filesInput, filesUpload, filesButton, filesProgress) 
{
    var urfiles = document.getElementById(filesInput).files;
    files_upload(filesInput, filesUpload, urfiles, 0, filesButton, filesProgress);
}           

function files_upload(filesInput, uploadID, files, counter, refreshID, filesProgress)
{

    var url = window.location.href;

    var formData = new FormData();
    var file = null;
    var form = XSP.findForm(filesInput);
    if (!files) return;
    var numberOfFiles = files.length;
    file = files[counter];
    var max = files.length;
    if (counter >= max) return;

    formData.append(document.querySelector('[id$=' + uploadID + ']').id, file);
    formData.append("$$viewid", dojo.query("input[name='$$viewid']")[0].value);
    formData.append("$$xspsubmitid", dojo.query("input[name='$$xspsubmitid']")[0].value);
    formData.append("$$xspsubmitvalue", dojo.query("input[name='$$xspsubmitvalue']")[0].value);
    formData.append("$$xspsubmitscroll", dojo.query("input[name='$$xspsubmitscroll']")[0].value);
    formData.append(form.id, form.id);


    var xhr = new XMLHttpRequest();


    /* event listners */

      xhr.upload.addEventListener("progress", function(e) 
      {
          if (e.lengthComputable)

           {
                var percentComplete = Math.round(e.loaded * 100 / e.total);
                document.getElementById(filesProgress).innerHTML = percentComplete.toString()+'%, ( '+(counter+1).toString()+' / '+numberOfFiles.toString()+' )';
           }
              else 
              {
                document.getElementById(filesProgress).innerHTML = '...';
              } 
      }, false);


      xhr.addEventListener("load", function()
      {
          counter++; 
          if (counter >= max)
          {
              document.getElementById(filesInput).value = "";

              if (refreshID) 

              {
                  document.querySelector('[id$=' + refreshID + ']').click(); // it's supposed to trigger the refresh button
              }

          } 
        else 
              { 
                files_upload(filesInput, uploadID, files, counter, refreshID, filesProgress) 
              }

      }, false);

     xhr.addEventListener("error", function(e) 
     {
         document.getElementById(filesProgress).innerHTML = "Error: "+e;

     }, false);

     xhr.addEventListener("abort", function()
     {
         document.getElementById(filesProgress).innerHTML = "Upload cancelled.";

     }, false);

      xhr.open("POST", url, true);
      xhr.send(formData);
      document.querySelector('[id$=' + uploadID + ']').value = '';
}

我不知道为什么document.querySelector('[id$=' + refreshID + ']').click()中的CSJS代码总是被执行。不管xp.this.rendered属性如何,我总是在浏览器控制台中打印'CSJS works so well',而But server side executes only without RENDERED仅在服务器控制台中打印而不呈现属性(或者如果将其永久设置为true)。背后的原因是什么?预先感谢。

1 个答案:

答案 0 :(得分:1)

确保“显示错误”组件在刷新区域中,并检查验证错误。这是CSJS运行但SSJS没有运行的最常见原因。