2

假设您有以下内容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;
    // ...
}

HTML那么是否存在您想要/需要使用 SafeHtml 或 SafeHtmlBuilder 的用例,或者仅在使用对象及其底层 DOM 结构时才需要“Safe*”API ?

如果存在 UiBinder 支持的复合材料需要使用 Safe* 的用例,也许一个简单的代码示例可以帮助我连接这些点。提前致谢!

4

1 回答 1

2

一个简单的例子,你应该将 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 构造函数之一

于 2012-11-09T00:25:25.943 回答