struts2 in a nutshell
Post on 24-May-2015
4.137 Views
Preview:
TRANSCRIPT
Struts2 in a nutshell
Shinpei Ohtani(shot6)
http://twitter.com/shot6/
Agenda
About meWhat is Struts?The big pictureThe 3 core componentsStruts2 pluginStruts2 Pros and ConsStruts2 resources
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
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
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/
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
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
The big picture
From http://struts.apache.org/2.1.6/docs/nutshell.html
The components
3 core componentsActionResultInterceptor
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….
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.
Action sample
Action configuration
Login action(mailreader2.Login)Login_input -> /Login.jspLogin_cancel -> redirect to Welcome actionLogin_expired -> chain to ChangePassword actionException mapping
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
Result
Implements Result interface from Xwork2All the result of Action.These are:ServletDispatcherResult(default)ServletRedirectResultStreamResultJasperReportsResult from jasper-report-pluginFreeMarkerResultChartResultand so on….
Result(continued)
Result is configured byxml
<result name="login" type="redirectAction">Login_input</result>
Annotation@Result(name = "list", value = "listPeople.action",
type = ServletRedirectResult.class)
Default Result
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
Interceptor(continued)
Interceptor IS the extensibility of Struts2.Autowired with SpringDebuggingException mappingFile uploadToken generator and validatorSecurity checker
Interceptor(continued2)
To use Interceptor:by xml configurationby annotation
Configuration Interceptor
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
Convention plugin
Execute CoCConverntion over configurationZero configuration(they are saying…)Directory traverse by convention
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
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
Convention plugin sample
Convention plugin sample
Convention plugin sample
Without it, it won’t work at my sample.
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
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”
REST sample(Action)
REST sample(jsp)
REST sample(xml)
Struts1 plugin
Use legacy Struts1 codes at Struts2Struts1 Action and ActionForm works without
change.Supports commons-validator
Struts1 config sampleSet Struts1Action to handle
Set actual Action class
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
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
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
Thanks
Enjoy with Struts2!!
top related