使用Wicket生成注释掉的内容

出于调试原因,并且一时兴起,我想将某些信息包含在Wicket页面的HTML输出中,该输出包含在HTML注释中

输出将是……

<!-- 
1234
-->

…其中“1234”是一些有趣的,动态创建的信息。

我试过,但无济于事:

  • <!--

    --> →Wicket抱怨HTML页面中缺少id为“foo”的元素

  • 括在 →这些部分不能包含带wicket:id元素
  • label.setVisible(false) →Wicket根本不输出标签
  • new Label("foo", "")<>获取转义

那么,你可以用Wicket(轻松)做到这一点,还是我应该忘掉它?

这个怎么样?

 class CommentOutModifier extends AbstractBehavior { private static final long serialVersionUID = 1L; @Override public void beforeRender(Component component) { component.getResponse().write(""); } } add(new Label("tohide", "Hi, can you see me?").add(new CommentOutModifier())); 

然后,放:

  

在您的标记中将产生:

  
 Label l = new Label("foo", ""); l.setEscapeModelStrings(false); 

它不漂亮,但它快速而简单。 我也相信有一个特定的wicket设置(在应用程序的某个地方),你可以打开它以防止它剥离评论,但我真的不记得我在哪里看到它。

编辑:添加评论工作者

Edit2:实现了Eelco的完整性行为。 无论如何,它比我的方法更好。

 public enum Comment { ; /** * creates a wicket comment (extends label */ public static Label newComment(String id, String comment) { Label label = new Label(id, comment); label.add(commentBehaviour()); return label; } /** * Adds <!-- and --&gt around the component. */ public static AbstractBehavior commentBehaviour() { return new AbstractBehavior() { private static final long serialVersionUID = 1L; @Override public void beforeRender(Component component) { component.getResponse().write(""); } }; } } add(Comment.newComment("comment","Something worth commenting upon")); 

玩了一下,得到了这个:

 Label label = new Label("commented", "Content") { @Override protected void onComponentTag(ComponentTag tag) { tag.setName("!--"); super.onComponentTag(tag); } }; add(label); 

但它并不漂亮……: This will be replaced
成为:

但至少它不会干扰布局/ CSS样式。