2011-12-30 180 views
1

幾乎所有的標題。文件不清楚這個電話的後果。JspFragment.invoke究竟做了什麼?

我的具體情況是,這個方法是用空傳入參數調用的,前後有代碼。我沒有得到這個調用的目的...
請注意,上下文是一個擴展SimpleTagSupport類(我正在做taglib的東西)。

感謝任何指針。這裏的Javadoc:

調用

public abstract void invoke(java.io.Writer out) 
        throws JspException, 
          java.io.IOException 

    Executes the fragment and directs all output to the given Writer, or the JspWriter returned by the getOut() method of the JspContext associated with the fragment if out is null. 

    Parameters: 
     out - The Writer to output the fragment to, or null if output should be sent to JspContext.getOut(). 
    Throws: 
     JspException - Thrown if an error occured while invoking this fragment. 
     SkipPageException - Thrown if the page that (either directly or indirectly) invoked the tag handler that invoked this fragment is to cease evaluation. The container must throw this exception if a Classic Tag Handler returned Tag.SKIP_PAGE or if a Simple Tag Handler threw SkipPageException. 
     java.io.IOException - If there was an error writing to the stream. 

回答

1

那麼,它允許標籤接收JSP片段(標記的機構)當標籤要執行。例如,您可以設計一個if標籤,它接收一個條件參數,並且具有一個標籤主體。如果條件爲false,則標籤不會執行任何操作。如果這是真的,標籤調用片段。一個loop標籤可以調用它的片段(執行其正文中的JSP代碼)的次數與它的iterationCount參數所要求的次數相同。

由於該片段可能用於生成一些HTML代碼以包含在響應中,該方法的out參數是生成片段輸出的位置。例如,如果您想要後處理片段生成的內容,則可能需要傳遞另一個作者,而不是讓片段直接寫入JSP writer。

+0

這是否意味着如果我在一個根本沒有身體的標籤(它只能通過閱讀標籤屬性轉換成html),這個調用沒有效果? – Sephy 2011-12-30 16:29:35

+0

如果標籤不接受正文,如果我正確讀取javadoc,則該片段將爲空。如果有一個身體,但它不包含任何字符,那麼它將不起作用。如果它包含一個空白區域,則將向作者寫入一個空白區域。 – 2011-12-30 16:51:29

+0

好吧,即使我仍然不明白爲什麼我在代碼中有這個,它會更清晰一些。謝謝! – Sephy 2011-12-30 16:52:22