2

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

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

我基本上想以生成类似<span class="myClass">The value</span>.

为什么要通过扩展插入来做到这一点?因为应用程序已经完成了很多,但我们意识到在使用 Insert 的任何地方都需要这个 CSS 类。我们将在所有文件中使用 'type="NewInsert">' 对 'type="Insert">' 进行全局替换。

4

2 回答 2

2

为了实现我想要的,我必须重写 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;
于 2009-06-23T14:42:16.220 回答
0

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

更好的是,考虑升级到 Tapestry 5;Tapestry 4 的东西在大约四年内没有得到积极开发。

于 2012-10-16T18:00:14.400 回答