示例#1
0
  /**
   * Generates the AU responses that are part of a page rendering. Notice that {@link
   * #outPageContent} will invoke this method automatically.
   *
   * @param directJS whether to generate JS directly. If true, it generates <code>"x,y"</code> where
   *     x and y are assumed to the responses. If false, it generates <code>
   *     &lt;script&gt;zkac(x,y);&lt;/script&gt;</pre></code>
   * @since 5.0.2
   */
  public static final String outResponseJavaScripts(Execution exec, boolean directJS) {
    final ExecutionCtrl execCtrl = (ExecutionCtrl) exec;
    final Collection<AuResponse> responses = execCtrl.getResponses();
    if (responses == null || responses.isEmpty()) return "";
    execCtrl.setResponses(null);

    final StringBuffer sb = new StringBuffer(256);
    if (!directJS) sb.append("<script class=\"z-runonce\" type=\"text/javascript\">\nzkac(");

    for (Iterator<AuResponse> it = responses.iterator(); it.hasNext(); ) {
      final AuResponse response = it.next();
      sb.append('\'').append(response.getCommand()).append("',");
      final List<?> encdata = response.getEncodedData();
      if (encdata != null)
        sb.append('\'')
            .append(
                Strings.escape(
                    org.zkoss.json.JSONArray.toJSONString(encdata), Strings.ESCAPE_JAVASCRIPT))
            .append('\'');
      else sb.append((String) null);
      if (it.hasNext()) sb.append(",\n");
    }

    if (!directJS) sb.append(");\n</script>");
    return sb.toString();
  }
示例#2
0
  /**
   * Sets the content type to the specified execution for the given page.
   *
   * @param exec the execution (never null)
   */
  public static final void setContentType(Execution exec, Page page) {
    String contentType = ((PageCtrl) page).getContentType();
    if (contentType == null) {
      contentType = page.getDesktop().getDevice().getContentType();
      if (contentType == null) contentType = "";
    }

    final int j = contentType.indexOf(';');
    if (j < 0) {
      final String cs = page.getDesktop().getWebApp().getConfiguration().getResponseCharset();
      if (cs != null && cs.length() > 0) contentType += ";charset=" + cs;
    }

    ((ExecutionCtrl) exec).setContentType(contentType);
  }