Struts2 Plugin Development
description
Transcript of Struts2 Plugin Development
Struts2 Plugin Development
- Extending Struts2
Justin Yip
2
Roadmap Plugin Architecture Extension points Static resources Tag development
3
Plugin ArchitectureStruts 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 Pointstype key
com.opensymphony.xwork2.ObjectFactory struts.objectFactorycom.opensymphony.xwork2.ActionProxyFactory struts.actionProxyFactorycom.opensymphony.xwork2.util.ObjectTypeDeterminer
struts.objectTypeDeterminer
org.apache.struts2.dispatcher.mapper.ActionMapper
struts.mapper.class
org.apache.struts2.dispatcher.multipart.MultiPartRequest
struts.multipart.parser
……
8
Plugin examples
9
Json-plugin Struts2 json 插件提供了序列化成为 json 对象的功能 插件定义了一个包 “ json-default” ,包内定义了一个结果类型“ json” ,还有一个负责将 java 对象序列化成 json 对象的拦截器
10
Spring-plugin Extending core component :
com.opensymphony.xwork2.ObjectFactory
11
Config Browser PluginSee an application's configuration at runtime. It is a small struts2
application. Provide actions, freemarker pages and stylesheets.
12
Static resourcesSome 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:<@s.url value="/static/background.jpg" var=“img_bg" />
13
Creating a new Project1. Create a new Maven2 project “widgets-accordion” using Archetype:
“maven-archetype-quickstart”2. Fill out Group Id, Artifact Id, version etc.
14
Creating a new Project3. Change compiler level to 1.5
• Config “maven-compiler-plugin” below:
• Update project facts using “mvn eclipse:eclipse”
15
Creating a new Project4. Add maven dependency
org.apache.struts struts2-core org.apache.struts struts2-json-plugin javax.servlet servlet-api javax.servlet.jsp jsp-api junit junit
16
Plugin Entry point5. Create a struts2 plugin configuration6. declare a bean of type “org.apache.struts2.views.TagLibrary”, named
“com.redhat.widget.DemoTagLibrary”
Perfix with “rw”
17
Class Diagram
18
The class
A custom tag library must be descendant of “org.apache.struts2.views.TagLibraryHere we support freemarker onlyReturn an new instance of DemoModel in getFreemarkerModels
19
The freemarker model
accordion.ftl
20
The AccordionModel
Returns the tag bean in getBean() method
21
Accordion
Theme selecting
Template selecting
Tld class for jsp
22
Handling params
Extra params from value stack
Retrieve param value from tagxxx.ftl
23
View templateaccordion.ftl
accordion-close.ftl
24
Test Drive Install the artifact using “mvn install” Test this tag in demo project
Test our API : $("#a1").trigger("accordion-foldAll")