29
Struts2 Plugin Development - Extending Struts2 Justin Yip

Struts2 Plugin Development - Extending Struts2 Justin Yip

Embed Size (px)

Citation preview

Page 1: Struts2 Plugin Development - Extending Struts2 Justin Yip

Struts2 Plugin Development

- Extending Struts2

Justin Yip

Page 2: Struts2 Plugin Development - Extending Struts2 Justin Yip

2

Roadmap Plugin Architecture

Extension points

Static resources

Tag development

Page 3: Struts2 Plugin Development - Extending Struts2 Justin Yip

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

Page 4: Struts2 Plugin Development - Extending Struts2 Justin Yip

4

Sample struts-plugin.xml

Page 5: Struts2 Plugin Development - Extending Struts2 Justin Yip

5

Roadmap Plugin Architecture

Extension points

Static resources

Tag development

Page 6: Struts2 Plugin Development - Extending Struts2 Justin Yip

6

Struts2 Plugin provides Packages

Interceptors

Results

Actions

Core component Extensions

Constants

Page 7: Struts2 Plugin Development - Extending Struts2 Justin Yip

7

Core component Extension Pointstype key

com.opensymphony.xwork2.ObjectFactory struts.objectFactory

com.opensymphony.xwork2.ActionProxyFactory struts.actionProxyFactory

com.opensymphony.xwork2.util.ObjectTypeDeterminer

struts.objectTypeDeterminer

org.apache.struts2.dispatcher.mapper.ActionMapper

struts.mapper.class

org.apache.struts2.dispatcher.multipart.MultiPartRequest

struts.multipart.parser

……

Page 8: Struts2 Plugin Development - Extending Struts2 Justin Yip

8

Example Plugins

Page 9: Struts2 Plugin Development - Extending Struts2 Justin Yip

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

Page 10: Struts2 Plugin Development - Extending Struts2 Justin Yip

10

Spring-plugin Extending core component:

com.opensymphony.xwork2.ObjectFactory

Page 11: Struts2 Plugin Development - Extending Struts2 Justin Yip

11

Config Browser Plugin

See an application's configuration at runtime. It is a small struts2 application. Provide actions, freemarker pages and stylesheets.

Page 12: Struts2 Plugin Development - Extending Struts2 Justin Yip

12

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.

Page 13: Struts2 Plugin Development - Extending Struts2 Justin Yip

13

Grid widgets The widgets provides a common action, a unified grid request handler.

Page 14: Struts2 Plugin Development - Extending Struts2 Justin Yip

14

Roadmap Plugin Architecture

Extension points

Static resources

Tags

Page 15: Struts2 Plugin Development - Extending Struts2 Justin Yip

15

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:

<@s.url value="/static/background.jpg" var=“img_bg" />

Page 16: Struts2 Plugin Development - Extending Struts2 Justin Yip

16

Roadmap Plugin Architecture

Extension points

Static resources

Tag development

Page 17: Struts2 Plugin Development - Extending Struts2 Justin Yip

17

Next we will create an example tag - accordion

Page 18: Struts2 Plugin Development - Extending Struts2 Justin Yip

18

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.

Page 19: Struts2 Plugin Development - Extending Struts2 Justin Yip

19

Creating a new Project

3. Change compiler level to 1.5

• Config “maven-compiler-plugin” below:

• Update project facts using “mvn eclipse:eclipse”

Page 20: Struts2 Plugin Development - Extending Struts2 Justin Yip

20

Creating a new Project

4. 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

Page 21: Struts2 Plugin Development - Extending Struts2 Justin Yip

21

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”

Page 22: Struts2 Plugin Development - Extending Struts2 Justin Yip

22

Class Diagram

Page 23: Struts2 Plugin Development - Extending Struts2 Justin Yip

23

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

Page 24: Struts2 Plugin Development - Extending Struts2 Justin Yip

24

The freemarker model

accordion.ftl

Page 25: Struts2 Plugin Development - Extending Struts2 Justin Yip

25

The AccordionModel

Returns the tag bean in getBean() method

Page 26: Struts2 Plugin Development - Extending Struts2 Justin Yip

26

Accordion

Theme selecting

Template selecting

Tld class for jsp

Page 27: Struts2 Plugin Development - Extending Struts2 Justin Yip

27

Handling params

Extra params from value stack

Retrieve param value from tagxxx.ftl

Page 28: Struts2 Plugin Development - Extending Struts2 Justin Yip

28

View templateaccordion.ftl

accordion-close.ftl

Page 29: Struts2 Plugin Development - Extending Struts2 Justin Yip

29

Test Drive Install the artifact using “mvn install”

Test this tag in demo project

Test our API :

$("#a1").trigger("accordion-foldAll")