Migrating from Struts 1 to Struts 2 Matt Raible Virtuas Open Source Solutions [email protected]
Total Page:16
File Type:pdf, Size:1020Kb
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 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? 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 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. 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 <form-bean name="userForm" type="org.apache.struts.validator.DynaValidatorForm"> <form-property name="user" type="org.appfuse.model.User"/> </form-bean> <action path="/user" type="org.springframework.web.struts.DelegatingActionProxy" name="userForm" scope="request" parameter="method" validate="false"> <forward name="list" path="/userList.jsp"/> <forward name="edit" path="/userForm.jsp"/> </action> <plug-in className="org.springframework.web.struts.ContextLoaderPlugIn"> <set-property property="contextConfigLocation" value="/WEB-INF/action-servlet.xml"/> </plug-in> 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 <display:table name="users" class="list" requestURI="" id="userList" export="true"> <display:column property="id" sort="true" href="editUser.html" paramId="id" paramProperty="id" titleKey="user.id"/> <display:column property="firstName" sort="true" titleKey="user.firstName"/> <display:column property="lastName" sort="true" titleKey="user.lastName"/> <display:column titleKey="user.birthday" sort="true" sortProperty="birthday"> <fmt:formatDate value="${userList.birthday}" pattern="${datePattern}"/> </display:column> </display:table> 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 <html:form action="/user" focus="user.firstName" onsubmit="return validateUserForm(this)"> <input type="hidden" name="method" value="save"/> <html:hidden property="user.id"/> <table class="detail"> <tr> <th><label for="user.firstName"><fmt:message key="user.firstName"/>:</label></th> <td><html:text property="user.firstName" styleId="user.firstName"/></td> </tr> <tr> <th><label for="user.lastName"><fmt:message key="user.lastName"/>:</label></th> <td> <html:text property="user.lastName" styleId="user.lastName"/> <span class="fieldError"><html:errors property="user.lastName"/></span> </td> </tr> <th><label for="user.birthday"><fmt:message key="user.birthday"/>:</label></th> <td> <c:set var="datePattern"><fmt:message key="date.format"/></c:set>