簡體   English   中英

使用 UiBinder 時是否需要 GWT SafeHtml?

[英]Is there a need for GWT SafeHtml when using UiBinder?

假設您有以下MyPanel.ui.xml

<!DOCTYPE ui:UiBinder SYSTEM "http://dl.google.com/gwt/DTD/xhtml.ent">
<ui:UiBinder xmlns:ui="urn:ui:com.google.gwt.uibinder"
    xmlns:gwt="urn:import:com.google.gwt.user.client.ui">
    <div>
        <span id="content">Some content</span>

        <gwt:RadioButton ...>
            ...
        </gwt:RadioButton>

        <!-- etc. -->
    </div>
</ui:UiBinder>

這“映射”到MyPanel.java

public class MyPanel extends Composite {
    private RadioButton radioButton;
    // ...
}

那么是否有任何您想要/需要使用 SafeHtml 或 SafeHtmlBuilder 的用例,或者僅在處理HTML對象及其底層 DOM 結構時才需要“Safe*”API?

如果有 UiBinder 支持的復合材料需要使用 Safe* 的用例,也許一個簡單的代碼示例可以幫助我連接點。 提前致謝!

您應該將 SafeHTML 與 UiBinder 結合使用的簡單示例:

<!DOCTYPE ui:UiBinder SYSTEM "http://dl.google.com/gwt/DTD/xhtml.ent">
<ui:UiBinder xmlns:ui="urn:ui:com.google.gwt.uibinder"
    xmlns:gwt="urn:import:com.google.gwt.user.client.ui">
    <gwt:HTMLPanel>
        <gwt:HTML ui:field="myHtml"/>
    </g:HTMLPanel>
</ui:UiBinder>
public class MyPanel extends Composite {
    private HTML myHtml;
    // ...
}

在這里你應該使用myHtml.setHTML(SafeHTML) [*] 原因是,這是示例中唯一可能出現用戶提供內容的地方。 用戶內容不能出現在 UiBinder 模板本身中(因為它是靜態的:在編譯時固定)。

因此,是否需要 SafeHTML 之間的區別相當於信任用戶提供的內容與信任開發人員提供的內容之間的區別。

[*] 在您自己的示例中,您應該使用 RadioButton 的 SafeHTML 構造函數之一

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM