コード例 #1
0
  /**
   * First render the body of the component. And if it is the header component of a Page (compared
   * to a Panel or Border), then get the header sections from all component in the hierarchy and
   * render them as well.
   */
  @Override
  public final void onComponentTagBody(MarkupStream markupStream, ComponentTag openTag) {
    // We are able to automatically add <head> to the page if it is
    // missing. But we only want to add it, if we have content to be
    // written to its body. Thus we first write the output into a
    // StringResponse and if not empty, we copy it to the original
    // web response.

    // Temporarily replace the web response with a String response
    final Response webResponse = getResponse();

    try {
      // Create a (string) response for all headers contributed by any component on the Page.
      final StringResponse response = new StringResponse();
      getRequestCycle().setResponse(response);

      IHeaderResponse headerResponse = getHeaderResponse();
      if (!response.equals(headerResponse.getResponse())) {
        getRequestCycle().setResponse(headerResponse.getResponse());
      }

      // Render the header sections of all components on the page
      AbstractHeaderRenderStrategy.get()
          .renderHeader(this, new HeaderStreamState(markupStream, openTag), getPage());

      // Close the header response before rendering the header container itself
      // See https://issues.apache.org/jira/browse/WICKET-3728
      headerResponse.close();

      // Cleanup extraneous CR and LF from the response
      CharSequence output = getCleanResponse(response);

      // Automatically add <head> if necessary
      if (output.length() > 0) {
        if (renderOpenAndCloseTags()) {
          webResponse.write("<head>");
        }

        webResponse.write(output);

        if (renderOpenAndCloseTags()) {
          webResponse.write("</head>");
        }
      }
    } finally {
      // Restore the original response
      getRequestCycle().setResponse(webResponse);
    }
  }