Wicket:从 IBehavior::onComponentTag 更改组件主体
我正在实现 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您也许可以通过从组件获取模型、从模型获取对象并对对象进行所需的任何更改来完成此操作,但是
onComponentTag
并不是更改组件的最佳位置。模型。此方法在渲染过程中调用,此时您的页面可能已部分渲染。已呈现的页面的任何部分都将使用模型的先前状态进行呈现。由于模型可以在组件之间共享,因此生成的页面可能会不一致。
如果您尝试更改渲染的主体,那就是另一个故事了,并且在这种方法中完成的工作是完全合理的。它通常涉及调用
ComponentTag 标记
参数上的方法。您试图通过创建此行为来解决什么问题?或许我们可以想出更好的办法。
编辑:
对于修剪标签上显示的特定情况,您可能最好通过简单地子类化
Label
组件来获得更好的服务,大约如下: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:源自快速入门 http://wicket.apache.org/start/quickstart.html我的建议如下:
不过,不要忘记注意 NumberFormat 异常。
唐罗比的建议也都适用。如果不需要,请不要弄乱模型。
Derived from the Quickstart http://wicket.apache.org/start/quickstart.html my suggestion would look like this:
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.