Struts2 Plugin Development - Extending Struts2 Justin Yip
2 Roadmap Plugin Architecture Extension points Static resources Tag development
3 Plugin Architecture Struts 2 provides a simple plugin architecture so that developers can extend the framework just by adding a JAR to the application's classpath. Struts2 Plugins are contained in a JAR Contains classes and configurations Extend, replace, or add to exsiting Struts framework functionality The JAR should contain a struts-plugin.xml file
4 Sample struts-plugin.xml
5 Roadmap Plugin Architecture Extension points Static resources Tag development
6 Struts2 Plugin provides Packages Interceptors Results Actions Core component Extensions Constants
7 Core component Extension Points typekey com.opensymphony.xwork2.ObjectFactorystruts.objectFactory com.opensymphony.xwork2.ActionProxyFactorystruts.actionProxyFactory com.opensymphony.xwork2.util.ObjectTypeDeterminerstruts.objectTypeDeterminer org.apache.struts2.dispatcher.mapper.ActionMapperstruts.mapper.class org.apache.struts2.dispatcher.multipart.MultiPartRequeststruts.multipart.parser ……
8 Example Plugins
9 Json-plugin Struts2 json plugin can serializing java objects to json objects The plugin Defines a package“json-default” , a reslut type “json” and a interceptor within
1010 Spring-plugin Extending core component : com.opensymphony.xwork2.ObjectFactory
1 Config Browser Plugin See an application's configuration at runtime. It is a small struts2 application. Provide actions, freemarker pages and stylesheets.
1212 Redwork Plugin The feature “zero config” of redwork plugin was implemented by Extending “com.opensymphony.xwork2.UnknownHandler”. RedworkUnknownHandler lookups actions and freemarker pages automatically, without a single line of configuration in struts.xml.
1313 Grid widgets The widgets provides a common action, a unified grid request handler.
1414 Roadmap Plugin Architecture Extension points Static resources Tags
1515 Static resources Some plugin, UI widgets in particular, need to provide static resource(stylesheets, images, etc…) within plugin archive. With struts2 plugin mechanism, you can package these resources in jar. Static resources are placed under “/static” in plugin jar, and we can refer it in page using “/static” as the path. Assuming “/static/background.jpg” is inside a plugin jar, And we refer it:
1616 Roadmap Plugin Architecture Extension points Static resources Tag development
1717 Next we will create an example tag - accordion
1818 Creating a new Project 1. Create a new Maven2 project “widgets-accordion” using Archetype: “maven- archetype-quickstart” 2. Fill out Group Id, Artifact Id, version etc.
1919 Creating a new Project 3. Change compiler level to 1.5 Config “maven-compiler-plugin” below: Update project facts using “mvn eclipse:eclipse”
2020 Creating a new Project 4. Add maven dependency org.apache.strutsstruts2-core org.apache.strutsstruts2-json-plugin javax.servletservlet-api javax.servlet.jspjsp-api junitjunit
2121 Plugin Entry point 5. Create a struts2 plugin configuration 6. declare a bean of type “org.apache.struts2.views.TagLibrary”, named “com.redhat.widget.DemoTagLibrary” Perfix with “rw”
2 Class Diagram
2323 The class A custom tag library must be descendant of “org.apache.struts2.views.TagLibrary Here we support freemarker only Return an new instance of DemoModel in getFreemarkerModels
2424 The freemarker model accordion.ftl
2525 The AccordionModel Returns the tag bean in getBean() method
2626 Accordion Theme selecting Template selecting Tld class for jsp
2727 Handling params Extra params from value stack Retrieve param value from tag xxx.ftl
2828 View template accordion.ftl accordion-close.ftl
2929 Test Drive Install the artifact using “mvn install” Test this tag in demo project Test our API : $("#a1").trigger("accordion-foldAll")