Presentation is loading. Please wait.

Presentation is loading. Please wait.

Copyright AlphaCSP Israel 2008 – Web Framework Playoff Seminar 1 Struts2 Reinventing Struts1 Wheel Ori Dar Consultant and Architect, AlphaCSP.

Similar presentations


Presentation on theme: "Copyright AlphaCSP Israel 2008 – Web Framework Playoff Seminar 1 Struts2 Reinventing Struts1 Wheel Ori Dar Consultant and Architect, AlphaCSP."— Presentation transcript:

1 Copyright AlphaCSP Israel 2008 – Web Framework Playoff Seminar 1 Struts2 Reinventing Struts1 Wheel Ori Dar Consultant and Architect, AlphaCSP

2 Copyright AlphaCSP Israel 2008 – Web Framework Playoff Seminar 2 Agenda  Introduction  Background  Framework features review  Configuration  View technology  Page flow  Form binding  Table sorting  Pagination  Validation  AJAX  Error handling  I18n support  Documentation  Summary

3 Copyright AlphaCSP Israel 2008 – Web Framework Playoff Seminar 3 Introduction::In a nutshell (1) Action Based MVC Framework –FilterDispatcher : controller –Action : POJO model –Result : view Comparable to –Struts1 –Spring MVC –Stripes

4 Copyright AlphaCSP Israel 2008 – Web Framework Playoff Seminar 4 Introduction::In a nutshell (2) First released in Oct. 2006 –Merger of Struts1 and WebWork2 –Uses WebWork2 architecture Modern and clean architecture Xml or annotations –Actions –Validators –Datatype convertors

5 Copyright AlphaCSP Israel 2008 – Web Framework Playoff Seminar 5 Introduction::In a nutshell (3) Extensible via plugins –Other frameworks integration –Embedding other application modules Intuitive, fast learning curve Other core components –Interceptors: controlling action pre and post processing –ValueStack: central storage for request model data

6 Copyright AlphaCSP Israel 2008 – Web Framework Playoff Seminar 6 Introduction::HelloWorld action2 org.apache.struts2.dispatcher.FilterDispatcher action2 /* Controller: FilterDispatcher web.xml

7 Copyright AlphaCSP Israel 2008 – Web Framework Playoff Seminar 7 Introduction::HelloWorld, Model public class HelloWorld { private String message="Hello World. Time is: "; public String execute () { message += new Date(); return "success"; } public String getMessage() { return message; } action method returns a result code We don’t have to extend Action Boss … and no request, response in execute() Boss

8 Copyright AlphaCSP Israel 2008 – Web Framework Playoff Seminar 8 Introduction::HelloWorld, View View: HelloWorld.jsp –Utilizes a single struts taglib –The taglib is common for JSP, Velocity and FreeMarker Prints action’s message property. Unlike struts1, action is a POJO, and acts as a model

9 Copyright AlphaCSP Israel 2008 – Web Framework Playoff Seminar 9 Introduction::HelloWorld /pages/HelloWorld.jsp struts.xml: –Actions mapping –Results mapping links action to view http://host:port/app/hello.action Use action name in URL invocation

10 Copyright AlphaCSP Israel 2008 – Web Framework Playoff Seminar 10 Agenda  Introduction  Background  Framework features review  Configuration  View technology  Page flow  Form binding  Table sorting  Pagination  Validation  AJAX  Error handling  I18n support  Documentation  Summary

11 Copyright AlphaCSP Israel 2008 – Web Framework Playoff Seminar 11 Background::S2 Vs. S1 Struts1Struts2 Extends ActionActionPOJO (with execute) ControllerAction RoleModel DependantServlet APIDecoupled Single instanceThreadingInstance per request Action FormForm bindingAction JavaBean properties JSP mechanismsView bindingValue Stack JSTL ELELOGNL Action FormValidationxml or annotations SharedLifecycleIndependent via interceptors VerboseConfigurationWildcards, annotations

12 Copyright AlphaCSP Israel 2008 – Web Framework Playoff Seminar 12 Background::Interceptors Actions pre & post processing mechanism Like Spring AOP, EJB3 interceptors Interceptors are packed in stacks Custom interceptors and stacks can be added No equivalent in Struts1 /pages/phoneBook.jsp struts.xml

13 Copyright AlphaCSP Israel 2008 – Web Framework Playoff Seminar 13 Background::Interceptors Excerpt from struts-default.xml interceptor interceptor stack contains other interceptors the default stack for actions in package struts-default.xml

14 Copyright AlphaCSP Israel 2008 – Web Framework Playoff Seminar 14 Background::ValueStack The ValueStack is a storage for request domain model Serves as context per request “Glues” model and view Accessed by interceptors & views Stores action properties Also stores message bundles

15 Copyright AlphaCSP Israel 2008 – Web Framework Playoff Seminar 15 Background::OGNL ValueStack is referenced and manipulated by OGNL OGNL is also used instead of JSTL <s:textfield name="contact.email"/> <s:text name="email"/> phoneBook.jsp retrieves email property of actions’ contact property from stack using OGNL retrieves localized message using email as key from stack using OGNL

16 Copyright AlphaCSP Israel 2008 – Web Framework Playoff Seminar 16 Background::Other features Tag Library –Form component tags –Ajax UI component tags –Control tags: iterator, if/else… –Data tags: manipulate the ValueStack Datatype conversion framework Themes: templates for customizing components markup

17 Copyright AlphaCSP Israel 2008 – Web Framework Playoff Seminar 17 Background:: Theme example Xhtml theme generates table and validation feedback. No themes in Struts1 login.jsp

18 Copyright AlphaCSP Israel 2008 – Web Framework Playoff Seminar 18 Background::Other features Reasonable defaults: struts-default –Result types –Interceptor stacks Actions extending ActionSupport –facilitates validation –facilitates localization Spring integration –Dependency Injection interceptor –Full Spring lifecycle management

19 Copyright AlphaCSP Israel 2008 – Web Framework Playoff Seminar 19 Agenda  Introduction  Background  Framework features review  Configuration  View technology  Page flow  Form binding  Table sorting  Pagination  Validation  AJAX  Error handling  I18n support  Documentation  Summary

20 Copyright AlphaCSP Israel 2008 – Web Framework Playoff Seminar 20 Features::Action Configuration /WEB-INF/list.jsp result type the view technology (default value: “dispatcher” for rendering JSP) result name action method should return a matching result code string (default value: “success”) action method within action class (default value: “execute”) action class action name matched by a URL

21 Copyright AlphaCSP Israel 2008 – Web Framework Playoff Seminar 21 Features:: Action Configuration /WEB-INF/list{1}s.jsp Wildcards can be used for mapping Mandates naming convention Makes XML much less verbose listEmployees.action is mapped to Employee class and listEmployees.jsp listDepartments.action is mapped to Department class and listDepartmrnts.jsp …

22 Copyright AlphaCSP Israel 2008 – Web Framework Playoff Seminar 22 Features::Annotation Config. @Result(name= "list", value="/WEB-INF/list.jsp") public class Employee extends ActionSupport { public String listEmployees() { // business logic goes here return "list" ; } Annotation based configuration –Can make XML utterly redundant –Struts scans packages for action classes –Results are expressed via @Result “list” result code is mapped to JSP. By extending ActionSupport, Employee is mapped as action by the package scanning mechanism

23 Copyright AlphaCSP Israel 2008 – Web Framework Playoff Seminar 23 Features::View technology Result types –JSP : dispatcher –Velocity –FreeMarker –XSLT –Stream : PDF, MS Word etc. Plugins –JasperReports –JFreeChart –JSON

24 Copyright AlphaCSP Israel 2008 – Web Framework Playoff Seminar 24 Features::Page flow 1 3 4 5 6 7 8 2

25 Copyright AlphaCSP Israel 2008 – Web Framework Playoff Seminar 25 Features::Page flow 1.User Sends HTTP request 2.FilterDispatcher determines appropriate action 3.Interceptors are applied 4.Action parameters are set (via params interceptor) 5.Action is being executed 6.The Result (view) renders the output 7.Optionaly: the view retrieves data from action 8.The result is displayed to the user

26 Copyright AlphaCSP Israel 2008 – Web Framework Playoff Seminar 26 Features::Form binding <s:form action="login" validate="true" namespace="/"> <s:textfield cssClass="loginField" key= "username" /> <s:submit key="login" cssClass="button" align="center"/> </s:form> login.jsp public class Login { private String username; public void setUsername(String username) { this.username = username; } Login.java Form field parameters are injected into setter methods by the params interceptor

27 Copyright AlphaCSP Israel 2008 – Web Framework Playoff Seminar 27 Features::Table sorting 1.Using Table Tags plugin –Undocumented –Looks inactive –Lack of evidence 2.Using Dojo + JSON –Too complex 3.YUI DataTable –Buggy, beta 4.Display tag library

28 Copyright AlphaCSP Israel 2008 – Web Framework Playoff Seminar 28 Features::Table sorting From demo application phoneBook.jsp

29 Copyright AlphaCSP Israel 2008 – Web Framework Playoff Seminar 29 Features::Pagination Used the display tag library internal pagination support –Supports internal and external pagination –Forces you to use HTTP GET –Adds pagination parameter to request

30 Copyright AlphaCSP Israel 2008 – Web Framework Playoff Seminar 30 Features::Validation Uses 12 out-of-box field validators Uses expression validators for complex validations Validators can be declared using xml Validators can be declared using annotations –Field validators for setter methods –Expression validator for action methods

31 Copyright AlphaCSP Israel 2008 – Web Framework Playoff Seminar 31 Features::Validation @EmailValidator( fieldName = "email", key="wrongEmailFormat", message="Wrong Email Format") public void setEmail(String email) { this.email = email; } PhoneBook.java Wrong Email Format PhoneBook_validation.xml Annotation vs. xml example (No annotated version for Struts1)

32 Copyright AlphaCSP Israel 2008 – Web Framework Playoff Seminar 32 Features::Client side validation Automatic creation of client side validation –No client side for expression validators –Apply a theme for generating error feedback markup login.jsp

33 Copyright AlphaCSP Israel 2008 – Web Framework Playoff Seminar 33 Features::Ajax Dojo is embedded for Ajax tags Dojo head tag enables JS debugging Ajax requests are handled by actions DWR integration is enabled via plugin YUI integration is enabled via plugin Ajax file upload via plugin Ajax behaviour for component tags

34 Copyright AlphaCSP Israel 2008 – Web Framework Playoff Seminar 34 Features::Ajax autocompleter View Action <s:url id="acUrl" action="getDepts"/> <s:autocompleter name="dept" href="%{acUrl}" cssClass="acSearchField"/> private List deptList; public String execute() { deptList = service.findAllDepartments(); return ActionSupport.SUCCESS; } public List getDeptList() { return deptList; } phoneBook.jsp PhoneBook.java <action name="getDepts" class="DeptsAutoComplete"> deptList struts-default.xml

35 Copyright AlphaCSP Israel 2008 – Web Framework Playoff Seminar 35 Features::Error handling Validation error feedback tags –actionerror –actionmessage –fielderror: per form field Two framework supporting interfaces –Validatable: for programmatic validation –ValidationAware: for UI feedback

36 Copyright AlphaCSP Israel 2008 – Web Framework Playoff Seminar 36 Features::Error handling /pages/systemError.jsp Exception interceptor –maps exceptions to result error pages –should be first in interceptor stack struts.xml

37 Copyright AlphaCSP Israel 2008 – Web Framework Playoff Seminar 37 Features::I18N support Built on top of Java I18N support Bundles can be defined in: –action.properties –package.properties –global resource properties Bundles pushed to ValueStack Validation errors can be localized Locale interceptor to switch locale

38 Copyright AlphaCSP Israel 2008 – Web Framework Playoff Seminar 38 Features::Documentation Apache Struts 2 Documentation WIKI Struts Showcase Plugins

39 Copyright AlphaCSP Israel 2008 – Web Framework Playoff Seminar 39 Agenda  Introduction  Background  Framework features review  Configuration  View technology  Page flow  Form binding  Table sorting  Pagination  Validation  AJAX  Error handling  I18n support  Documentation  Summary

40 Copyright AlphaCSP Israel 2008 – Web Framework Playoff Seminar 40 Summary::Pros Extensible plugin framework Modular and clean architecture Annotation or xml configuration Customized lifecycle via interceptors Neat form & view binding Easy client side validation Markup generation with themes

41 Copyright AlphaCSP Israel 2008 – Web Framework Playoff Seminar 41 Summary::Pros Fast learning curve Intuitive Restful URL action mapper Easy to test (POJOs) using plugins DI via lightweight containers Decoration with tiles, sitemash

42 Copyright AlphaCSP Israel 2008 – Web Framework Playoff Seminar 42 Summary::Cons Unorganized documentation Lack of API documentation No clear roadmap ActionSupport dependency Lack of @Action Lack of @Interceptor Cases when fallback to Servlet API is needed Weakly typed sessions

43 Copyright AlphaCSP Israel 2008 – Web Framework Playoff Seminar 43 Summary::When to use Use when –Migrating from legacy framework –Natural around HTTP request response paradigm –Suitable for streaming –POC, prototype, RAD Don’t use when –Heavily utilized RIA is needed –Your developers have Swing orientation

44 Copyright AlphaCSP Israel 2008 – Web Framework Playoff Seminar 44 Thank You !


Download ppt "Copyright AlphaCSP Israel 2008 – Web Framework Playoff Seminar 1 Struts2 Reinventing Struts1 Wheel Ori Dar Consultant and Architect, AlphaCSP."

Similar presentations


Ads by Google