Wicket - 添加 body 标签属性

发布于 2024-12-12 02:04:17 字数 1355 浏览 0 评论 0原文

我被困住了 -

我需要一个 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 技术交流群。

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

发布评论

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

评论(3

泼猴你往哪里跑 2024-12-19 02:04:17

我个人认为 Javascript 选项对于这种特定情况来说是最干净的。但是,您对 add(Component...) 最终的评论让我相信您可能对 setTransparentResolver(true) 方法感兴趣。它的工作原理如下...

BasePage.html

<body wicket:id="body">
    <div wicket:id="panel" />
</body>

BasePage.java

public class BasePage extends Page {

    public String bodyClass = "";

    public BasePage() {
       super();
       WebMarkupContainer bodyContainer = new WebMarkupContainer("body");
       bodyContainer.setTransparentResolver(true);
       bodyContainer.add(new SimpleAttributeModifier("class", new PropertyModel<String>(this, "bodyClass")));
    }
}

MyPage.java(扩展 BasePage)

public class MyPage extends BasePage {

    public MyPage() {
        super();
        add(new SidebarPanel("panel"));
        super.bodyClass = "sidebar";
    }
}

即使您没有将 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 the setTransparentResolver(true) method. Here's how it works...

BasePage.html

<body wicket:id="body">
    <div wicket:id="panel" />
</body>

BasePage.java

public class BasePage extends Page {

    public String bodyClass = "";

    public BasePage() {
       super();
       WebMarkupContainer bodyContainer = new WebMarkupContainer("body");
       bodyContainer.setTransparentResolver(true);
       bodyContainer.add(new SimpleAttributeModifier("class", new PropertyModel<String>(this, "bodyClass")));
    }
}

MyPage.java (extends BasePage)

public class MyPage extends BasePage {

    public MyPage() {
        super();
        add(new SidebarPanel("panel"));
        super.bodyClass = "sidebar";
    }
}

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.

不再让梦枯萎 2024-12-19 02:04:17

如果您确实无法为 标记提供 wicket:id (我假设您没有每个或几乎每个其他页面都扩展的 BasePage 来抽象这个),在页面渲染时(渲染 标记时)不可能知道要附加到哪个类,它将简单地从 HTML 中复制到输出。

不过,您可以通过 javascript 实现相同的目的。让您的 Panel 实现 IHeaderContributor 并使用 IHeaderResponse.renderOnDomReadyJavscript()

public abstract class SpecialSidebarComponent(String id) extends Panel 
                 implements IHeaderContributor {
    .....
    public void renderHead(IHeaderResponse response){
        String javascript = "document.body.setAttribute('class', 'sidebar');";
        response.renderOnDomReadyJavascript(javascript);
    }
    ....
}

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 implement IHeaderContributor and use IHeaderResponse.renderOnDomReadyJavscript().

public abstract class SpecialSidebarComponent(String id) extends Panel 
                 implements IHeaderContributor {
    .....
    public void renderHead(IHeaderResponse response){
        String javascript = "document.body.setAttribute('class', 'sidebar');";
        response.renderOnDomReadyJavascript(javascript);
    }
    ....
}
雨轻弹 2024-12-19 02:04:17

我认为您使用 BodyTagAttributeModifier 的方向是正确的,至少根据 JavaDoc 是这样。链接文章中的编译问题源于使用不存在的构造函数...

在 SpecialSidebarComponent 中您应该能够执行此操作:

add(new BodyTagAttributeModifier("class", Model.of("sidebar"), this));

现在无法尝试此操作,因为我不在我的开发计算机上...

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:

add(new BodyTagAttributeModifier("class", Model.of("sidebar"), this));

Can't try this right now because I'm not at my development computer...

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