Migrating from Struts 1 to Struts 2

8,189 views
7,941 views

Published on

Struts has outgrown its reputation as a simple web framework and has become more of a brand. Because of this, two next generation frameworks are being developed within the project: Shale and Action 2.0. Action 2.0 is based on WebWork, and though its backing beans are similar to JSF, its architecture is much simpler, and easier to use.
Migrating to Struts Action 2.0 is more about unlearning Struts than it is about learning the "WebWork Way". Once you understand how simple WebWork is, you'll find that Struts Action 2.0 is a powerful framework that supports action-based navigation, page-based navigation, AOP/Interceptors, components and rich Ajax support.

Come to this session to see code comparisons and learn about migration strategies to use the kick-ass web framework of 2006.

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

No Downloads
Views
Total views
8,189
On SlideShare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
449
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide

Migrating from Struts 1 to Struts 2

  1. 1. Migrating from Struts 1 to Struts 2 Matt Raible, Virtuas Open Source Solutions mraible@virtuas.com © 2005-2006, Virtuas Open Source Solutions
  2. 2. Introductions Your experience with Java? Your experience with Web Frameworks? What do you hope to learn today? Open Source experience: Ant, Struts, WebWork, Spring, Hibernate, Eclipse, Tomcat? Favorite IDE? Favorite OS? Favorite Server?
  3. 3. Matt
  4. 4. Raible
  5. 5. Web Framework Experience Struts: used since June 2001 - same time 1.0 was released. Spring MVC: used since January 2004 - before 1.0 was released. WebWork: used since July 2004. Tapestry: used since July 2004. JSF: used since July 2004 - both Sun’s RI and MyFaces.
  6. 6. Agenda 1. Struts Overview 2. WebWork Overview 3. Reasons for Upgrading 4. Migrating from Struts 1 to Struts 2 5. Migrating from WebWork 2 to Struts 2 6. Pitfalls 7. Q and A
  7. 7. Struts 1.x Overview
  8. 8. Struts 1.x Pros: The “Standard” - lots of Struts jobs Lots of information and examples HTML tag library is one of the best Cons: ActionForms - they’re a pain Can’t unit test - StrutsTestCase only does integration Project has been rumored as “dead”
  9. 9. WebWork/Struts2
  10. 10. WebWork/Struts 2 Pros: Simple architecture - easy to extend Tag Library is easy to customize with FreeMarker or Velocity Interceptors are pretty slick Controller-based or page-based navigation Cons: Small Community Documentation is poorly organized
  11. 11. WebWork / Struts 2
  12. 12. WW/S2 Lifecycle
  13. 13. WebWork Action public class UserAction extends ActionSupport { private UserManager mgr; private User user; private String id; public void setUserManager(UserManager userManager) { this.mgr = userManager; } public void setId(String id) { this.id = id; } public User getUser() { return user; } public String edit() { // check for an add if (id != null) { user = mgr.getUser(id); } else { user = new User(); } return SUCCESS; }
  14. 14. WebWork Interceptors public class ValidationInterceptor extends AroundInterceptor { protected void after(ActionInvocation dispatcher, String result) throws Exception { } protected void before(ActionInvocation invocation) throws Exception { Action action = invocation.getAction(); String context = invocation.getProxy().getActionName(); final Map parameters = ActionContext.getContext().getParameters(); // don't validate on cancel, delete or GET if (ServletActionContext.getRequest().getMethod().equals(quot;GETquot;)) { log.debug(quot;Cancelling validation, detected GET requestquot;); } else if (parameters.containsKey(quot;cancelquot;) || parameters.containsKey(quot;deletequot;)) { log.debug(quot;Cancelling validation, detected clicking cancel or deletequot;); } else { ActionValidatorManager.validate(action, context); } } }
  15. 15. xwork.xml <!-- List of Users --> <action name=quot;usersquot; class=quot;userActionquot; method=quot;listquot;> <result name=quot;successquot;>userList.jsp</result> <result name=quot;inputquot;>userList.jsp</result> </action> <!-- Edit User --> <action name=quot;editUserquot; class=quot;userActionquot; method=quot;editquot;> <result name=quot;successquot;>userForm.jsp</result> <result name=quot;inputquot;>userList.jsp</result> </action> <!-- Save User --> <action name=quot;saveUserquot; class=quot;userActionquot;> <result name=quot;cancelquot; type=quot;redirectquot;>users.html</result> <result name=quot;deletequot; type=quot;redirectquot;>users.html</result> <result name=quot;inputquot;>userForm.jsp</result> <result name=quot;successquot; type=quot;chainquot;>saveUserWithValidation</result> </action>
  16. 16. WebWork JSP View <ww:form name=quot;userFormquot; action=quot;saveUserquot; method=quot;postquot; validate=quot;truequot;> <ww:hidden name=quot;user.idquot; value=quot;%{user.id}quot;/> <ww:textfield label=quot;%{getText('user.firstName')}quot; name=quot;user.firstNamequot; value=quot;%{user.firstName}quot; id=quot;user.firstNamequot;/> <ww:textfield label=quot;%{getText('user.lastName')}quot; name=quot;user.lastNamequot; value=quot;%{user.lastName}quot; required=quot;truequot;/> <ww:datepicker label=quot;%{getText('user.birthday')}quot; name=quot;user.birthdayquot; size=quot;11quot;/>
  17. 17. WebWork DatePicker <ww:form name=quot;userFormquot; action=quot;saveUserquot; method=quot;postquot; validate=quot;truequot;> <ww:hidden name=quot;user.idquot; value=quot;%{user.id}quot;/> <ww:textfield label=quot;%{getText('user.firstName')}quot; name=quot;user.firstNamequot; value=quot;%{user.firstName}quot; id=quot;user.firstNamequot;/> <ww:textfield label=quot;%{getText('user.lastName')}quot; name=quot;user.lastNamequot; value=quot;%{user.lastName}quot; required=quot;truequot;/> <ww:datepicker label=quot;%{getText('user.birthday')}quot; name=quot;user.birthdayquot; size=quot;11quot;/>
  18. 18. Page-based Navigation <%@ include file=quot;/common/taglibs.jspquot;%> <h2>Author Blogs</h2> <ww:action name=quot;authorsquot; id=quot;%{authors}quot; namespace=quot;defaultquot;/> <div class=quot;itemquot;> <ww:iterator value=quot;#authors.authorsquot; status=quot;indexquot;> <a href=quot;<ww:property value=quot;blog.feedUrlquot;/>quot;> <img src=quot;${ctxPath}/images/icons/xml.gifquot; alt=quot;XML Feedquot;/></a> <a href=quot;<ww:property value=quot;blog.urlquot;/>quot;><ww:property value=quot;namequot;/></a> <br /> </ww:iterator> </div>
  19. 19. OGNL <ww:form name=quot;userFormquot; action=quot;saveUserquot; method=quot;postquot; validate=quot;truequot;> <ww:hidden name=quot;user.idquot; value=quot;%{user.id}quot;/> <ww:textfield label=quot;%{getText('user.firstName')}quot; name=quot;user.firstNamequot; value=quot;%{user.firstName}quot; id=quot;user.firstNamequot;/> <ww:textfield label=quot;%{getText('user.lastName')}quot; name=quot;user.lastNamequot; value=quot;%{user.lastName}quot; required=quot;truequot;/> </tr> <th><label for=quot;user.birthdayquot;><fmt:message key=quot;user.birthdayquot;/>:</label></th> <td> <ww:set name=quot;birthdayquot; scope=quot;requestquot; value=quot;(user.birthday instanceof java.util.Date) ? user.birthday : ''quot;/> <input type=quot;textquot; size=quot;11quot; name=quot;user.birthdayquot; id=quot;user.birthdayquot; value=quot;<fmt:formatDate value=quot;${birthday}quot; pattern=quot;${datePattern}quot;/>quot;/> [${datePattern}] </td> <tr>
  20. 20. Struts 1 Struts 2
  21. 21. Comparison Struts 1 Struts 2 Action Action ActionForm Action or POJO ActionForward Result struts-config.xml struts.xml ActionServlet FilterDispatcher RequestProcessor Interceptors validation.xml Action-validation.xml
  22. 22. Features only in Struts 2 Page-based Navigation Built-in Ajax Support: DWR and Dojo Spring as default inversion of control container Changed from front-controller servlet to filter Much better client-side validation support QuickStart and Annotations JSF Support Built-in support for testing with StrutsTestCase
  23. 23. Struts Plugins
  24. 24. Run Struts 1.x Actions <action name=quot;editGangsterquot; class=quot;org.apache.struts2.s1.Struts1Actionquot;> <param name=quot;classNamequot;> com.mycompany.gangstas.EditGangsterAction </param> <result> gangsterForm.jsp </result> </action>
  25. 25. Equinox AppFuse Light - designed for quick apps with few requirements (i.e. prototypes) Includes 6 MVC implementations: JSF, Spring MVC, Struts 1, Struts 2, Tapestry and WebWork Includes 5 Persistence frameworks: Hibernate, iBATIS, JDO, OJB, Spring JDBC 50 combinations available! Located at http://equinox.dev.java.net
  26. 26. Sample Migration
  27. 27. WebWork 2 Struts 2
  28. 28. Comparison WebWork 2 Struts 2 com.opensymphony.xwork.* com.opensymphony.xwork2.* com.opensymphony.webwork.* org.apache.struts2.* xwork.xml struts.xml webwork.properties struts.properties <ww:*/> <s:*/>
  29. 29. Sample Migration
  30. 30. Pitfalls and Issues
  31. 31. Learn more from... Don Brown's Struts 2.0 presentation/article: http://us.apachecon.com/presentations/WE9/WE9- struts-2.0.ppt http://www.oreillynet.com/onjava/blog/2006/10/ my_history_of_struts_2.html InfoQ's Migrating to Struts 2 articles: http://infoq.com/articles/converting-struts-2-part1 http://infoq.com/news/struts2-migration-part2
  32. 32. Questions? Struts Project: http://struts.apache.org Community: http://struts.apache.org/mail.html Tutorials: http://cwiki.apache.org/confluence/display/ WW/Tutorials Kickstart your development with Equinox: https://equinox.dev.java.net

×