Struts2 Plugin Development - Extending Struts2 Justin Yip.

Post on 12-Jan-2016

228 views 0 download

transcript

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

……

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

10

Spring-plugin Extending core component:

com.opensymphony.xwork2.ObjectFactory

11

Config Browser Plugin

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

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.

13

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

14

Roadmap Plugin Architecture

Extension points

Static resources

Tags

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" />

16

Roadmap Plugin Architecture

Extension points

Static resources

Tag development

17

Next we will create an example tag - accordion

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.

19

Creating a new Project

3. Change compiler level to 1.5

• Config “maven-compiler-plugin” below:

• Update project facts using “mvn eclipse:eclipse”

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

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”

22

Class Diagram

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

24

The freemarker model

accordion.ftl

25

The AccordionModel

Returns the tag bean in getBean() method

26

Accordion

Theme selecting

Template selecting

Tld class for jsp

27

Handling params

Extra params from value stack

Retrieve param value from tagxxx.ftl

28

View templateaccordion.ftl

accordion-close.ftl

29

Test Drive Install the artifact using “mvn install”

Test this tag in demo project

Test our API :

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