如何使用J2HTMLJava将包含html的文本呈现到html库
我一直在使用Java创建html,工作得很好,但我不知道当我想要这样的东西时如何使用如何使用J2HTMLJava将包含html的文本呈现到html库,java,html,j2html,Java,Html,J2html,我一直在使用Java创建html,工作得很好,但我不知道当我想要这样的东西时如何使用 <p>The fox ran over the <b>Bridge</b> in the forest</p> 我明白了 给予是否恰当 <p><b>the bridge</b></p> 桥 我从未使用过j2html,但是如果我没有错的话,我想语法应该是: p("The fox ran over the ", b(
<p>The fox ran over the <b>Bridge</b> in the forest</p>
我明白了
给予是否恰当
<p><b>the bridge</b></p>
桥
我从未使用过j2html,但是如果我没有错的话,我想语法应该是:
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>
或与“助手”
我发布这个答案是因为这是我搜索“j2html插入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>
System.out.println(html(body(join)(这是一个测试)).render();
System.out.println(html(body(join(这是一个测试另一个测试))).renderformat();
println(html(body(p(“这是一个测试”)、p(“另一个测试”)).renderFormatted();
产出如下:
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提供的连接函数,找不到它吗?是,
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")
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>