java GWT:将原始 HTML 放入标签
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7692306/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
GWT: Putting raw HTML inside a Label
提问by Michael
Is there a way to put raw HTML inside of a Label
widget with GWT? The constructor and setText()
methods automatically escape the text for HTML (so <
appears as <
, etc).
有没有办法将原始 HTML 放入Label
带有 GWT的小部件中?构造函数和setText()
方法会自动转义 HTML 的文本(因此<
显示为<
等)。
What I need is something like:
我需要的是这样的:
String matched = "two";
List<String> values = Arrays.asList("one", "two", "three");
StringBuilder sb = new StringBuilder();
for (String v : values){
if (v.equals(matched)){
sb.append("<b>" + v + "<b>");
} else {
sb.append(v);
}
sb.append(", ");
}
Label label = new Label();
label.setRawText(sb.toString());
//div contains the following HTML: "one, <b>two</b>, three, "
I want to output a comma-separated list of Strings, but I want one of those Strings to be bolded. Thanks.
我想输出一个逗号分隔的字符串列表,但我希望这些字符串之一被加粗。谢谢。
采纳答案by Michael
Sorry, I'm going to answer my own question because I found what I was looking for.
抱歉,我要回答我自己的问题,因为我找到了我要找的东西。
The SafeHtmlBuilder
class is perfect for this. You tell it what strings you want to escape and what strings you do not want to escape. It works like StringBuilder
because you call append
methods:
这SafeHtmlBuilder
门课非常适合这个。你告诉它你想转义哪些字符串以及你不想转义哪些字符串。它的工作原理就像StringBuilder
你调用append
方法一样:
String matched = "two";
List<String> values = Arrays.asList("one", "two", "three <escape-me>");
SafeHtmlBuilder builder = new SafeHtmlBuilder();
for (String v : values){
if (v.equals(matched)){
builder.appendHtmlConstant("<b>");
builder.appendEscaped(v);
builder.appendHtmlConstant("</b>");
} else {
builder.appendEscaped(v);
}
builder.appendEscaped(", ");
}
HTML widget = new HTML();
widget.setHTML(builder.toSafeHtml());
//div contains the following HTML: "one, <b>two</b>, three <escape-me>, "
Note that the appendHtmlConstant
method expects a completetag. So if you want to add attributes to the tag whose values change during runtime, it won't work. For example, this won'twork (it throws an IllegalArgumentException
):
请注意,该appendHtmlConstant
方法需要一个完整的标签。因此,如果您想向其值在运行时发生变化的标记添加属性,它将不起作用。例如,这将不起作用(它会抛出IllegalArgumentException
):
String url = //...
SafeHtmlBuilder builder = new SafeHtmlBuilder();
builder.appendHtmlConstant("<a href=\""); //throws IllegalArgumentException
builder.appendEscaped(url);
builder.appendHtmlConstant("\">link</a>");
回答by MTilsted
Use the HTML class (Or more likely: The InlineHTML class instead of the Label class. InlineHTML works like label, except that you can give it html.
使用 HTML 类(或者更有可能:InlineHTML 类而不是 Label 类。InlineHTML 的工作方式类似于 label,只是您可以给它 html。
And just a security warning: if part of the input to your InlineHTML object is input by the user, remember to strip html out of that part, so users can't insert their own scripts into your code.
并且只是一个安全警告:如果您的 InlineHTML 对象的部分输入是由用户输入的,请记住从该部分中去除 html,这样用户就无法将他们自己的脚本插入到您的代码中。
回答by Zoltan Balazs
Either create a label and set it to bold:
创建一个标签并将其设置为粗体:
Label label = new Label("text");
label.getElement().getStyle().setFontWeight(FontWeight.BOLD);
Or you can create a SpanElement
and set its innerHtml.
或者您可以创建一个SpanElement
并设置其innerHtml。
回答by user2661223
Here's example to put a space in a widget, e.g. Label:
这是在小部件中放置空格的示例,例如标签:
public void setTextNbsp( final Widget w ) {
w.getElement().setInnerHTML( " " );
}
Other HTML entities could be used as well. Take care with this not to open security holes. SafeHtml, etc. might need consideration if doing something more dynamic.
也可以使用其他 HTML 实体。小心不要打开安全漏洞。如果做一些更动态的事情,SafeHtml 等可能需要考虑。
回答by Shamaila Tahir
I think you should use SpanElement where you don't want the html to be escaped and label where you want then to be escaped and put them on a vertical panel so that they would appear as a single line of text.
我认为您应该在不希望转义 html 的地方使用 SpanElement 并在您希望转义的地方使用标签并将它们放在垂直面板上,以便它们显示为单行文本。