自定义struts2标签

网上有人指出基本步骤如下:
One of the developers recently mentioned they is writing a document about how to do this and I think there was talk about creating a maven archetype.
In the mean-time, try following some of existing code. The current version of the struts2 yui plugin provides a nice simple example as it only contains two tags and is packaged as a a plugin (http://code.google.com/p/struts2yuiplugin/source).【见附件:】
I find it's easiest to follow that example completely - use the maven plugin to generate the TLD and package your tags as a struts2 plugin. This isn't mandatory though as you can write the TLD by hand and include the classes in your webapp directly.
In general:
1. create a bean that extends UIBean or ClosingUIBean (for open & close tags), identifies the templates and populates the template context
2. create a tag class that extends AbstractUITag that creates the bean instance and sets the properties of the bean
3. create the templates referenced by the bean
After completing these 3 steps you can start using your tag if you write a TLD and include it in your classpath. If you copy the pom.xml from the example able you can use maven to generate the TLD (if you've used the appropriate annotations).
The remaining steps allowing your tags to be uses within freemarker and velocity templates (rather than only JSP)
4. Create a TagLibrary and TagModels for Freemarker and Directive's for velocity
5. Create struts-plugin.xml that declares your TagLibrary bean
----------------------------------------------------------------------------------------------------------------------------------------------------------------------
plugin配置中:
<bean type="org.apache.struts2.views.TagLibrary" name="tagtest" class="test.s2.plugin.views.TestTagLibrary" />
freemarker models中:
    public AncorModel getAncor() {
        if (this.ancor == null) {
            this.ancor = new AncorModel(this.stack, this.req, this.res);
        }
        return this.ancor;
    }
那么应该这样使用:
<@tagtest.ancor ... />
注意前缀tagtest和后缀ancor分别对应蓝色斜体字。【shit!】
然而velocity的用法又不一样:
AbstractDirective:
    public String getName()
    {       
        return "testa" ;
    }
#testa (... )
这个与配置无关,是由getName返回的来决定的。


Total views.

© 2013 - 2018. All rights reserved.

Powered by Hydejack v6.6.1