假设您有以下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构造函数

关于java - 使用UiBinder时是否需要GWT SafeHtml?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13298881/

10-10 01:35