哪个是渲染任意 HTML 的最佳 Wicket 组件?

发布于 2024-10-12 16:16:09 字数 365 浏览 3 评论 0原文

我正在使用 Apache Wicket 实现一个简单的 Markdown wiki。 wiki 通常会根据用户输入的内容呈现任意 HTML。

我对哪个 Wicket 组件最适合呈现此类任意 HTML 感到有点困惑。

我尝试了 Label 组件,但它没有正确渲染列表,MultilineLabel 也没有(它放置中断而不是常规列表 HTML)。

感谢您的任何帮助。

更新:标签组件工作完美。我的错误是我没能让它更早地工作。这是一些糟糕的样式表和深夜编码的结合。感谢您提供有用的答案。正如建议的那样,我还将查看一些所见即所得编辑器,它们实际上可能比 Markdown 更好。 Visual Wicket 似乎特别有前途。

I am implementing a simple markdown wiki using Apache Wicket. The wiki would typically render any arbitrary HTML based on what the user has entered.

I am a bit confused about which Wicket component would be best suited to render such arbitrary HTML.

I tried the Label component but it does not render lists properly, neither does the MultilineLabel (which puts breaks instead of the regular list HTML).

Thanks for any help.

UPDATE: The Label component works perfectly. It was my mistake that I was not able to get it to work earlier. It was a combination of some bad stylesheets and late night coding. Thanks for the helpfull answers. As suggested, I am also going to check out some WYSIWYG editors, which actually might work out better than markdown. Visural Wicket seems especially promising.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

佞臣 2024-10-19 16:16:09

如果您要渲染的内容不大,或者已经表示为字符串,则 Label 可以很好地工作,只需调用 label.setEscapeModelStrings(false); 以确保它按原样打印字符串。

但是,如果您的 HTML 内容是动态生成的,或者从 InputStream/Reader 读取的,并且您不想将其保留在内存中,则可以直接使用 WebComponent,并重写方法 onComponentTagBody() 。这样,您可以直接写入响应,而不是填充内存缓冲区,而是将其转换为字符串,然后写入响应(如果您使用 Label,则会发生这种情况)。

两种情况的示例代码:

HomePage.java

public class HomePage extends WebPage {

    public HomePage() {

        add(new Label("label", "<ul><li>test</li><li>test</li><li>test</li><li>test</li><li>test</li></ul>")
            .setEscapeModelStrings(false));

        add(new WebComponent("html") {
            @Override
            protected void onComponentTagBody(MarkupStream markupStream, ComponentTag openTag) {
                Response response = getRequestCycle().getResponse();
                response.write("<ul>");
                for (int i = 0; i < 5; i++)
                    response.write("<li>test</li>");
                response.write("</ul>");
            }
        });
    }
}

HomePage.html

<html xmlns:wicket="http://wicket.apache.org">
<body>
  <h2>Label</h2>
  <div wicket:id="label"></div>
  <h2>WebComponent</h2>
  <div wicket:id="html"></div>
</body>
</html>

If what you want to render is not big, or is already represented as a String, Label will work well, just call label.setEscapeModelStrings(false); to ensure it prints the string as is.

But, if your HTML content is generated dynamically, or read from an InputStream/Reader, and you don't want to keep it in memory, you could use WebComponent directly, and override the method onComponentTagBody(). This way, you write directly to the response, instead of filling a in-memory buffer, transform it to a String, and then write to the response (which happens if you use Label).

Sample code, for both cases:

HomePage.java

public class HomePage extends WebPage {

    public HomePage() {

        add(new Label("label", "<ul><li>test</li><li>test</li><li>test</li><li>test</li><li>test</li></ul>")
            .setEscapeModelStrings(false));

        add(new WebComponent("html") {
            @Override
            protected void onComponentTagBody(MarkupStream markupStream, ComponentTag openTag) {
                Response response = getRequestCycle().getResponse();
                response.write("<ul>");
                for (int i = 0; i < 5; i++)
                    response.write("<li>test</li>");
                response.write("</ul>");
            }
        });
    }
}

HomePage.html

<html xmlns:wicket="http://wicket.apache.org">
<body>
  <h2>Label</h2>
  <div wicket:id="label"></div>
  <h2>WebComponent</h2>
  <div wicket:id="html"></div>
</body>
</html>
白色秋天 2024-10-19 16:16:09

它是Label,调用Component.setEscapeModelStrings(false) 不过要渲染模型返回的原始 html。

It is Label, call Component.setEscapeModelStrings(false) though to render the raw html your model returns.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文