2009-06-02 28 views
2

我没有太多Tapestry的经验,所以我不知道从哪里开始。你会如何推荐我在Tapestry 4中扩展Insert类?

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

我基本上最终想要产生类似<span class="myClass">The value</span>的东西。

为什么通过扩展插入?因为应用程序已经完成了很多工作,但我们意识到无论我们使用Insert,我们都需要这个CSS类。我们只需在全部文件中对'type =“插入”>“与'type =”NewInsert“>'进行全局替换。

回答

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; 
0

为什么覆盖插入?为什么不创建您自己的InsertSpan组件?看看插入的源代码,你会看到它是多么的简单...随意剪切和粘贴,它是开源的。

更好的是,升级到Tapestry 5; Tapestry 4的东西在四年左右没有得到积极的开发。