您建议我如何扩展 Tapestry 4 中的 Insert 类?

发布于 2024-07-23 05:34:24 字数 354 浏览 7 评论 0原文

我没有太多 Tapestry 经验,所以我真的不知道从哪里开始。

我需要使用一个新组件(例如 NewInsert)扩展 Insert 组件,该组件将给定的 CSS 类应用于要插入的内容。 我该怎么做?

我基本上希望最终得到生成类似 The value 之类的东西。

为什么要通过扩展Insert来实现呢? 因为应用程序已经基本完成,但我们意识到在任何使用 Insert 的地方都需要这个 CSS 类。 我们只需对 'type="Insert">' 进行全局替换 与 'type="NewInsert">' 在所有文件中。

I don't have much Tapestry experience so I don't really know where to start.

I need to extend the Insert component with a new component, say NewInsert, that applies a given CSS class to what is being inserted. How should I do this?

I basically want to end up with something that generates something like <span class="myClass">The value</span>.

Why do it by extending Insert? Because the application is pretty much done but we realized that everywhere we use Insert we need this CSS class. We'll just do a global replace on 'type="Insert">' with 'type="NewInsert">' in all files.

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

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

发布评论

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

评论(2

放我走吧 2024-07-30 05:34:24

为了实现我想要的,我必须重写 Insert 的 renderComponent 方法。 这只是因为 Tapestry 4.0.2 没有 setStyleClass 方法。 它看起来基本上就像

    if (!cycle.isRewinding()) {
      Object value = getValue();

      if (value != null) {
        String styleClass;
        String insert = null;
        Format format = getFormat();

        if (format == null) {
          insert = value.toString();
        }
        else {
          insert = format.format(value);
        }

        styleClass = getStyleClass();

        if (styleClass == null) {
          /* No classes specified */
          styleClass = MY_CLASS;
        }
        else {
          /* Append the preserveWhiteSpace class to the string listing the style classes. */
          styleClass += " " + MY_CLASS;
        }

        if (styleClass != null) {
          writer.begin("span");
          writer.attribute("class", styleClass);

          renderInformalParameters(writer, cycle);
        }

        writer.print(insert, getRaw());

        if (styleClass != null) {
          /* </span> */
          writer.end();
        }
      }
    }
  }

如果我们有一个 setStyleClass 方法我们就可以完成

setStyleClass(MY_CLASS);
super.renderComponent;

To achieve what I wanted I had to override Insert's renderComponent method. This is only because Tapestry 4.0.2 does not have a setStyleClass method. It looked basically like

    if (!cycle.isRewinding()) {
      Object value = getValue();

      if (value != null) {
        String styleClass;
        String insert = null;
        Format format = getFormat();

        if (format == null) {
          insert = value.toString();
        }
        else {
          insert = format.format(value);
        }

        styleClass = getStyleClass();

        if (styleClass == null) {
          /* No classes specified */
          styleClass = MY_CLASS;
        }
        else {
          /* Append the preserveWhiteSpace class to the string listing the style classes. */
          styleClass += " " + MY_CLASS;
        }

        if (styleClass != null) {
          writer.begin("span");
          writer.attribute("class", styleClass);

          renderInformalParameters(writer, cycle);
        }

        writer.print(insert, getRaw());

        if (styleClass != null) {
          /* </span> */
          writer.end();
        }
      }
    }
  }

If we have a setStyleClass method we could have just done

setStyleClass(MY_CLASS);
super.renderComponent;
我不在是我 2024-07-30 05:34:24

为什么要覆盖插入? 为什么不创建自己的 InsertSpan 组件呢? 只需查看 Insert 的源代码,您就会发现它是多么简单......可以随意剪切和粘贴,它是开源的。

更好的是,考虑升级到 Tapestry 5; Tapestry 4 的东西已经有四年没有积极开发了。

Why override Insert? Why not create your own InsertSpan component? Just look at the source for Insert and you'll see how simple it is ... feel free to cut-and-paste, it's open source.

Better yet, look into upgrading to Tapestry 5; the Tapestry 4 stuff has not been actively developed in about four years.

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