Wicket:从 IBehavior::onComponentTag 更改组件主体

发布于 2024-09-28 00:52:31 字数 706 浏览 6 评论 0原文

我正在实现 Wicket IBehavior 接口,并希望我的行为通过 onComponentTag 方法更改组件的主体(或以某种方式更新模型)。 有办法做到这一点吗?

@Override
public void onComponentTag(final Component component, final ComponentTag tag)
{
    String myValue = tag.getAttribute("myAttribute");

    // TODO: Based on the value of this attribute, update the body/model of the component


    super.onComponentTag(component, tag);
}

编辑:我想从 html 中获取一个属性,该属性指定元素允许的最大字符数,然后根据需要以编程方式截断元素的主体。

示例:

<span wicket:id="myLabel" maxChars="10">The body of my tag</span>

将替换为:

<span wicket:id="myLabel" maxChars="10">The bod...</span>

I am implementing the Wicket IBehavior interface and want my behavior change the body of a component (or somehow update the model) from the onComponentTag method.
Is there a way to do this?

@Override
public void onComponentTag(final Component component, final ComponentTag tag)
{
    String myValue = tag.getAttribute("myAttribute");

    // TODO: Based on the value of this attribute, update the body/model of the component


    super.onComponentTag(component, tag);
}

Edit: I would like to grab an attribute from the html which specifies the maximum number of allowed characters for the element and then programmatically truncate the body of the element if needed.

Example:

<span wicket:id="myLabel" maxChars="10">The body of my tag</span>

would be replaced with:

<span wicket:id="myLabel" maxChars="10">The bod...</span>

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

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

发布评论

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

评论(2

雪落纷纷 2024-10-05 00:52:31

您也许可以通过从组件获取模型、从模型获取对象并对对象进行所需的任何更改来完成此操作,但是 onComponentTag 并不是更改组件的最佳位置。模型。

此方法在渲染过程中调用,此时您的页面可能已部分渲染。已呈现的页面的任何部分都将使用模型的先前状态进行呈现。由于模型可以在组件之间共享,因此生成的页面可能会不一致。

如果您尝试更改渲染的主体,那就是另一个故事了,并且在这种方法中完成的工作是完全合理的。它通常涉及调用 ComponentTag 标记 参数上的方法。

您试图通过创建此行为来解决什么问题?或许我们可以想出更好的办法。

编辑:

对于修剪标签上显示的特定情况,您可能最好通过简单地子类化 Label 组件来获得更好的服务,大约如下:

public class TrimmedLabel extends Label {
    private int size;

    public TrimmedLabel(String id, int size) {
        super(id);
        this.size = size;
    }

    public TrimmedLabel(String id, String label, int size) {
        super(id, label);
        this.size = size;
    }

    public TrimmedLabel(String id, IModel model, int size) {
        super(id, model);
        this.size = size;
    }

    @Override
    protected void onComponentTagBody(MarkupStream markupStream, ComponentTag openTag) {
        String value = getModelObjectAsString();
        if (value.length() > size) {
            value = value.substring(0, size);
        }
        replaceComponentTagBody(markupStream, openTag, value);
    }
}

You might be able to do this by getting the model from the component, getting the object from the model and making whatever changes you need on the object, but onComponentTag is not the best place for work that changes the model.

This method is called during the rendering process, at a point when your page may have been partially rendered. Any part of the page that has already been rendered will have been rendered with the prior state of the model. As models can be shared among components, your resulting page might be inconsistent.

If you're trying to change the rendered body, that is another story, and perfectly reasonable work to do in this method. It generally involves calling methods on the ComponentTag tag parameter.

What is the problem you're trying to solve by creating this behavior? Perhaps we can think of a better way.

EDIT:

For the particular case of trimming the display on a label, you're likely better served by simply subclassing the Label component approximately as follows:

public class TrimmedLabel extends Label {
    private int size;

    public TrimmedLabel(String id, int size) {
        super(id);
        this.size = size;
    }

    public TrimmedLabel(String id, String label, int size) {
        super(id, label);
        this.size = size;
    }

    public TrimmedLabel(String id, IModel model, int size) {
        super(id, model);
        this.size = size;
    }

    @Override
    protected void onComponentTagBody(MarkupStream markupStream, ComponentTag openTag) {
        String value = getModelObjectAsString();
        if (value.length() > size) {
            value = value.substring(0, size);
        }
        replaceComponentTagBody(markupStream, openTag, value);
    }
}
无悔心 2024-10-05 00:52:31

源自快速入门 http://wicket.apache.org/start/quickstart.html我的建议如下:

    add(new Label("message", "If you see this message wicket is properly configured and running") {

        @Override
        protected void onComponentTagBody(MarkupStream markupStream, ComponentTag openTag) {
            String myAttrib = openTag.getAttribute("myAttrib");
            replaceComponentTagBody(markupStream, openTag, getDefaultModelObjectAsString().substring(0, Integer.valueOf(myAttrib)));
        }

    });

不过,不要忘记注意 NumberFormat 异常。

唐罗比的建议也都适用。如果不需要,请不要弄乱模型。

Derived from the Quickstart http://wicket.apache.org/start/quickstart.html my suggestion would look like this:

    add(new Label("message", "If you see this message wicket is properly configured and running") {

        @Override
        protected void onComponentTagBody(MarkupStream markupStream, ComponentTag openTag) {
            String myAttrib = openTag.getAttribute("myAttrib");
            replaceComponentTagBody(markupStream, openTag, getDefaultModelObjectAsString().substring(0, Integer.valueOf(myAttrib)));
        }

    });

Don't forget to watch for NumberFormat Exceptions though.

Also donroby's suggestions do all aply. Don't mess with the model if not needed.

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