Presentation is loading. Please wait.

Presentation is loading. Please wait.

Struts:The good, the bad, the ugly A detailed evaluation of Struts 1.1 and Enterprise Web Applications. By Paul Smith Ancept, Inc. (www.ancept.com) www.ancept.com.

Similar presentations


Presentation on theme: "Struts:The good, the bad, the ugly A detailed evaluation of Struts 1.1 and Enterprise Web Applications. By Paul Smith Ancept, Inc. (www.ancept.com) www.ancept.com."— Presentation transcript:

1 Struts:The good, the bad, the ugly A detailed evaluation of Struts 1.1 and Enterprise Web Applications. By Paul Smith Ancept, Inc. (www.ancept.com) www.ancept.com psmith@ancept.com

2 Presentation Overview Themes Themes Struts Overview (Basic Features) Struts Overview (Basic Features) Development Challenges in 1.0 Development Challenges in 1.0 –Problem Description –Solution in 1.1 –Example Architectural Challenges Struts Applications Architectural Challenges Struts Applications –Problem Description –Possible Solution Q & A Q & A

3 Themes 1. Struts 1.1 provides small to medium sized applications all the functionality they should need “out of the box” 2. A large project team should be aware of limitations of Struts 1.1 which may require customized solutions 3. Large architecture teams may need a custom implementation of Struts 1.1 to enterprise class architectures

4 Supporting the Themes To illustrate the themes: To illustrate the themes: –Section 1 describes Struts basic features –Section 2 describes the new features of Struts 1.1 and why they exist –Section 3 describes architectural challenges in large projects and possible solutions

5 Section 1-Basic Features Why Use A Framework Why Use A Framework Struts Overview Struts Overview

6 Why Use the Struts Framework A pre-built framework is better than roll- your-own A pre-built framework is better than roll- your-own Increased productivity in JSP production Increased productivity in JSP production Cleaner design Cleaner design More time on business logic More time on business logic

7 Easier than building your own Probably very similar to what you would end up building anyway Probably very similar to what you would end up building anyway Strong industry support instead of your own custom framework Strong industry support instead of your own custom framework The Struts framework is configurable so it will probably work for your project without building your own The Struts framework is configurable so it will probably work for your project without building your own Customizing open-source is simpler than building from scratch Customizing open-source is simpler than building from scratch

8 JSP Productivity Writing pages in Struts is roughly 40-60% faster than without struts because you use tags NOT java code Writing pages in Struts is roughly 40-60% faster than without struts because you use tags NOT java code The custom tags, form object, messages architecture makes data display easy (for medium sized apps) The custom tags, form object, messages architecture makes data display easy (for medium sized apps) Tiles development helps manage page complexity Tiles development helps manage page complexity

9 Cleaner Design Struts applications are easier to develop and maintain because the design concepts are elegant Struts applications are easier to develop and maintain because the design concepts are elegant –Non-technical staff better understands layout and design –Since code is targeted at specific business problems, the team communicates better –Design is easier to expand for very large application architectures

10 More Time on Business Logic Less coding of web infrastructure means more time for business logic: Less coding of web infrastructure means more time for business logic: –Page flow design –Action execution synchronization –Page model objects –Validation –Page templates Focus on re-usable custom frameworks to use with Struts (J2EE integration, Web services) Focus on re-usable custom frameworks to use with Struts (J2EE integration, Web services)

11 Struts Overview Struts Description Struts Description Common Infrastructure Tasks Common Infrastructure Tasks Struts Solutions to Infrastructure Tasks Struts Solutions to Infrastructure Tasks How does Struts works How does Struts works The Struts Configuration File The Struts Configuration File Where does it fit in J2EE applications Where does it fit in J2EE applications Case Study Case Study

12 Struts Description Struts is a Web Application Framework (custom tag libraries, servlet, xml control files) which increase the productivity of web developers Struts is a Web Application Framework (custom tag libraries, servlet, xml control files) which increase the productivity of web developers Struts was invented by Craig McClanahan in 2000 Struts was invented by Craig McClanahan in 2000 Struts solves common problems with web application development by abstracting common infrastructure tasks Struts solves common problems with web application development by abstracting common infrastructure tasks

13 Common Infrastructure Tasks Page flow design and control Page flow design and control Code execution framework (Command Pattern) Code execution framework (Command Pattern) Automated data objects (MVC Pattern) Automated data objects (MVC Pattern) Easy to use custom tags for display Easy to use custom tags for display Validation Validation Page templates & layout Page templates & layout Re-usable text messages Re-usable text messages Much more… Much more…

14 Struts Solutions to Infrastructure Forwards: Page flow design and control Forwards: Page flow design and control Actions: Code execution framework (Command Pattern) Actions: Code execution framework (Command Pattern) Forms: Automated data objects (MVC Pattern) Forms: Automated data objects (MVC Pattern) Struts Tags: Easy to use custom tags for display Struts Tags: Easy to use custom tags for display Validator: Validation Validator: Validation Tiles: Page templates & layout Tiles: Page templates & layout Message Resources: Handles re-usable text messages in multiple languages Message Resources: Handles re-usable text messages in multiple languages

15 How Struts Works

16 Struts Configuration Struts is controlled by a Configuration file Struts is controlled by a Configuration file Struts Configuration contains: Struts Configuration contains: –Form Definitions: Data objects for JSP pages –Forward Definitions: Abstract pages for page flow control –Action Definitions: maps requests to Command class –Message Resources: Storage of common text messages –Plug-In Definitions: Startup classes used for initializing custom functionality

17 Where does Struts fit into J2EE

18 Case Study Department of Transportation created an aircraft registration application for internal staff (J2EE with a 2 nd Generation Framework) Department of Transportation created an aircraft registration application for internal staff (J2EE with a 2 nd Generation Framework) 100 JSPs 100 JSPs 13.5 k lines of code in the web tier (no business logic). 13.5 k lines of code in the web tier (no business logic). 20,000 lines of code for business logic. 20,000 lines of code for business logic. 8 months of development with a team of 5 8 months of development with a team of 5

19 Section 1 Conclusion Struts base feature-set include significant functionality for small to medium applications Struts base feature-set include significant functionality for small to medium applications Small applications should not need to customize Struts significantly: Small applications should not need to customize Struts significantly: –Form objects and Action classes provide MVC design –Action classes house functional code –Forwards abstract page flow control –Form objects and Struts tags provide page display –Tiles provides flexible page layout –Validator provides flexible form/page level validation –Message Resources provide re-usable text

20 Development Challenges in 1.0 Large Numbers of Form Classes Large Numbers of Form Classes Large teams and Resources Large teams and Resources Handling Errors More Flexibly Handling Errors More Flexibly Problems with Page Layout Problems with Page Layout Validating Pages and Forms Validating Pages and Forms Initializing Custom Functionality Initializing Custom Functionality

21 Lots of Form Classes In Struts 1.0 developers had to write a custom class for every form (generally 1 per JSP form) In Struts 1.0 developers had to write a custom class for every form (generally 1 per JSP form) Form classes are generally very simple Form classes are generally very simple Task assigned to junior programmers (drudge work) Task assigned to junior programmers (drudge work) Form classes sometimes not consistent Form classes sometimes not consistent

22 1.1 Solution Dynamic Form Objects Declare Forms in the struts-config.xml file Declare Forms in the struts-config.xml file Form objects are created when the action executes and stored in the session or request scope Form objects are created when the action executes and stored in the session or request scope Form fields are set and retrieved using the PropertyUtils.setSimpleProperty(…) method or treated as HashMap Form fields are set and retrieved using the PropertyUtils.setSimpleProperty(…) method or treated as HashMap

23 Dynamic Form Example Declaration<form> </form> Java Use Public void execute(mapping, form, …){ String testProp = (String)PropertyUtils.getSimpleProperty(form, “testProp”); PropertyUtils.setSimpleProperty(form, “testProp”, value); }

24 Large Teams and Resources It has been difficult to co-ordinate the struts-config file and other resources with large teams and projects It has been difficult to co-ordinate the struts-config file and other resources with large teams and projects Resources declared in config file might need to be different for portions of the application (shippingForm) Resources declared in config file might need to be different for portions of the application (shippingForm) Partitioning development teams has been somewhat difficult Partitioning development teams has been somewhat difficult

25 1.1 Solution: Modules Separate, independent applications (uses different configuration file) under “default” application Separate, independent applications (uses different configuration file) under “default” application Modules are based on paths (xyz.com/app/sub- app) and web.xml entries Modules are based on paths (xyz.com/app/sub- app) and web.xml entries Created to modularize large applications Created to modularize large applications –Break modules up according to team Unable to share resources between modules Unable to share resources between modules

26 Module Example Jakarta Struts 1.1: Ready for Prime TimeJakarta Struts 1.1: Ready for Prime Time (PDF) by Chuck Cavaness.

27 Handling Errors Struts 1.0 applications had to do all of their error handling inside the Action classes Struts 1.0 applications had to do all of their error handling inside the Action classes This could mean a significant amount duplication of error handling code This could mean a significant amount duplication of error handling code Some large apps had to customize the framework to do flexible error handling Some large apps had to customize the framework to do flexible error handling

28 1.1 Solution: Declarative Error Handling Provides global and per Action handling of errors Provides global and per Action handling of errors Declare errors by Exception class Declare errors by Exception class Create custom error handlers by implementing interface Create custom error handlers by implementing interface

29 Error Handling Example Declaration<global-exceptions> </global-exceptions> Java Use public ActionForward execute( Exception ex, ExceptionConfig exConfig, ActionMapping mapping, ActionForm formInstance, HttpServletRequest request, HttpServletResponse response ) throws ServletException { handle error… return mapping.findForward(“errorPage”); }

30 Problems with Page Layout Struts 1.0 provided some page layout mechanism Struts 1.0 provided some page layout mechanism Large application pages can get very complicated without a better layout mechanism Large application pages can get very complicated without a better layout mechanism 1.0 version could not change based on Locale 1.0 version could not change based on Locale 1.0 version did not have significant re-use functionality 1.0 version did not have significant re-use functionality

31 1.1 Solution:Tiles Integration Tiles code has been integrated into core Struts Tiles code has been integrated into core Struts Tiles provides flexible page layout and re- use mechanism Tiles provides flexible page layout and re- use mechanism Provides support for Locales Provides support for Locales Tiles can be nested Tiles can be nested Tiles can be used as Struts Forwards by tile name Tiles can be used as Struts Forwards by tile name

32 Tiles Example Tiles-Config.xml<definitions> </definition> <definition name=“home.default” extends=“home.default”/> </definition></definitions>Index.jsp <html:html><body> …

33 Validating Pages and Forms Struts 1.0 only provided automatic support for validating form objects (by calling the validate method) Struts 1.0 only provided automatic support for validating form objects (by calling the validate method) Programmers had to implement validation code inside of the class (ex. making sure phone was really a number) by hand Programmers had to implement validation code inside of the class (ex. making sure phone was really a number) by hand Client-side validation had to be implemented by hand Client-side validation had to be implemented by hand

34 1.1 Solution: Validator Integration Validator integrated into core code base Validator integrated into core code base Use a xml file to declaratively validate form objects Use a xml file to declaratively validate form objects Validators are keyed against the form by name Validators are keyed against the form by name Comes with many pre-built validators (e- mail, date, etc) Comes with many pre-built validators (e- mail, date, etc) Supports client-side (JavaScript) and server-side validation Supports client-side (JavaScript) and server-side validation

35 Validator Config Example Struts-Config.xml </form>Validator.xml <field property=“address” depends=“required, mask”/> depends=“required, mask”/> <var> mask mask ^\w+$ ^\w+$ </var></field>…

36 Validator JSP Example Index.jsp <html:html><body> (this makes the form call the validation JavaScript onSubmit) <table>…etc.</table> (This inserts the javascript which executes the validation) (This inserts the javascript which executes the validation)…

37 Initializing Custom Functionality Often custom applications need to initialize information when the application starts up Often custom applications need to initialize information when the application starts up Since Struts runs off of a servlet this was very difficult Since Struts runs off of a servlet this was very difficult

38 1.1 Solution: Plug-Ins Plug-Ins provide a means for initializing components on application startup Plug-Ins provide a means for initializing components on application startup Provide means for Factory functionality or customization Provide means for Factory functionality or customization Init-parameters let you pass information into plug-in Init-parameters let you pass information into plug-in

39 Plug-in Example Struts-Config.xml </plug-in> TestPlugin Code public void init(ActionServlet servlet, ApplicationConfig config) { perform initialization… } Public void destroy() { perform shutdown… }

40 Section 2 Conclusion Struts 1.1 has added significant functionality to help medium to large application teams Struts 1.1 has added significant functionality to help medium to large application teams Even large application teams should be able to use Struts without actually modifying the underlying Struts code: Even large application teams should be able to use Struts without actually modifying the underlying Struts code: –Plug-ins allow initialization of advanced “patterns” –Declarative error handling allows re-use of error code –Dynamic forms save tremendous effort –Validation saves significant effort –Modules provide large team partitioning

41 Architectural Challenges “Contextual” Pages & Declarative Branching “Contextual” Pages & Declarative Branching Composable Actions Composable Actions Re-Use Re-Use Event Handling Event Handling J2EE Integration J2EE Integration Content Management Integration Content Management Integration

42 Contextual Pages Sometimes the developer wants to use JSPs in different locations in the page flow Sometimes the developer wants to use JSPs in different locations in the page flow These pages and their associated action classes must then have large if/then blocks These pages and their associated action classes must then have large if/then blocks Certain types of context tracking are almost impossible without extending the framework (dynamically modifying actions, etc) Certain types of context tracking are almost impossible without extending the framework (dynamically modifying actions, etc)

43 Contextual Pages

44 Declarative Branching

45 Solutions: Context Subsystem Create Context object which tracks the “path” the user has traveled Create Context object which tracks the “path” the user has traveled Create ContextualAction super-class which adds the page and action to the context Create ContextualAction super-class which adds the page and action to the context Make all Action classes subclass ContextualAction and use path history for logic Make all Action classes subclass ContextualAction and use path history for logic

46 Composable Actions Actions are currently made up of only one class file Actions are currently made up of only one class file To partition an Action the developer must use large if/then blocks and Request properties To partition an Action the developer must use large if/then blocks and Request properties The developer must put all of this code in one action or write custom JavaScript to call different actions based on the users mouse-click The developer must put all of this code in one action or write custom JavaScript to call different actions based on the users mouse-click This is not made easy for the developer and can cause problems if not thought through early on This is not made easy for the developer and can cause problems if not thought through early on

47 Composable Actions

48 Solution:Composable Actions Some amount of “composition” can be achieved using local ActionForwards Some amount of “composition” can be achieved using local ActionForwards Better composition can be achieved with the Better composition can be achieved with the Struts Action Plug-in Extension 1.0.2 by ASQdotCOMASQdotCOM Struts 2.0 should have a “workflow” component to compose actions from re- usable steps

49 Solution:Composable Actions Step 1:Write a Java class that implements the ActionPlugIn interface, to create an action plug-in. Step 1:Write a Java class that implements the ActionPlugIn interface, to create an action plug-in. Step 1: Step 1: Step 2:Declare (and configure) the action plug-in that you created in step 1 in the action-plug-in-config.xml configuration file. Step 2:Declare (and configure) the action plug-in that you created in step 1 in the action-plug-in-config.xml configuration file. Step 2: Step 2: Step 3:Add a Struts tag to the struts- config.xml configuration file to get the action plug-in chain initialized on application start-up. Step 3:Add a Struts tag to the struts- config.xml configuration file to get the action plug-in chain initialized on application start-up. Step 3: Step 3: Step 4:Make sure that there is a tag specified in the struts-config.xml configuration file, that has its processClass attribute set to be.ff.web.struts.action.ActionPlugInRequestProcessor. Step 4:Make sure that there is a tag specified in the struts-config.xml configuration file, that has its processClass attribute set to be.ff.web.struts.action.ActionPlugInRequestProcessor. Step 4: Step 4:

50 Solution:Composable Actions Declaration<action-plug-in-config> ActionPlugIn ActionPlugIn <init-params><init-params> /logon /logon</disabled-for></action-plug-in> WorkflowActionP lugIn WorkflowActionP lugIn </action-plug-in> Java Use public ActionForward execute() throws ActionPlugInException { UserAccount account = (UserAccount) session.getAttribute(accountSessionKey); if (account == null) { if (account == null) { return mapping.findForward("logon"); } else { return chain.execute(mapping, form, request, response); }

51 Re-Use There are two possibilities for re-use in Struts, pages and actions There are two possibilities for re-use in Struts, pages and actions Re-use of actions is very difficult because: Re-use of actions is very difficult because: –the granularity of the actions is too high (one action per page) –The actions cannot be chained together or segmented Re-use of pages in struts is very difficult because: Re-use of pages in struts is very difficult because: –There is no context object tracking location, etc. –There is no way of overriding a page/form declaration within a context

52 Action Re-use

53 J2EE Integration Struts does not provide any out of the box functionality for J2EE integration (EJBs, JDO, JDBC, JMS, JavaMail) Struts does not provide any out of the box functionality for J2EE integration (EJBs, JDO, JDBC, JMS, JavaMail) Each project/development team must decide ahead of time on a mechanism to do J2EE integration Each project/development team must decide ahead of time on a mechanism to do J2EE integration Developers must build their own frameworks for J2EE integration even in very simple applications Developers must build their own frameworks for J2EE integration even in very simple applications

54 Solution: J2EE Integration Create Declarative support for long-lived model objects (beyond forms) Create Declarative support for long-lived model objects (beyond forms) Create Struts J2EE integration (session and entity beans) using dynamic value objects and session bean proxies Create Struts J2EE integration (session and entity beans) using dynamic value objects and session bean proxies Create Struts JDO integration Create Struts JDO integration

55 Solution: J2EE Integration

56 Events Struts provides no mechanism for event based functionality Struts provides no mechanism for event based functionality Though not strictly required, it is often much easier to write some function using an event Though not strictly required, it is often much easier to write some function using an event For example, if you want to run some function every time the user goes to the edit page, but the edit page can be reached by 10 different actions… For example, if you want to run some function every time the user goes to the edit page, but the edit page can be reached by 10 different actions…

57 Solution: Event Subsystem Create an Event Multicaster/Controller to manage event listeners and publish specific events Create an Event Multicaster/Controller to manage event listeners and publish specific events Most useful events are Navigation, Action, Form events Most useful events are Navigation, Action, Form events Create Action Superclass which fires navigation and action events Create Action Superclass which fires navigation and action events Create Dynamic form superclass which fires form events Create Dynamic form superclass which fires form events

58 Solution: Event Subsystem Fire Navigate Event Fire Action Event

59 Content Integration Struts provides very little in terms of content management integration. Struts provides very little in terms of content management integration. If your team uses custom content management systems, you will have to customize the own framework If your team uses custom content management systems, you will have to customize the own framework The message framework in Struts is only acceptable for small to medium applications The message framework in Struts is only acceptable for small to medium applications

60 Solution: Content Integration Customize Message Resources classes to handle more advanced input Customize Message Resources classes to handle more advanced input Customize Validator framework to handle more advanced input Customize Validator framework to handle more advanced input Possible input mechanisms: Possible input mechanisms: –Xml files –Content Management Systems –DBMS

61 Section 3 Conclusion While Struts 1.1 provides excellent features for web applications it may be deficient for Enterprise class architectures While Struts 1.1 provides excellent features for web applications it may be deficient for Enterprise class architectures Large Application Teams may need to create a custom Struts implementation to accomplish: Large Application Teams may need to create a custom Struts implementation to accomplish: –Contextual Page Handling –Dynamic Action Branching and page flow –Composable Actions for Re-Use –Significant J2EE integration –Event Handling –Content Management Integration

62 Resources Struts website: http://jakarta.apache.org/struts Struts website: http://jakarta.apache.org/strutshttp://jakarta.apache.org/struts Struts Action Plug-in Extension 1.0.2: http://www.asqdotcom.be/struts/ Struts Action Plug-in Extension 1.0.2: http://www.asqdotcom.be/struts/ http://www.asqdotcom.be/struts/ Struts workflow proposal: http://jakarta.apache.org/struts/proposals/workflow.html Struts workflow proposal: http://jakarta.apache.org/struts/proposals/workflow.html http://jakarta.apache.org/struts/proposals/workflow.html Struts Resources: http://jakarta.apache.org/struts/resources/index.html Struts Resources: http://jakarta.apache.org/struts/resources/index.html http://jakarta.apache.org/struts/resources/index.html


Download ppt "Struts:The good, the bad, the ugly A detailed evaluation of Struts 1.1 and Enterprise Web Applications. By Paul Smith Ancept, Inc. (www.ancept.com) www.ancept.com."

Similar presentations


Ads by Google