Extending JSP Custom Tags

Tom*_*ker 5 java jsp jsp-tags

How do you extend an existing JSP custom tag?

As you know, a custom tag consists of two parts, an implementation class and a TLD file. I can extend the parent custom tag's class, but how do you "extend" its TLD file? One obvious solution is to cut and paste it and then add my stuff, but I wonder if there's a more elegant solution like the way you extend a tiles definition in Apache Tiles.

Thanks.

Sea*_*oyd 3

我认为您不能扩展现有标签,但类似的方法是对两个标签实现类使用公共抽象超类:

// 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();
    }
}
Run Code Online (Sandbox Code Playgroud)

但恐怕您必须单独配置每个标签类,因为我认为标签库中没有抽象标签定义。