Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Virtuas Open Source Solutions

Migrating from Struts 1 to Struts 2 Matt Raible Virtuas Open Source Solutions [email protected]

Matt Raible — Migrating from Struts 1 to Struts 2 Page 1 Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Virtuas Open Source Solutions Introductions  Your experience with ?  Your experience with Web Frameworks?  What do you hope to learn today?  Open Source experience: Ant, Struts, WebWork, Spring, , Eclipse, Tomcat?  Favorite IDE? Favorite OS? Favorite Server?

Matt Raible — Migrating from Struts 1 to Struts 2 Page 2 Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Virtuas Open Source Solutions Matt

Matt Raible — Migrating from Struts 1 to Struts 2 Page 3 Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Virtuas Open Source Solutions Raible

Matt Raible — Migrating from Struts 1 to Struts 2 Page 4 Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Virtuas Open Source Solutions

Matt Raible — Migrating from Struts 1 to Struts 2 Page 5 Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Virtuas Open Source Solutions

Matt Raible — Migrating from Struts 1 to Struts 2 Page 6 Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Virtuas Open Source Solutions

Matt Raible — Migrating from Struts 1 to Struts 2 Page 7 Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Virtuas Open Source Solutions

Matt Raible — Migrating from Struts 1 to Struts 2 Page 8 Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Virtuas Open Source Solutions

Matt Raible — Migrating from Struts 1 to Struts 2 Page 9 Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Virtuas Open Source Solutions

Matt Raible — Migrating from Struts 1 to Struts 2 Page 10 Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Virtuas Open Source Solutions

Matt Raible — Migrating from Struts 1 to Struts 2 Page 11 Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Virtuas Open Source Solutions

Matt Raible — Migrating from Struts 1 to Struts 2 Page 12 Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Virtuas Open Source Solutions

Matt Raible — Migrating from Struts 1 to Struts 2 Page 13 Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Virtuas Open Source Solutions

Matt Raible — Migrating from Struts 1 to Struts 2 Page 14 Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Virtuas Open Source Solutions

Matt Raible — Migrating from Struts 1 to Struts 2 Page 15 Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Virtuas Open Source Solutions

Matt Raible — Migrating from Struts 1 to Struts 2 Page 16 Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Virtuas Open Source Solutions

Matt Raible — Migrating from Struts 1 to Struts 2 Page 17 Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Virtuas Open Source Solutions

Matt Raible — Migrating from Struts 1 to Struts 2 Page 18 Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Virtuas Open Source Solutions 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.

Matt Raible — Migrating from Struts 1 to Struts 2 Page 19 Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Virtuas Open Source Solutions 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

Matt Raible — Migrating from Struts 1 to Struts 2 Page 20 Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Virtuas Open Source Solutions Struts 1.x Overview

Matt Raible — Migrating from Struts 1 to Struts 2 Page 21 Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Virtuas Open Source Solutions 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”

Matt Raible — Migrating from Struts 1 to Struts 2 Page 22 Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Virtuas Open Source Solutions Struts 1.x

Matt Raible — Migrating from Struts 1 to Struts 2 Page 23 Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Virtuas Open Source Solutions Struts 1.x Lifecycle

Matt Raible — Migrating from Struts 1 to Struts 2 Page 24 Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Virtuas Open Source Solutions Struts Action

public class UserAction extends DispatchAction { private UserManager mgr = null;

public void setUserManager(UserManager userManager) { this.mgr = userManager; }

public ActionForward delete(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { DynaActionForm userForm = (DynaActionForm) form; User user = (User) userForm.get("user");

mgr.removeUser(request.getParameter("user.id"));

ActionMessages messages = new ActionMessages(); messages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("user.deleted", user.getFullName()));

saveMessages(request.getSession(), messages);

return mapping.findForward("users"); }

Matt Raible — Migrating from Struts 1 to Struts 2 Page 25 Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Virtuas Open Source Solutions Struts Action

public class UserAction extends DispatchAction { private UserManager mgr = null;

public void setUserManager(UserManager userManager) { this.mgr = userManager; }

public ActionForward delete(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { DynaActionForm userForm = (DynaActionForm) form; User user = (User) userForm.get("user");

mgr.removeUser(request.getParameter("user.id"));

ActionMessages messages = new ActionMessages(); messages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("user.deleted", user.getFullName()));

saveMessages(request.getSession(), messages);

return mapping.findForward("users"); }

Matt Raible — Migrating from Struts 1 to Struts 2 Page 26 Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Virtuas Open Source Solutions Struts Action

public class UserAction extends DispatchAction { private UserManager mgr = null;

public void setUserManager(UserManager userManager) { this.mgr = userManager; }

public ActionForward delete(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { DynaActionForm userForm = (DynaActionForm) form; User user = (User) userForm.get("user");

mgr.removeUser(request.getParameter("user.id"));

ActionMessages messages = new ActionMessages(); messages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("user.deleted", user.getFullName()));

saveMessages(request.getSession(), messages);

return mapping.findForward("users"); }

Matt Raible — Migrating from Struts 1 to Struts 2 Page 27 Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Virtuas Open Source Solutions struts-config.xml

Matt Raible — Migrating from Struts 1 to Struts 2 Page 28 Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Virtuas Open Source Solutions Display Tag

Matt Raible — Migrating from Struts 1 to Struts 2 Page 29 Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Virtuas Open Source Solutions Struts 1.x: JSP View

Matt Raible — Migrating from Struts 1 to Struts 2 Page 30 Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Virtuas Open Source Solutions Struts 1.x: JSP View

"/> [${datePattern}]

Matt Raible — Migrating from Struts 1 to Struts 2 Page 31 Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Virtuas Open Source Solutions WebWork / Struts 2

Matt Raible — Migrating from Struts 1 to Struts 2 Page 32 Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Virtuas Open Source Solutions 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

Matt Raible — Migrating from Struts 1 to Struts 2 Page 33 Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Virtuas Open Source Solutions WebWork / Struts 2

Matt Raible — Migrating from Struts 1 to Struts 2 Page 34 Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Virtuas Open Source Solutions WebWork / Struts 2 Lifecycle

Matt Raible — Migrating from Struts 1 to Struts 2 Page 35 Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Virtuas Open Source Solutions 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; }

Matt Raible — Migrating from Struts 1 to Struts 2 Page 36 Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Virtuas Open Source Solutions 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("GET")) { log.debug("Cancelling validation, detected GET request"); } else if (parameters.containsKey("cancel") || parameters.containsKey("delete")) { log.debug("Cancelling validation, detected clicking cancel or delete"); } else { ActionValidatorManager.validate(action, context); } } }

Matt Raible — Migrating from Struts 1 to Struts 2 Page 37 Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Virtuas Open Source Solutions xwork.xml

userList.jsp userList.jsp

userForm.jsp userList.jsp

users.html users.html userForm.jsp saveUserWithValidation

Matt Raible — Migrating from Struts 1 to Struts 2 Page 38 Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Virtuas Open Source Solutions WebWork JSP View

Matt Raible — Migrating from Struts 1 to Struts 2 Page 39 Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Virtuas Open Source Solutions WebWork DatePicker

Matt Raible — Migrating from Struts 1 to Struts 2 Page 40 Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Virtuas Open Source Solutions Page-based Navigation

<%@ include file="/common/taglibs.jsp"%>

Author Blogs

Matt Raible — Migrating from Struts 1 to Struts 2 Page 41 Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Virtuas Open Source Solutions OGNL

Matt Raible — Migrating from Struts 1 to Struts 2 Page 42 Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Virtuas Open Source Solutions

Struts 1 Struts 2

Matt Raible — Migrating from Struts 1 to Struts 2 Page 43 Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Virtuas Open Source Solutions Comparison

Action Action

ActionForm Action or POJO

ActionForward Result

struts-config.xml struts.xml

ActionServlet FilterDispatcher

RequestProcessor Interceptors

validation.xml Action-validation.xml

Matt Raible — Migrating from Struts 1 to Struts 2 Page 44 Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Virtuas Open Source Solutions 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

Matt Raible — Migrating from Struts 1 to Struts 2 Page 45 Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Virtuas Open Source Solutions Struts Plugins

Matt Raible — Migrating from Struts 1 to Struts 2 Page 46 Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Virtuas Open Source Solutions Run Struts 1.x Actions

com.mycompany.gangstas.EditGangsterAction gangsterForm.jsp

Matt Raible — Migrating from Struts 1 to Struts 2 Page 47 Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Virtuas Open Source Solutions

Showcase Demo

Matt Raible — Migrating from Struts 1 to Struts 2 Page 48 Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Virtuas Open Source Solutions 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

Matt Raible — Migrating from Struts 1 to Struts 2 Page 49 Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Virtuas Open Source Solutions Sample Migration

Matt Raible — Migrating from Struts 1 to Struts 2 Page 50 Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Virtuas Open Source Solutions

WebWork 2 Struts 2

Matt Raible — Migrating from Struts 1 to Struts 2 Page 51 Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Virtuas Open Source Solutions Comparison

com.opensymphony.xwork.* com.opensymphony.xwork2.*

com.opensymphony.webwork.* org.apache.struts2.*

xwork.xml struts.xml

webwork.properties struts.properties

Matt Raible — Migrating from Struts 1 to Struts 2 Page 52 Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Virtuas Open Source Solutions Sample Migration

Matt Raible — Migrating from Struts 1 to Struts 2 Page 53 Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Virtuas Open Source Solutions

Pitfalls and Issues

Matt Raible — Migrating from Struts 1 to Struts 2 Page 54 Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Virtuas Open Source Solutions 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

Matt Raible — Migrating from Struts 1 to Struts 2 Page 55 Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Virtuas Open Source Solutions 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

Matt Raible — Migrating from Struts 1 to Struts 2 Page 56 Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Virtuas Open Source Solutions

Don't forget to fill out your evaluation forms!

Matt Raible — Migrating from Struts 1 to Struts 2 Page 57

"/> [${datePattern}]
"/> [${datePattern}]