Struts2 in a nutshell Shinpei Ohtani(shot6) http://twitter.com/shot6/
Agenda <ul><li>About me </li></ul><ul><li>What is Struts? </li></ul><ul><li>The big picture </li></ul><ul><li>The 3 core c...
About me <ul><li>Shinpei Ohtani </li></ul><ul><ul><li>HN : shot6 or shot </li></ul></ul><ul><ul><li>Works at ISI Dentsu, L...
About me(continued) <ul><li>Open source activity </li></ul><ul><ul><li>T2 web framework </li></ul></ul><ul><ul><ul><li>Web...
What is Struts? <ul><li>Struts is </li></ul><ul><ul><li>Web framework for java web application </li></ul></ul><ul><ul><ul>...
What is Struts1? <ul><li>Struts1 is </li></ul><ul><ul><li>The defacto standard since 2002 </li></ul></ul><ul><ul><li>Front...
What is Struts2? <ul><li>Struts2 is </li></ul><ul><ul><li>Totally  different  from Struts1! </li></ul></ul><ul><ul><li>Cur...
The big picture From http://struts.apache.org/2.1.6/docs/nutshell.html
The components <ul><li>3 core components </li></ul><ul><ul><li>Action </li></ul></ul><ul><ul><li>Result </li></ul></ul><ul...
Action <ul><li>The Action for user action created by: </li></ul><ul><ul><li>Implements Action interface from Xwork2 </li><...
Action(continued) <ul><li>Other things of Action are: </li></ul><ul><ul><li>Default Action extension is  .action . </li></...
Action sample
Action configuration <ul><li>Login action(mailreader2.Login) </li></ul><ul><ul><li>Login_input -> /Login.jsp </li></ul></u...
Action config by annotation <ul><li>Annotations are: </li></ul><ul><ul><li>@Result for page transition </li></ul></ul><ul>...
Result <ul><ul><li>Implements Result interface from Xwork2 </li></ul></ul><ul><li>All the result of Action.These are: </li...
Result(continued) <ul><li>Result is configured by </li></ul><ul><ul><li>xml </li></ul></ul><ul><ul><ul><li><result name=&q...
Default Result
Interceptor <ul><li>Interceptor is the filter hook for Action like AOP. </li></ul><ul><ul><li>Implements Interceptor inter...
Interceptor(continued) <ul><li>Interceptor IS the extensibility of Struts2. </li></ul><ul><ul><li>Autowired with Spring </...
Interceptor(continued2) <ul><li>To use Interceptor: </li></ul><ul><ul><li>by xml configuration </li></ul></ul><ul><ul><li>...
Configuration Interceptor
Plugins <ul><li>So many Struts2 Plugins out there! </li></ul><ul><ul><li>Almost all is based on Interceptor </li></ul></ul...
Convention plugin <ul><li>Execute CoC </li></ul><ul><ul><li>Converntion over configuration </li></ul></ul><ul><ul><li>Zero...
Convention plugin(cont’d) <ul><li>Convention </li></ul><ul><ul><li>The class named XxxAction is treated as Struts Action. ...
Convention plugin(cont’d2) <ul><li>Convention </li></ul><ul><ul><li>Example2 : examples.action.mine.MyAction </li></ul></u...
Convention plugin sample
Convention plugin sample
Convention plugin sample Without it, it won’t work at my sample.
REST plugin <ul><li>Handle REST-like request </li></ul><ul><ul><li>Inspired from Rails REST-like convention </li></ul></ul...
REST convention <ul><li>Handle REST-like request(Rails-like conv) </li></ul><ul><ul><li>GET: /movies => method=&quot;index...
REST sample(Action)
REST sample(jsp)
REST sample(xml)
Struts1 plugin <ul><li>Use legacy Struts1 codes at Struts2 </li></ul><ul><ul><li>Struts1 Action and ActionForm works witho...
Struts1 config sample Set Struts1Action to handle Set actual Action class
Struts2 Pros and Cons <ul><li>Pros </li></ul><ul><ul><li>Very extensible if you know the architecture </li></ul></ul><ul><...
Struts2 Pros and Cons <ul><li>Cons </li></ul><ul><ul><li>Confused with many programming model.Should be one unified model....
Struts2 resources <ul><li>Struts2 tutorials </li></ul><ul><ul><li>http://struts.apache.org/2.1.6/docs/tutorials.html </li>...
Thanks <ul><li>Enjoy with Struts2!! </li></ul>
Upcoming SlideShare
Loading in...5
×

Struts2 in a nutshell

4,166

Published on

Published in: Technology, Education
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
4,166
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
250
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Struts2 in a nutshell

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

    Clipping is a handy way to collect important slides you want to go back to later.

×