240 likes | 383 Views
Struts2 Plugin Development. - Extending Struts2 Justin Yip. Roadmap. Plugin Architecture Extension points Static resources Tag development. Plugin Architecture.
E N D
Struts2 Plugin Development - Extending Struts2 Justin Yip
Roadmap • PluginArchitecture • Extension points • Static resources • Tag development
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
Roadmap • PluginArchitecture • Extension points • Static resources • Tag development
Struts2 Plugin provides • Packages • Interceptors • Results • Actions • Core component Extensions • Constants
Json-plugin • Struts2 json插件提供了序列化成为json对象的功能 • 插件定义了一个包 “json-default”,包内定义了一个结果类型“json”,还有一个负责将java对象序列化成json对象的拦截器
Spring-plugin • Extending core component:com.opensymphony.xwork2.ObjectFactory
Config Browser Plugin See an application's configuration at runtime. It is a small struts2 application. Provide actions, freemarker pages and stylesheets.
Static resources Some plugin, UI widgets in particular, need to provide static resource(stylesheets, images, etc…) within plugin archive. With struts2 pluginmechanism, 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 pluginjar, And we refer it: <@s.url value="/static/background.jpg" var=“img_bg" />
Creating a new Project • Create a new Maven2 project “widgets-accordion” using Archetype: “maven-archetype-quickstart” • Fill out Group Id, Artifact Id, version etc.
Creating a new Project • Change compiler level to 1.5 • Config “maven-compiler-plugin” below: • Update project facts using “mvneclipse:eclipse”
Creating a new Project • Add maven dependency • org.apache.struts struts2-core • org.apache.struts struts2-json-plugin • javax.servletservlet-api • javax.servlet.jspjsp-api • junitjunit
Plugin Entry point • Create a struts2 plugin configuration • declare a bean of type “org.apache.struts2.views.TagLibrary”, named “com.redhat.widget.DemoTagLibrary” Perfix with “rw”
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
The freemarker model accordion.ftl
The AccordionModel • Returns the tag bean in getBean() method
Accordion Tld class for jsp Template selecting Theme selecting
Handling params Extra params from value stack Retrieve param value from tag xxx.ftl
View template accordion.ftl accordion-close.ftl
Test Drive • Install the artifact using “mvn install” • Test this tag in demo project • Test our API : $("#a1").trigger("accordion-foldAll")