如何使用 j2html java 将包含 html 的文本渲染到 html lib
How do I render text containing html with j2html java to html lib
一直在使用 j2html 从 Java 创建 html,效果很好,但当我想要这样的东西时我不知道如何使用
<p>The fox ran over the <b>Bridge</b> in the forest</p>
如果我这样做
import static j2html.TagCreator.*;
public class HtmlTest
{
public static void main(String[] args)
{
System.out.println(p("The fox ran over the " + b(" the bridge") + "in the forest"));
}
}
我明白了
<p>The fox ran over the <b>the bridge</b> in the forest</p>
也就是说,它将粗体视为文本。
注意简单地做
import static j2html.TagCreator.*;
public class HtmlTest
{
public static void main(String[] args)
{
System.out.println(p(b("the bridge")));
}
}
是否正确渲染给予
<p><b>the bridge</b></p>
我没用过j2html,但是看了example,如果我没记错的话,我猜语法应该是:
p("The fox ran over the ", b(" the bridge"), "in the forest")
抱歉我的公司环境不允许我下载 Eclipse 等来测试..
.
更新:以上是错误的。但是我找到了一个方法——虽然它相当复杂:
p("The fox ran over the ").with((DomContent)b("the bridge")).withText(" in the forest")
输出:
<p>The fox ran over the <b>the bridge</b> in the forest</p>
(DomContent)
可以删除,但我保留以供澄清。我猜逻辑是,如果作为文本添加的任何内容都会被转义,那么使其工作的唯一方法是添加 DomContent
或 ContainerTag
。
更新 2:"Better" 方法找到了!
p(new Text("The fox ran over the "), b("the bridge"), new Text(" in the forest"))
或 "helper"
import static j2html.TagCreator.*;
import j2html.tags.Text;
public class Test {
private static Text $(String str) {
return new Text(str);
}
public static void main(String[] args) {
System.out.println(p($("The fox ran over the "), b("the bridge"), $(" in the forest")));
}
}
<p>The fox ran over the <b>Bridge</b> in the forest</p>
可以写成
p(join("The fox ran over the", b("Bridge"), "in the forest")
我发布这个答案是因为这是我搜索“j2html insert html”时出现的第一批结果之一;本质上,我希望将 HTML 文本插入到我使用 j2html 构建的文件中。事实证明 j2html.TagCreator#join
方法也可以在不转义的情况下连接文本,因此如下所示:
System.out.println(html(body(join("<p>This is a test</p>"))).render());
System.out.println(html(body(join("<p>This is a test</p><p>Another Test</p>"))).renderFormatted());
System.out.println(html(body(p("This is a test"), p("Another Test"))).renderFormatted());
输出如下:
<html><body><p>This is a test</p></body></html>
<html>
<body>
<p>This is a test</p><p>Another Test</p>
</body>
</html>
<html>
<body>
<p>
This is a test
</p>
<p>
Another Test
</p>
</body>
</html>
请注意,renderFormat
方法不会渲染连接的 HTML,这既不意外也不重要;只是值得注意。希望这有助于执行与我相同的搜索的人。
一直在使用 j2html 从 Java 创建 html,效果很好,但当我想要这样的东西时我不知道如何使用
<p>The fox ran over the <b>Bridge</b> in the forest</p>
如果我这样做
import static j2html.TagCreator.*;
public class HtmlTest
{
public static void main(String[] args)
{
System.out.println(p("The fox ran over the " + b(" the bridge") + "in the forest"));
}
}
我明白了
<p>The fox ran over the <b>the bridge</b> in the forest</p>
也就是说,它将粗体视为文本。
注意简单地做
import static j2html.TagCreator.*;
public class HtmlTest
{
public static void main(String[] args)
{
System.out.println(p(b("the bridge")));
}
}
是否正确渲染给予
<p><b>the bridge</b></p>
我没用过j2html,但是看了example,如果我没记错的话,我猜语法应该是:
p("The fox ran over the ", b(" the bridge"), "in the forest")
抱歉我的公司环境不允许我下载 Eclipse 等来测试..
.
更新:以上是错误的。但是我找到了一个方法——虽然它相当复杂:
p("The fox ran over the ").with((DomContent)b("the bridge")).withText(" in the forest")
输出:
<p>The fox ran over the <b>the bridge</b> in the forest</p>
(DomContent)
可以删除,但我保留以供澄清。我猜逻辑是,如果作为文本添加的任何内容都会被转义,那么使其工作的唯一方法是添加 DomContent
或 ContainerTag
。
更新 2:"Better" 方法找到了!
p(new Text("The fox ran over the "), b("the bridge"), new Text(" in the forest"))
或 "helper"
import static j2html.TagCreator.*;
import j2html.tags.Text;
public class Test {
private static Text $(String str) {
return new Text(str);
}
public static void main(String[] args) {
System.out.println(p($("The fox ran over the "), b("the bridge"), $(" in the forest")));
}
}
<p>The fox ran over the <b>Bridge</b> in the forest</p>
可以写成
p(join("The fox ran over the", b("Bridge"), "in the forest")
我发布这个答案是因为这是我搜索“j2html insert html”时出现的第一批结果之一;本质上,我希望将 HTML 文本插入到我使用 j2html 构建的文件中。事实证明 j2html.TagCreator#join
方法也可以在不转义的情况下连接文本,因此如下所示:
System.out.println(html(body(join("<p>This is a test</p>"))).render());
System.out.println(html(body(join("<p>This is a test</p><p>Another Test</p>"))).renderFormatted());
System.out.println(html(body(p("This is a test"), p("Another Test"))).renderFormatted());
输出如下:
<html><body><p>This is a test</p></body></html>
<html>
<body>
<p>This is a test</p><p>Another Test</p>
</body>
</html>
<html>
<body>
<p>
This is a test
</p>
<p>
Another Test
</p>
</body>
</html>
请注意,renderFormat
方法不会渲染连接的 HTML,这既不意外也不重要;只是值得注意。希望这有助于执行与我相同的搜索的人。