Transcription of Migrating from Struts 1 to Struts 2 - Software Summit
1 Migrating from Struts 1 to Struts 2 Matt RaibleVirtuas Open Source Software Summit : October 22 27, 2006 Copyright 2006, Virtuas Open Source SolutionsMatt Raible Migrating from Struts 1 to Struts 2 Page 1 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, To m c a t ? Favorite IDE? Favorite OS? Favorite Server?Colorado Software Summit : October 22 27, 2006 Copyright 2006, Virtuas Open Source SolutionsMatt Raible Migrating from Struts 1 to Struts 2 Page 2 Matt Colorado Software Summit : October 22 27, 2006 Copyright 2006, Virtuas Open Source SolutionsMatt Raible Migrating from Struts 1 to Struts 2 Page 3 RaibleColorado Software Summit : October 22 27, 2006 Copyright 2006, Virtuas Open Source SolutionsMatt Raible Migrating from Struts 1 to Struts 2 Page 4 Colorado Software Summit : October 22 27, 2006 Copyright 2006, Virtuas Open Source SolutionsMatt Raible Migrating from Struts 1 to Struts 2 Page 5 Colorado Software Summit : October 22 27, 2006 Copyright 2006, Virtuas Open Source SolutionsMatt Raible Migrating from Struts 1 to Struts 2 Page 6 Colorado Software Summit : October 22 27, 2006 Copyright 2006, Virtuas Open Source SolutionsMatt Raible Migrating from Struts 1 to Struts 2 Page 7 Colorado Software Summit .
2 October 22 27, 2006 Copyright 2006, Virtuas Open Source SolutionsMatt Raible Migrating from Struts 1 to Struts 2 Page 8 Colorado Software Summit : October 22 27, 2006 Copyright 2006, Virtuas Open Source SolutionsMatt Raible Migrating from Struts 1 to Struts 2 Page 9 Colorado Software Summit : October 22 27, 2006 Copyright 2006, Virtuas Open Source SolutionsMatt Raible Migrating from Struts 1 to Struts 2 Page 10 Colorado Software Summit : October 22 27, 2006 Copyright 2006, Virtuas Open Source SolutionsMatt Raible Migrating from Struts 1 to Struts 2 Page 11 Colorado Software Summit : October 22 27, 2006 Copyright 2006, Virtuas Open Source SolutionsMatt Raible Migrating from Struts 1 to Struts 2 Page 12 Colorado Software Summit : October 22 27, 2006 Copyright 2006, Virtuas Open Source SolutionsMatt Raible Migrating from Struts 1 to Struts 2 Page 13 Colorado Software Summit : October 22 27, 2006 Copyright 2006, Virtuas Open Source SolutionsMatt Raible Migrating from Struts 1 to Struts 2 Page 14 Colorado Software Summit : October 22 27, 2006 Copyright 2006, Virtuas Open Source SolutionsMatt Raible Migrating from Struts 1 to Struts 2 Page 15 Colorado Software Summit : October 22 27, 2006 Copyright 2006, Virtuas Open Source SolutionsMatt Raible Migrating from Struts 1 to Struts 2 Page 16 Colorado Software Summit : October 22 27, 2006 Copyright 2006, Virtuas Open Source SolutionsMatt Raible Migrating from Struts 1 to Struts 2 Page 17 Colorado Software Summit : October 22 27, 2006 Copyright 2006, Virtuas Open Source SolutionsMatt Raible Migrating from Struts 1 to Struts 2 Page 18 Web Framework Experience Struts : used since June 2001 - same time was released.
3 Spring MVC: used since January 2004 - before was released. WebWork: used since July 2004. Tapestry: used since July 2004. JSF: used since July 2004 - both Sun s RI and Software Summit : October 22 27, 2006 Copyright 2006, Virtuas Open Source SolutionsMatt Raible Migrating from Struts 1 to Struts 2 Page for from Struts 1 to Struts from WebWork 2 to Struts and AColorado Software Summit : October 22 27, 2006 Copyright 2006, Virtuas Open Source SolutionsMatt Raible Migrating from Struts 1 to Struts 2 Page 20 Struts OverviewColorado Software Summit : October 22 27, 2006 Copyright 2006, Virtuas Open Source SolutionsMatt Raible Migrating from Struts 1 to Struts 2 Page 21 Struts 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 Colorado Software Summit : October 22 27, 2006 Copyright 2006, Virtuas Open Source SolutionsMatt Raible Migrating from Struts 1 to Struts 2 Page 22 Struts Software Summit : October 22 27, 2006 Copyright 2006, Virtuas Open Source SolutionsMatt Raible Migrating from Struts 1 to Struts 2 Page 23 Struts LifecycleColorado Software Summit : October 22 27, 2006 Copyright 2006, Virtuas Open Source SolutionsMatt Raible Migrating from Struts 1 to Struts 2 Page 24 Struts Actionpublic class UserAction extends DispatchAction { private UserManager mgr = null; public void setUserManager(UserManager userManager) { = userManager; } public ActionForward delete(ActionMapping mapping, ActionForm form , HttpServletRequest request, HttpServletResponse response) throws Exception { DynaActionForm userForm = (DynaActionForm) form ; User user = (User) ("user"); ( (" ")).}}
4 ActionMessages messages = new ActionMessages(); ( , new ActionMessage(" ", ())); saveMessages( (), messages); return ("users"); }Colorado Software Summit : October 22 27, 2006 Copyright 2006, Virtuas Open Source SolutionsMatt Raible Migrating from Struts 1 to Struts 2 Page 25 Struts Actionpublic class UserAction extends DispatchAction { private UserManager mgr = null; public void setUserManager(UserManager userManager) { = userManager; } public ActionForward delete(ActionMapping mapping, ActionForm form , HttpServletRequest request, HttpServletResponse response) throws Exception { DynaActionForm userForm = (DynaActionForm) form ; User user = (User) ("user"); ( (" ")); ActionMessages messages = new ActionMessages(); ( , new ActionMessage(" ", ())); saveMessages( (), messages); return ("users"); }Colorado Software Summit : October 22 27, 2006 Copyright 2006, Virtuas Open Source SolutionsMatt Raible Migrating from Struts 1 to Struts 2 Page 26 Struts Actionpublic class UserAction extends DispatchAction { private UserManager mgr = null; public void setUserManager(UserManager userManager) { = userManager; } public ActionForward delete(ActionMapping mapping, ActionForm form , HttpServletRequest request, HttpServletResponse response) throws Exception { DynaActionForm userForm = (DynaActionForm) form ; User user = (User) ("user"); ( (" ")); ActionMessages messages = new ActionMessages(); ( , new ActionMessage(" ", ())); saveMessages( (), messages); return ("users").}}
5 }Colorado Software Summit : October 22 27, 2006 Copyright 2006, Virtuas Open Source SolutionsMatt Raible Migrating from Struts 1 to Struts 2 Page < form -bean name="userForm" type=" "> < form -property name="user" type=" "/> </ form -bean> <action path="/user" type=" " name="userForm" scope="request" parameter="method" validate="false"> <forward name="list" path=" "/> <forward name="edit" path=" "/> </action> <plug-in className=" "> <set-property property="contextConfigLocation" value="/WEB- "/> </plug-in>Colorado Software Summit : October 22 27, 2006 Copyright 2006, Virtuas Open Source SolutionsMatt Raible Migrating from Struts 1 to Struts 2 Page 28 Display Tag<display:table name="users" class="list" requestURI="" id="userList" export="true"> <display:column property="id" sort="true" href=" " paramId="id" paramProperty="id" titleKey=" "/> <display:column property="firstName" sort="true" titleKey=" "/> <display:column property="lastName" sort="true" titleKey=" "/> <display:column titleKey=" " sort="true" sortProperty="birthday"> <fmt:formatDate value="${ }" pattern="${datePattern}"/> </display:column> </display:table>Colorado Software Summit : October 22 27, 2006 Copyright 2006, Virtuas Open Source SolutionsMatt Raible Migrating from Struts 1 to Struts 2 Page 29 Struts : JSP View<html: form action="/user" focus=" " onsubmit="return validateUserForm(this)"> <input type="hidden" name="method" value="save"/> <html:hidden property=" "/> <table class="detail"> <tr> <th> <label for=" "> <fmt:message key=" "/>:</label> </th> <td> <html:text property=" " styleId=" "/> </td> </tr> <tr> <th> <label for=" "> <fmt:message key=" "/>:</label> </th> <td> <html:text property=" " styleId=" "/> <span class="fieldError"> <html:errors property=" "/> </span> </td> </tr> <th> <label for=" "> <fmt:message key=" "/>:</label> </th> <td> <c.
6 Set var="datePattern"> <fmt:message key=" "/> </c:set> <input type="text" size="11" name=" " id=" " value="<fmt:formatDate value="${ }" pattern="${datePattern}"/>"/> [${datePattern}] </td> <tr>Colorado Software Summit : October 22 27, 2006 Copyright 2006, Virtuas Open Source SolutionsMatt Raible Migrating from Struts 1 to Struts 2 Page 30 Struts : JSP View<html: form action="/user" focus=" " onsubmit="return validateUserForm(this)"> <input type="hidden" name="method" value="save"/> <html:hidden property=" "/> <table class="detail"> <tr> <th> <label for=" "> <fmt:message key=" "/>:</label> </th> <td> <html:text property=" " styleId=" "/> </td> </tr> <tr> <th> <label for=" "> <fmt:message key=" "/>:</label> </th> <td> <html:text property=" " styleId=" "/> <span class="fieldError"> <html:errors property=" "/> </span> </td> </tr> <th> <label for=" "> <fmt:message key=" "/>:</label> </th> <td> <c:set var="datePattern"> <fmt:message key=" "/> </c:set> <input type="text" size="11" name=" " id=" " value="<fmt:formatDate value="${ }" pattern="${datePattern}"/>"/> [${datePattern}] </td> <tr>Colorado Software Summit : October 22 27, 2006 Copyright 2006, Virtuas Open Source SolutionsMatt Raible Migrating from Struts 1 to Struts 2 Page 31 WebWork / Struts 2 Colorado Software Summit : October 22 27, 2006 Copyright 2006, Virtuas Open Source SolutionsMatt Raible Migrating from Struts 1 to Struts 2 Page 32 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 organizedColorado Software Summit .
7 October 22 27, 2006 Copyright 2006, Virtuas Open Source SolutionsMatt Raible Migrating from Struts 1 to Struts 2 Page 33 WebWork / Struts 2 Colorado Software Summit : October 22 27, 2006 Copyright 2006, Virtuas Open Source SolutionsMatt Raible Migrating from Struts 1 to Struts 2 Page 34 WebWork / Struts 2 LifecycleColorado Software Summit : October 22 27, 2006 Copyright 2006, Virtuas Open Source SolutionsMatt Raible Migrating from Struts 1 to Struts 2 Page 35 WebWork Actionpublic class UserAction extends ActionSupport { private UserManager mgr; private User user; private String id; public void setUserManager(UserManager userManager) { = userManager; } public void setId(String id) { = id; } public User getUser() { return user; } public String edit() { // check for an add if (id != null) { user = (id); } else { user = new User(); } return SUCCESS; }Colorado Software Summit : October 22 27, 2006 Copyright 2006, Virtuas Open Source SolutionsMatt Raible Migrating from Struts 1 to Struts 2 Page 36 WebWork Interceptorspublic class ValidationInterceptor extends AroundInterceptor { protected void after(ActionInvocation dispatcher, String result) throws Exception { } protected void before(ActionInvocation invocation) throws Exception { Action action = (); String context = ().}}}
8 GetActionName(); final Map parameters = ().getParameters(); // don't validate on cancel, delete or GET if ( ().getMethod().equals("GET")) { ("Cancelling validation, detected GET request"); } else if ( ("cancel") || ("delete")) { ("Cancelling validation, detected clicking cancel or delete"); } else { (action, context); } }}Colorado Software Summit : October 22 27, 2006 Copyright 2006, Virtuas Open Source SolutionsMatt Raible Migrating from Struts 1 to Struts 2 Page <!-- List of Users --> <action name="users" class="userAction" method="list"> <result name="success"> </result> <result name="input"> </result> </action> <!-- Edit User --> <action name="editUser" class="userAction" method="edit"> <result name="success"> </result> <result name="input"> </result> </action> <!-- Save User --> <action name="saveUser" class="userAction"> <result name="cancel" type="redirect"> </result> <result name="delete" type="redirect"> </result> <result name="input"> </result> <result name="success" type="chain">saveUserWithValidation</result> </action>Colorado Software Summit : October 22 27, 2006 Copyright 2006, Virtuas Open Source SolutionsMatt Raible Migrating from Struts 1 to Struts 2 Page 38 WebWork JSP View<ww: form name="userForm" action="saveUser" method="post" validate="true"> <ww:hidden name=" " value="%{ }"/> <ww:textfield label="%{getText(' ')}" name=" " value="%{ }" id=" "/> <ww:textfield label="%{getText(' ')}" name=" " value="%{ }" required="true"/> <ww:datepicker label="%{getText(' ')}" name=" " size="11"/>Colorado Software Summit .
9 October 22 27, 2006 Copyright 2006, Virtuas Open Source SolutionsMatt Raible Migrating from Struts 1 to Struts 2 Page 39 WebWork DatePicker<ww: form name="userForm" action="saveUser" method="post" validate="true"> <ww:hidden name=" " value="%{ }"/> <ww:textfield label="%{getText(' ')}" name=" " value="%{ }" id=" "/> <ww:textfield label="%{getText(' ')}" name=" " value="%{ }" required="true"/> <ww:datepicker label="%{getText(' ')}" name=" " size="11"/>Colorado Software Summit : October 22 27, 2006 Copyright 2006, Virtuas Open Source SolutionsMatt Raible Migrating from Struts 1 to Struts 2 Page 40 Page-based Navigation<%@ include file="/ "%> <h2>Author Blogs</h2> <ww:action name="authors" id="%{authors}" namespace="default"/> <div class="