struts2 plugin development

24
Struts2 Plugin Development - Extending Struts2 Justin Yip

Upload: minor

Post on 23-Feb-2016

62 views

Category:

Documents


0 download

DESCRIPTION

Struts2 Plugin Development. - Extending Struts2 Justin Yip. Roadmap. Plugin Architecture Extension points Static resources Tag development. Plugin Architecture. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Struts2 Plugin Development

Struts2 Plugin Development

- Extending Struts2

Justin Yip

Page 2: Struts2 Plugin Development

2

Roadmap Plugin Architecture Extension points Static resources Tag development

Page 3: Struts2 Plugin 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

Page 4: Struts2 Plugin Development

4

Sample struts-plugin.xml

Page 5: Struts2 Plugin Development

5

Roadmap Plugin Architecture Extension points Static resources Tag development

Page 6: Struts2 Plugin Development

6

Struts2 Plugin provides Packages Interceptors Results Actions Core component Extensions Constants

Page 7: Struts2 Plugin Development

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

……

Page 8: Struts2 Plugin Development

8

Plugin examples

Page 9: Struts2 Plugin Development

9

Json-plugin Struts2 json 插件提供了序列化成为 json 对象的功能 插件定义了一个包 “ json-default” ,包内定义了一个结果类型“ json” ,还有一个负责将 java 对象序列化成 json 对象的拦截器

Page 10: Struts2 Plugin Development

10

Spring-plugin Extending core component :

com.opensymphony.xwork2.ObjectFactory

Page 11: Struts2 Plugin Development

11

Config Browser PluginSee an application's configuration at runtime. It is a small struts2

application. Provide actions, freemarker pages and stylesheets.

Page 12: Struts2 Plugin Development

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

Page 13: Struts2 Plugin Development

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.

Page 14: Struts2 Plugin Development

14

Creating a new Project3. Change compiler level to 1.5

• Config “maven-compiler-plugin” below:

• Update project facts using “mvn eclipse:eclipse”

Page 15: Struts2 Plugin Development

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

Page 16: Struts2 Plugin Development

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”

Page 17: Struts2 Plugin Development

17

Class Diagram

Page 18: Struts2 Plugin Development

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

Page 19: Struts2 Plugin Development

19

The freemarker model

accordion.ftl

Page 20: Struts2 Plugin Development

20

The AccordionModel

Returns the tag bean in getBean() method

Page 21: Struts2 Plugin Development

21

Accordion

Theme selecting

Template selecting

Tld class for jsp

Page 22: Struts2 Plugin Development

22

Handling params

Extra params from value stack

Retrieve param value from tagxxx.ftl

Page 23: Struts2 Plugin Development

23

View templateaccordion.ftl

accordion-close.ftl

Page 24: Struts2 Plugin Development

24

Test Drive Install the artifact using “mvn install” Test this tag in demo project

Test our API : $("#a1").trigger("accordion-foldAll")