Wicket - 添加 body 标签属性
我被困住了 -
我需要一个 Wicket Panel 能够向 添加一个类属性其所在页面的标签。 示例用法:
Java:
add(new SpecialSidebarComponent("sidebar"));
生成的 HTML:
<body class="sidebar">
...
<div id="sidebar">My Wicket Panel</div>
...
</body>
我无法添加 wicket:id 并使主体成为 Wicket 组件,因为这使得将组件添加到我拥有的大页面层次结构中的页面变得非常困难,而且仍然没有轻松允许面板修改主体属性。
我认为 BodyTagAttributeModifier 可能是用于此目的,但显然它是用于其他用途并且无法使其发挥作用( Wicket:如何使用 BodyTagAttributeModifier 类? )
有什么有用的想法吗?
更新:
在查看它时,似乎 BodyTagAttributeModifier 类仅用于面板的父标记,而不是页面的 标签。标签:
示例(Scala 语法):
class Home extends WebPage {
add(new Sidebar("sidebar"))
}
class Sidebar(id: String) extends Panel(id) {
add(new BodyTagAttributeModifier("class", true, new Model("layout-class"), getParent))
}
模板:
<html>
<body>
<div wicket:id="sidebar">Sidebar</div>
</body>
</html>
渲染:
<html>
<body>
<div class="layout-class">
<h1>Hello World!</h1>
</div>
</body>
</html>
非常令人困惑的名称恕我直言。不能解决问题,但至少更有意义。
I'm stuck -
I need to have a Wicket Panel be able to add a class attribute to the <body> tag of whatever page it's on.
Example usage:
Java:
add(new SpecialSidebarComponent("sidebar"));
Generated HTML:
<body class="sidebar">
...
<div id="sidebar">My Wicket Panel</div>
...
</body>
I cannot add a wicket:id and make the body a Wicket component, because this makes it very difficult to add components to a page in the big page hierarchy I have, and it still also doesn't easily allow for a Panel to modify the body attribute.
I thought BodyTagAttributeModifier may be for this, but apparently it is for something else and cannot get it to function ( Wicket: how to use the BodyTagAttributeModifier class? )
Any helpful ideas?
Update:
In looking at it, it appears the BodyTagAttributeModifier class is only for a Panel's parent tag, not the Page's <body> tag:
Example (Scala syntax):
class Home extends WebPage {
add(new Sidebar("sidebar"))
}
class Sidebar(id: String) extends Panel(id) {
add(new BodyTagAttributeModifier("class", true, new Model("layout-class"), getParent))
}
Template:
<html>
<body>
<div wicket:id="sidebar">Sidebar</div>
</body>
</html>
Rendered:
<html>
<body>
<div class="layout-class">
<h1>Hello World!</h1>
</div>
</body>
</html>
Very confusing name IMHO. Doesn't solve the issue but at least makes more sense.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我个人认为 Javascript 选项对于这种特定情况来说是最干净的。但是,您对
add(Component...)
最终的评论让我相信您可能对setTransparentResolver(true)
方法感兴趣。它的工作原理如下...BasePage.html
BasePage.java
MyPage.java(扩展 BasePage)
即使您没有将 SidebarPanel 直接添加到 BasePage 中的 bodyContainer,它仍然可以工作,因为
setTransparentResolver(true)
。对于简单的情况,请使用 Javascript。对于因子类无法放入容器而感到受限的一般问题,请注意透明解决。
I personally think the Javascript option is the cleanest for this specific case. However, your comment about
add(Component...)
being final leads me to believe that you might be interested in thesetTransparentResolver(true)
method. Here's how it works...BasePage.html
BasePage.java
MyPage.java (extends BasePage)
Even though you are not adding the SidebarPanel directly to the bodyContainer in the BasePage, it will still work out because of
setTransparentResolver(true)
.For your simple case, go with the Javascript. For the general issue of feeling constrained by subclasses not being able to fit inside containers, be aware of transparent resolving.
如果您确实无法为
标记提供 wicket:id (我假设您没有每个或几乎每个其他页面都扩展的 BasePage 来抽象这个),在页面渲染时(渲染
标记时)不可能知道要附加到哪个类,它将简单地从 HTML 中复制到输出。
不过,您可以通过 javascript 实现相同的目的。让您的
Panel
实现IHeaderContributor
并使用IHeaderResponse.renderOnDomReadyJavscript()
。If you really can't give the
<body>
tag a wicket:id (I'll assume you don't have a BasePage that every, or almost every, other page extends in which to abstract this), it'll be not possible to know at page render time (when that<body>
tag is rendered) what class to append to it, it will be simply copied as is from your HTML to the output.You could achieve the same via javascript, however. Make your
Panel
implementIHeaderContributor
and useIHeaderResponse.renderOnDomReadyJavscript()
.我认为您使用 BodyTagAttributeModifier 的方向是正确的,至少根据 JavaDoc 是这样。链接文章中的编译问题源于使用不存在的构造函数...
在 SpecialSidebarComponent 中您应该能够执行此操作:
现在无法尝试此操作,因为我不在我的开发计算机上...
I think you were on the right track with BodyTagAttributeModifier, at least according to JavaDoc. The compilation problems in the linked article stem from the use of a non-existing Constructor...
in SpecialSidebarComponent you should be able to do this:
Can't try this right now because I'm not at my development computer...