Struts2 in a nutshell Shinpei Ohtani(shot6) http://twitter.com/shot6/
Agenda About me What is Struts? The big picture The 3 core components Struts2 plugin Struts2 Pros and Cons Struts2 resources
About me Shinpei Ohtani HN : shot6 or shot Works at ISI Dentsu, Ltd Web developer/Open source developer Java/ActionScript/JavaScript/Silverlight/C# Contact information Blog :  http://d.hatena.ne.jp/shot6/  (Japanese) Twitter  : shot6 Skype : shot_6
About me(continued) Open source activity T2 web framework Web framework for Web2.0 style application Go to  http://code.google.com/p/t-2/wiki/Index?wl=en
What is Struts? Struts is Web framework for java web application Based on Servlet and JSP spec MVC pattern2 based architecture It is licensed Apache Software License2 Develop at http://struts.apache.org/
What is Struts1? Struts1 is The defacto standard since 2002 Front controller pattern from PofEAA XML configuration User only develop Action and ActionForm, and jsp Action is a template pattern for accepts user action ActionForm 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 basically Plug-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 components Action Result Interceptor
Action The Action for user action created by: Implements Action interface from Xwork2 Just create as POJO with methods: Should have String execute() method. Or declared by struts.xml Or 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.jsp Login_cancel -> redirect to Welcome action Login_expired -> chain to ChangePassword action Exception 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 Xwork2 All the result of Action.These are: ServletDispatcherResult(default) ServletRedirectResult StreamResult JasperReportsResult from jasper-report-plugin FreeMarkerResult ChartResult and so on….
Result(continued) Result is configured by xml <result name=&quot;login&quot; type=&quot;redirectAction&quot;>Login_input</result> Annotation @Result(name = &quot;list&quot;, value = &quot;listPeople.action&quot;, type = ServletRedirectResult. class)
Default Result
Interceptor Interceptor is the filter hook for Action like AOP. Implements Interceptor interface from XWork2 Struts2 extension is basically built on this. Struts2 itself is also implemented on Interceptors. It is like AOP around invocation Interceptor is called from ActionProxy
Interceptor(continued) Interceptor IS the extensibility of Struts2. Autowired with Spring Debugging Exception mapping File upload Token generator and validator Security checker
Interceptor(continued2) To use Interceptor: by xml configuration by annotation
Configuration Interceptor
Plugins So many Struts2 Plugins out there! Almost all is based on Interceptor Some of these is based on Result These plugins is configured by struts.xml
Convention plugin Execute CoC Converntion over configuration Zero configuration(they are saying…) Directory traverse by convention
Convention plugin(cont’d) Convention The class named XxxAction is treated as Struts Action. Example1 : examples.action.HogeAction Translate to /hoge.action Match “ action|actions|struts|struts2 ” as root package by default Then, “Action” removes from the name and make it lower case string(hoge) Add “.action” -> hoge.action
Convention plugin(cont’d2) Convention Example2 : examples.action.mine.MyAction Translate to /mine/my.action Example.action package is as the root Subpackage(mine) treats as url So 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 request Inspired from Rails REST-like convention Builts on Convention plugin Example 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=&quot;index” GET: /movies/Thrillers => method=&quot;show&quot;, id=&quot;Thrillers” GET: /movies/Thrillers;edit => method=&quot;edit&quot;, id=&quot;Thrillers” GET: /movies/Thrillers/edit => method=&quot;edit&quot;, id=&quot;Thrillers” GET: /movies/new => method=&quot;editNew” POST: /movies => method=&quot;create”
REST sample(Action)
REST sample(jsp)
REST sample(xml)
Struts1 plugin Use legacy Struts1 codes at Struts2 Struts1 Action and ActionForm works without change. Supports commons-validator
Struts1 config sample Set Struts1Action to handle Set actual Action class
Struts2 Pros and Cons Pros Very extensible if you know the architecture Many plugins are already developed so don’t have to develop your own Easy to collaborate with other template engine Work with jsp/freemarker/velocity So many view components
Struts2 Pros and Cons Cons Confused 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 Struts2 Should be merged as one good product Configuration inheritance is not good idea XML in jar file is not easy to find
Struts2 resources Struts2 tutorials http://struts.apache.org/2.1.6/docs/tutorials.html Struts2 developer guide http://struts.apache.org/2.1.6/docs/guides.html Plugins http://struts.apache.org/2.1.6/docs/plugins.html
Thanks Enjoy with Struts2!!

Struts2 in a nutshell

  • 1.
    Struts2 in anutshell Shinpei Ohtani(shot6) http://twitter.com/shot6/
  • 2.
    Agenda About meWhat is Struts? The big picture The 3 core components Struts2 plugin Struts2 Pros and Cons Struts2 resources
  • 3.
    About me ShinpeiOhtani HN : shot6 or shot Works at ISI Dentsu, Ltd Web developer/Open source developer Java/ActionScript/JavaScript/Silverlight/C# Contact information Blog : http://d.hatena.ne.jp/shot6/ (Japanese) Twitter : shot6 Skype : shot_6
  • 4.
    About me(continued) Opensource activity T2 web framework Web framework for Web2.0 style application Go to http://code.google.com/p/t-2/wiki/Index?wl=en
  • 5.
    What is Struts?Struts is Web framework for java web application Based on Servlet and JSP spec MVC pattern2 based architecture It is licensed Apache Software License2 Develop at http://struts.apache.org/
  • 6.
    What is Struts1?Struts1 is The defacto standard since 2002 Front controller pattern from PofEAA XML configuration User only develop Action and ActionForm, and jsp Action is a template pattern for accepts user action ActionForm is a DTO as user request
  • 7.
    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 basically Plug-in architecture(so many plugins) Multi view templates
  • 8.
    The big pictureFrom http://struts.apache.org/2.1.6/docs/nutshell.html
  • 9.
    The components 3core components Action Result Interceptor
  • 10.
    Action The Actionfor user action created by: Implements Action interface from Xwork2 Just create as POJO with methods: Should have String execute() method. Or declared by struts.xml Or use @Action from struts2-convention-plugin All the samples take implement Action way though….
  • 11.
    Action(continued) Other thingsof Action are: Default Action extension is .action . Action can chain one to one. Action can be configured by xml configuration or annotation.
  • 12.
  • 13.
    Action configuration Loginaction(mailreader2.Login) Login_input -> /Login.jsp Login_cancel -> redirect to Welcome action Login_expired -> chain to ChangePassword action Exception mapping
  • 14.
    Action config byannotation Annotations are: @Result for page transition @Action for action method name instead of juse execute @Actions for multiple names for just one method
  • 15.
    Result Implements Resultinterface from Xwork2 All the result of Action.These are: ServletDispatcherResult(default) ServletRedirectResult StreamResult JasperReportsResult from jasper-report-plugin FreeMarkerResult ChartResult and so on….
  • 16.
    Result(continued) Result isconfigured by xml <result name=&quot;login&quot; type=&quot;redirectAction&quot;>Login_input</result> Annotation @Result(name = &quot;list&quot;, value = &quot;listPeople.action&quot;, type = ServletRedirectResult. class)
  • 17.
  • 18.
    Interceptor Interceptor isthe filter hook for Action like AOP. Implements Interceptor interface from XWork2 Struts2 extension is basically built on this. Struts2 itself is also implemented on Interceptors. It is like AOP around invocation Interceptor is called from ActionProxy
  • 19.
    Interceptor(continued) Interceptor ISthe extensibility of Struts2. Autowired with Spring Debugging Exception mapping File upload Token generator and validator Security checker
  • 20.
    Interceptor(continued2) To useInterceptor: by xml configuration by annotation
  • 21.
  • 22.
    Plugins So manyStruts2 Plugins out there! Almost all is based on Interceptor Some of these is based on Result These plugins is configured by struts.xml
  • 23.
    Convention plugin ExecuteCoC Converntion over configuration Zero configuration(they are saying…) Directory traverse by convention
  • 24.
    Convention plugin(cont’d) ConventionThe class named XxxAction is treated as Struts Action. Example1 : examples.action.HogeAction Translate to /hoge.action Match “ action|actions|struts|struts2 ” as root package by default Then, “Action” removes from the name and make it lower case string(hoge) Add “.action” -> hoge.action
  • 25.
    Convention plugin(cont’d2) ConventionExample2 : examples.action.mine.MyAction Translate to /mine/my.action Example.action package is as the root Subpackage(mine) treats as url So here comes result : /mine/my.action
  • 26.
  • 27.
  • 28.
    Convention plugin sampleWithout it, it won’t work at my sample.
  • 29.
    REST plugin HandleREST-like request Inspired from Rails REST-like convention Builts on Convention plugin Example examples.hoge.FooController -> /foo If succeed, go foo-success.jsp If failed, go foo-failed.jsp
  • 30.
    REST convention HandleREST-like request(Rails-like conv) GET: /movies => method=&quot;index” GET: /movies/Thrillers => method=&quot;show&quot;, id=&quot;Thrillers” GET: /movies/Thrillers;edit => method=&quot;edit&quot;, id=&quot;Thrillers” GET: /movies/Thrillers/edit => method=&quot;edit&quot;, id=&quot;Thrillers” GET: /movies/new => method=&quot;editNew” POST: /movies => method=&quot;create”
  • 31.
  • 32.
  • 33.
  • 34.
    Struts1 plugin Uselegacy Struts1 codes at Struts2 Struts1 Action and ActionForm works without change. Supports commons-validator
  • 35.
    Struts1 config sampleSet Struts1Action to handle Set actual Action class
  • 36.
    Struts2 Pros andCons Pros Very extensible if you know the architecture Many plugins are already developed so don’t have to develop your own Easy to collaborate with other template engine Work with jsp/freemarker/velocity So many view components
  • 37.
    Struts2 Pros andCons Cons Confused 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 Struts2 Should be merged as one good product Configuration inheritance is not good idea XML in jar file is not easy to find
  • 38.
    Struts2 resources Struts2tutorials http://struts.apache.org/2.1.6/docs/tutorials.html Struts2 developer guide http://struts.apache.org/2.1.6/docs/guides.html Plugins http://struts.apache.org/2.1.6/docs/plugins.html
  • 39.