4
如何擴展現有的JSP自定義標籤?擴展JSP自定義標籤
如您所知,自定義標籤由兩部分組成,一個實現類和一個TLD文件。我可以擴展父定製標記的類,但是如何「擴展」其TLD文件?一個明顯的解決方案是剪切並粘貼它,然後添加我的東西,但是我想知道是否有像Apache Tile中擴展tile定義那樣的更優雅的解決方案。
謝謝。
如何擴展現有的JSP自定義標籤?擴展JSP自定義標籤
如您所知,自定義標籤由兩部分組成,一個實現類和一個TLD文件。我可以擴展父定製標記的類,但是如何「擴展」其TLD文件?一個明顯的解決方案是剪切並粘貼它,然後添加我的東西,但是我想知道是否有像Apache Tile中擴展tile定義那樣的更優雅的解決方案。
謝謝。
我不認爲你可以擴展現有的標籤,但類似的方法是使用一個共同的抽象超兩個標籤實現類:
// define repetitive stuff in abstract class
public abstract class TextConverterTag extends TagSupport{
private final long serialVersionUID = 1L;
private String text;
public String getText(){
return text;
}
public void setText(final String text){
this.text = text;
}
@Override
public final int doStartTag() throws JspException{
if(text != null){
try{
pageContext.getOut().append(process(text));
} catch(final IOException e){
throw new JspException(e);
}
}
return TagSupport.SKIP_BODY;
}
protected abstract CharSequence process(String input);
}
// implementing class defines core functionality only
public class UpperCaseConverter extends TextConverterTag{
private final long serialVersionUID = 1L;
@Override
protected CharSequence process(final String input){
return input.toUpperCase();
}
}
// implementing class defines core functionality only
public class LowerCaseConverter extends TextConverterTag{
private final long serialVersionUID = 1L;
@Override
protected CharSequence process(final String input){
return input.toLowerCase();
}
}
但我怕你將不得不配置每個標籤類分開,因爲我認爲taglibs中沒有抽象標籤定義。
剪切和粘貼是我知道這樣做的唯一方法。 – DwB 2010-09-30 19:39:11
是否有理由不能將您的標記定義添加到您正在擴展的TLD中? – Snekse 2010-09-30 22:27:31
你不想直接從一個開源項目中修改某個類,而是將它擴展到customzie中,這是同樣的原因......你想保持原來的狀態。 – 2010-09-30 22:37:11