Generating commented-out content with Wicket

谁说我不能喝 提交于 2019-12-01 04:34:23
Eelco

How about this?

class CommentOutModifier extends AbstractBehavior {
    private static final long serialVersionUID = 1L;

    @Override
    public void beforeRender(Component component) {
        component.getResponse().write("<!--");
    }

    @Override
    public void onRendered(Component component) {
      component.getResponse().write("-->");
    }
}

add(new Label("tohide", "Hi, can you see me?").add(new CommentOutModifier()));

then, putting:

<span wicket:id="tohide"></span>

in your markup will yield:

<!--<span>Hi, can you see me?</span>-->
Label l = new Label("foo", "<!-- " + foo + " -->");
l.setEscapeModelStrings(false);

Its not pretty but it's quick and easy. I also believe there is a specific wicket setting (somewhere in application) which which you can turn on to prevent it from stripping comments, but I honestly can't remember where I saw it.

Edit: Added comment worker

Edit2: Implemented Eelco's behaviour for completeness. Its better than my approach anyway.

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 &lt;!-- 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("<!--");
            }

            @Override
            public void onRendered(Component component) {
                component.getResponse().write("--!>");
            }
        };
    }
}

add(Comment.newComment("comment","Something worth commenting upon"));

Played around a little and got to this:

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

But it's not pretty..: <span wicket:id="commented">This will be replaced</span>
becomes: <!-- wicket:id="commented">Content</!-->

But at least it won't interfere with layout / css styles.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!