39
Struts2 in a nutshell Shinpei Ohtani(shot6) http://twitter.com/shot6/

Struts2 in a nutshell

Embed Size (px)

Citation preview

Page 1: Struts2 in a nutshell

Struts2 in a nutshell

Shinpei Ohtani(shot6)

http://twitter.com/shot6/

Page 2: Struts2 in a nutshell

Agenda

About meWhat is Struts?The big pictureThe 3 core componentsStruts2 pluginStruts2 Pros and ConsStruts2 resources

Page 3: Struts2 in a nutshell

About me

Shinpei OhtaniHN : shot6 or shotWorks at ISI Dentsu, LtdWeb developer/Open source developer

Java/ActionScript/JavaScript/Silverlight/C#

Contact informationBlog : http://d.hatena.ne.jp/shot6/ (Japanese)Twitter : shot6Skype : shot_6

Page 4: Struts2 in a nutshell

About me(continued)Open source activity

T2 web frameworkWeb framework for Web2.0 style applicationGo to http://code.google.com/p/t-2/wiki/Index?wl=en

Page 5: Struts2 in a nutshell

What is Struts?

Struts isWeb framework for java web application

Based on Servlet and JSP spec

MVC pattern2 based architectureIt is licensed Apache Software License2Develop at http://struts.apache.org/

Page 6: Struts2 in a nutshell

What is Struts1?

Struts1 isThe defacto standard since 2002Front controller pattern from PofEAAXML configurationUser only develop Action and ActionForm, and

jspAction is a template pattern for accepts user actionActionForm is a DTO as user request

Page 7: Struts2 in a nutshell

What is Struts2?Struts2 is

Totally different from Struts1!Current version is 2.1.6-ga.It’s based on WebWork

Core engine is Xwork core

Action-based model(could be POJO)No more create ActionForm because Action takes that roll.

Filter and command pattern basicallyPlug-in architecture(so many plugins)Multi view templates

Page 8: Struts2 in a nutshell

The big picture

From http://struts.apache.org/2.1.6/docs/nutshell.html

Page 9: Struts2 in a nutshell

The components

3 core componentsActionResultInterceptor

Page 10: Struts2 in a nutshell

Action

The Action for user action created by:Implements Action interface from Xwork2Just create as POJO with methods:

Should have String execute() method.Or declared by struts.xmlOr use @Action from struts2-convention-plugin

All the samples take implement Action way though….

Page 11: Struts2 in a nutshell

Action(continued)

Other things of Action are:Default Action extension is .action.Action can chain one to one.Action can be configured by xml configuration

or annotation.

Page 12: Struts2 in a nutshell

Action sample

Page 13: Struts2 in a nutshell

Action configuration

Login action(mailreader2.Login)Login_input -> /Login.jspLogin_cancel -> redirect to Welcome actionLogin_expired -> chain to ChangePassword actionException mapping

Page 14: Struts2 in a nutshell

Action config by annotation

Annotations are: @Result for page transition @Action for action method name instead of juse execute @Actions for multiple names for just one method

Page 15: Struts2 in a nutshell

Result

Implements Result interface from Xwork2All the result of Action.These are:ServletDispatcherResult(default)ServletRedirectResultStreamResultJasperReportsResult from jasper-report-pluginFreeMarkerResultChartResultand so on….

Page 16: Struts2 in a nutshell

Result(continued)

Result is configured byxml

<result name="login" type="redirectAction">Login_input</result>

Annotation@Result(name = "list", value = "listPeople.action",

type = ServletRedirectResult.class)

Page 17: Struts2 in a nutshell

Default Result

Page 18: Struts2 in a nutshell

Interceptor

Interceptor is the filter hook for Action like AOP.Implements Interceptor interface from XWork2Struts2 extension is basically built on this.Struts2 itself is also implemented on

Interceptors.It is like AOP around invocationInterceptor is called from ActionProxy

Page 19: Struts2 in a nutshell

Interceptor(continued)

Interceptor IS the extensibility of Struts2.Autowired with SpringDebuggingException mappingFile uploadToken generator and validatorSecurity checker

Page 20: Struts2 in a nutshell

Interceptor(continued2)

To use Interceptor:by xml configurationby annotation

Page 21: Struts2 in a nutshell

Configuration Interceptor

Page 22: Struts2 in a nutshell

Plugins

So many Struts2 Plugins out there!Almost all is based on InterceptorSome of these is based on ResultThese plugins is configured by struts.xml

Page 23: Struts2 in a nutshell

Convention plugin

Execute CoCConverntion over configurationZero configuration(they are saying…)Directory traverse by convention

Page 24: Struts2 in a nutshell

Convention plugin(cont’d)

ConventionThe class named XxxAction is treated as Struts

Action.Example1 : examples.action.HogeAction

Translate to /hoge.actionMatch “action|actions|struts|struts2” as root package

by defaultThen, “Action” removes from the name and make it

lower case string(hoge)Add “.action” -> hoge.action

Page 25: Struts2 in a nutshell

Convention plugin(cont’d2)

ConventionExample2 : examples.action.mine.MyAction

Translate to /mine/my.actionExample.action package is as the rootSubpackage(mine) treats as urlSo here comes result : /mine/my.action

Page 26: Struts2 in a nutshell

Convention plugin sample

Page 27: Struts2 in a nutshell

Convention plugin sample

Page 28: Struts2 in a nutshell

Convention plugin sample

Without it, it won’t work at my sample.

Page 29: Struts2 in a nutshell

REST plugin

Handle REST-like requestInspired from Rails REST-like conventionBuilts on Convention pluginExample

examples.hoge.FooController -> /foo• If succeed, go foo-success.jsp• If failed, go foo-failed.jsp

Page 30: Struts2 in a nutshell

REST convention

Handle REST-like request(Rails-like conv)GET: /movies => method="index”GET: /movies/Thrillers => method="show",

id="Thrillers”GET: /movies/Thrillers;edit => method="edit",

id="Thrillers”GET: /movies/Thrillers/edit => method="edit",

id="Thrillers”GET: /movies/new => method="editNew”POST: /movies => method="create”

Page 31: Struts2 in a nutshell

REST sample(Action)

Page 32: Struts2 in a nutshell

REST sample(jsp)

Page 33: Struts2 in a nutshell

REST sample(xml)

Page 34: Struts2 in a nutshell

Struts1 plugin

Use legacy Struts1 codes at Struts2Struts1 Action and ActionForm works without

change.Supports commons-validator

Page 35: Struts2 in a nutshell

Struts1 config sampleSet Struts1Action to handle

Set actual Action class

Page 36: Struts2 in a nutshell

Struts2 Pros and Cons

ProsVery extensible if you know the architectureMany plugins are already developed so don’t

have to develop your ownEasy to collaborate with other template engine

Work with jsp/freemarker/velocity

So many view components

Page 37: Struts2 in a nutshell

Struts2 Pros and Cons

ConsConfused with many programming

model.Should be one unified model.Over-used CoC which is not maintainable.

Don’t think it’s good idea to have convention plugin

Confused between Xwork and Struts2Should be merged as one good product

Configuration inheritance is not good ideaXML in jar file is not easy to find

Page 38: Struts2 in a nutshell

Struts2 resources

Struts2 tutorialshttp://struts.apache.org/2.1.6/docs/tutorials.html

Struts2 developer guidehttp://struts.apache.org/2.1.6/docs/guides.html

Pluginshttp://struts.apache.org/2.1.6/docs/plugins.html

Page 39: Struts2 in a nutshell

Thanks

Enjoy with Struts2!!