Struts:The good, the bad, the ugly A detailed evaluation of Struts 1.1 and Enterprise Web...

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

Transcript of Struts:The good, the bad, the ugly A detailed evaluation of Struts 1.1 and Enterprise Web...

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

Struts:The good, the Struts:The good, the bad, the uglybad, the ugly

A detailed evaluation of Struts 1.1 A detailed evaluation of Struts 1.1 and Enterprise Web Applications.and Enterprise Web Applications.

By Paul SmithBy Paul SmithAncept, Inc. (Ancept, Inc. (www.ancept.comwww.ancept.com))

[email protected]@ancept.com

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

Presentation OverviewPresentation Overview ThemesThemes Struts Overview (Basic Features)Struts Overview (Basic Features) Development Challenges in 1.0Development Challenges in 1.0

– Problem DescriptionProblem Description– Solution in 1.1Solution in 1.1– ExampleExample

Architectural Challenges Struts Architectural Challenges Struts ApplicationsApplications

– Problem DescriptionProblem Description– Possible SolutionPossible Solution

Q & AQ & A

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

ThemesThemes

1.1. Struts 1.1 provides small to medium Struts 1.1 provides small to medium sized applications all the functionality sized applications all the functionality they should need “out of the box”they should need “out of the box”

2.2. A large project team should be aware A large project team should be aware of limitations of Struts 1.1 which may of limitations of Struts 1.1 which may require customized solutions require customized solutions

3.3. Large architecture teams may need a Large architecture teams may need a custom implementation of Struts 1.1 custom implementation of Struts 1.1 to enterprise class architecturesto enterprise class architectures

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

Supporting the ThemesSupporting the Themes

To illustrate the themes:To illustrate the themes:– Section 1 describes Struts basic featuresSection 1 describes Struts basic features– Section 2 describes the new features of Section 2 describes the new features of

Struts 1.1 and why they existStruts 1.1 and why they exist– Section 3 describes architectural Section 3 describes architectural

challenges in large projects and possible challenges in large projects and possible solutionssolutions

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

Section 1-Basic FeaturesSection 1-Basic Features

Why Use A FrameworkWhy Use A Framework Struts OverviewStruts Overview

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

Why Use the Struts Why Use the Struts FrameworkFramework

A pre-built framework is better than A pre-built framework is better than roll-your-ownroll-your-own

Increased productivity in JSP Increased productivity in JSP productionproduction

Cleaner designCleaner design More time on business logicMore time on business logic

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

Easier than building your Easier than building your ownown

Probably very similar to what you Probably very similar to what you would end up building anywaywould end up building anyway

Strong industry support instead of Strong industry support instead of your own custom frameworkyour own custom framework

The Struts framework is configurable The Struts framework is configurable so it will probably work for your so it will probably work for your project without building your ownproject without building your own

Customizing open-source is simpler Customizing open-source is simpler than building from scratchthan building from scratch

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

JSP ProductivityJSP Productivity

Writing pages in Struts is roughly 40-Writing pages in Struts is roughly 40-60% faster than without struts 60% faster than without struts because you use tags NOT java codebecause you use tags NOT java code

The custom tags, form object, The custom tags, form object, messages architecture makes data messages architecture makes data display easy (for medium sized apps)display easy (for medium sized apps)

Tiles development helps manage Tiles development helps manage page complexitypage complexity

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

Cleaner DesignCleaner Design

Struts applications are easier to Struts applications are easier to develop and maintain because the develop and maintain because the design concepts are elegantdesign concepts are elegant– Non-technical staff better understands Non-technical staff better understands

layout and designlayout and design– Since code is targeted at specific business Since code is targeted at specific business

problems, the team communicates betterproblems, the team communicates better– Design is easier to expand for very large Design is easier to expand for very large

application architecturesapplication architectures

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

More Time on Business More Time on Business LogicLogic

Less coding of web infrastructure means Less coding of web infrastructure means more time for business logic:more time for business logic:– Page flow designPage flow design– Action execution synchronizationAction execution synchronization– Page model objectsPage model objects– ValidationValidation– Page templatesPage templates

Focus on re-usable custom frameworks to Focus on re-usable custom frameworks to use with Struts (J2EE integration, Web use with Struts (J2EE integration, Web services)services)

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

Struts OverviewStruts Overview

Struts DescriptionStruts Description Common Infrastructure TasksCommon Infrastructure Tasks Struts Solutions to Infrastructure Struts Solutions to Infrastructure

TasksTasks How does Struts worksHow does Struts works The Struts Configuration FileThe Struts Configuration File Where does it fit in J2EE applicationsWhere does it fit in J2EE applications Case StudyCase Study

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

Struts DescriptionStruts Description

Struts is a Web Application Framework Struts is a Web Application Framework (custom tag libraries, servlet, xml (custom tag libraries, servlet, xml control files) which increase the control files) which increase the productivity of web developersproductivity of web developers

Struts was invented by Craig Struts was invented by Craig McClanahan in 2000McClanahan in 2000

Struts solves common problems with Struts solves common problems with web application development by web application development by abstracting common infrastructure abstracting common infrastructure taskstasks

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

Common Infrastructure Common Infrastructure TasksTasks

Page flow design and controlPage flow design and control Code execution framework (Command Code execution framework (Command

Pattern)Pattern) Automated data objects (MVC Pattern)Automated data objects (MVC Pattern) Easy to use custom tags for displayEasy to use custom tags for display ValidationValidation Page templates & layoutPage templates & layout Re-usable text messages Re-usable text messages Much more…Much more…

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

Struts Solutions to Struts Solutions to InfrastructureInfrastructure

Forwards:Forwards: Page flow design and control Page flow design and control Actions:Actions: Code execution framework (Command Code execution framework (Command

Pattern)Pattern) Forms:Forms: Automated data objects (MVC Pattern) Automated data objects (MVC Pattern) Struts Tags:Struts Tags: Easy to use custom tags for Easy to use custom tags for

displaydisplay Validator:Validator: Validation Validation Tiles:Tiles: Page templates & layout Page templates & layout Message Resources:Message Resources: Handles re-usable text Handles re-usable text

messages in multiple languagesmessages in multiple languages

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

How Struts WorksHow Struts Works

Web Container

JSP

Struts Tags

Struts Controller Servlet

Browser - HTML

Request

Execute

ResponseHTML Form

HTML Text Box

HTML List Box

HTML SubmitButton

1

Browser - HTML

HTML Form

HTML SubmitButton

Struts Config XML file-Forms-Forwards-Actions-Messages-Plug-ins

Action Classes

Forward

1

2

3

4

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

Struts ConfigurationStruts Configuration

Struts is controlled by a Configuration fileStruts is controlled by a Configuration file Struts Configuration contains:Struts Configuration contains:

– Form Definitions: Data objects for JSP pagesForm Definitions: Data objects for JSP pages– Forward Definitions: Abstract pages for page Forward Definitions: Abstract pages for page

flow control flow control – Action Definitions: maps requests to Command Action Definitions: maps requests to Command

classclass– Message Resources: Storage of common text Message Resources: Storage of common text

messagesmessages– Plug-In Definitions: Startup classes used for Plug-In Definitions: Startup classes used for

initializing custom functionalityinitializing custom functionality

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

Where does Struts fit into Where does Struts fit into J2EEJ2EE

Java Application ServerWeb Container

DB

Web Srvc

SessionBeans

MessageBeans

Entity Beans

CMP BMP

ControllerServlet

PersistenceManager

JCA

Browser

Scripting

JSP

CustomTags

JMS

HTTP

RMI(IIOP)

StrutsConfigFiles

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

Case StudyCase Study

Web Pages100 pages(display)

Custom Control Logic13,500 code lines(data, validation,

business logic calls)

ApplicationBusiness

Logic

Still a significantamount of effort

Department of Department of Transportation created an Transportation created an aircraft registration aircraft registration application for internal application for internal staff (J2EE with a 2staff (J2EE with a 2ndnd Generation Framework)Generation Framework)

100 JSPs100 JSPs 13.5 k lines of code in the 13.5 k lines of code in the

web tier (no business web tier (no business logic).logic).

20,000 lines of code for 20,000 lines of code for business logic.business logic.

8 months of development 8 months of development with a team of 5with a team of 5

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

Section 1 ConclusionSection 1 Conclusion Struts base feature-set include significant Struts base feature-set include significant

functionality for small to medium applicationsfunctionality for small to medium applications Small applications should not need to Small applications should not need to

customize Struts significantly:customize Struts significantly:– Form objects and Action classes provide MVC designForm objects and Action classes provide MVC design– Action classes house functional codeAction classes house functional code– Forwards abstract page flow controlForwards abstract page flow control– Form objects and Struts tags provide page displayForm objects and Struts tags provide page display– Tiles provides flexible page layoutTiles provides flexible page layout– Validator provides flexible form/page level Validator provides flexible form/page level

validationvalidation– Message Resources provide re-usable textMessage Resources provide re-usable text

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

Development Challenges in Development Challenges in 1.01.0

Large Numbers of Form ClassesLarge Numbers of Form Classes Large teams and ResourcesLarge teams and Resources Handling Errors More FlexiblyHandling Errors More Flexibly Problems with Page LayoutProblems with Page Layout Validating Pages and FormsValidating Pages and Forms Initializing Custom FunctionalityInitializing Custom Functionality

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

Lots of Form ClassesLots of Form Classes

In Struts 1.0 developers had to write a In Struts 1.0 developers had to write a custom class for every form (generally custom class for every form (generally 1 per JSP form)1 per JSP form)

Form classes are generally very simpleForm classes are generally very simple Task assigned to junior programmers Task assigned to junior programmers

(drudge work)(drudge work) Form classes sometimes not Form classes sometimes not

consistentconsistent

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

1.1 Solution Dynamic Form 1.1 Solution Dynamic Form ObjectsObjects

Declare Forms in the struts-config.xml Declare Forms in the struts-config.xml filefile

Form objects are created when the Form objects are created when the action executes and stored in the action executes and stored in the session or request scopesession or request scope

Form fields are set and retrieved Form fields are set and retrieved using the using the PropertyUtils.setSimpleProperty(…) PropertyUtils.setSimpleProperty(…) method or treated as HashMap method or treated as HashMap

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

Dynamic Form ExampleDynamic Form Example

DeclarationDeclaration<form><form>

<form-property <form-property name=“” type=“” name=“” type=“” value=“”/>value=“”/>

</form></form>

Java UseJava UsePublic void execute(mapping, form, …){Public void execute(mapping, form, …){

String testProp = String testProp = (String)PropertyUtils.getSimpleProperty(f(String)PropertyUtils.getSimpleProperty(form, “testProp”);orm, “testProp”);

PropertyUtils.setSimpleProperty(form, PropertyUtils.setSimpleProperty(form, “testProp”, value);“testProp”, value);

}}

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

Large Teams and ResourcesLarge Teams and Resources

It has been difficult to co-ordinate the It has been difficult to co-ordinate the struts-config file and other resources struts-config file and other resources with large teams and projectswith large teams and projects

Resources declared in config file might Resources declared in config file might need to be different for portions of the need to be different for portions of the application (shippingForm)application (shippingForm)

Partitioning development teams has Partitioning development teams has been somewhat difficultbeen somewhat difficult

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

1.1 Solution: Modules1.1 Solution: Modules

Separate, independent applications (uses Separate, independent applications (uses different configuration file) under “default” different configuration file) under “default” applicationapplication

Modules are based on paths Modules are based on paths (xyz.com/app/sub-app) and web.xml (xyz.com/app/sub-app) and web.xml entriesentries

Created to modularize large applicationsCreated to modularize large applications– Break modules up according to teamBreak modules up according to team

Unable to share resources between Unable to share resources between modules modules

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

Module ExampleModule Example

Jakarta Struts 1.1: Ready for Prime Time (PDF) by Chuck Cavaness.

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

Handling ErrorsHandling Errors

Struts 1.0 applications had to do all Struts 1.0 applications had to do all of their error handling inside the of their error handling inside the Action classesAction classes

This could mean a significant amount This could mean a significant amount duplication of error handling codeduplication of error handling code

Some large apps had to customize Some large apps had to customize the framework to do flexible error the framework to do flexible error handlinghandling

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

1.1 Solution: Declarative Error 1.1 Solution: Declarative Error HandlingHandling

Provides global and per Action Provides global and per Action handling of errorshandling of errors

Declare errors by Exception classDeclare errors by Exception class Create custom error handlers by Create custom error handlers by

implementing interfaceimplementing interface

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

Error Handling ExampleError Handling Example

DeclarationDeclaration

<global-exceptions><global-exceptions>

<exception <exception handler="CustomHahandler="CustomHandler" ndler" key="error.messagekey="error.message" path="/error.jsp" " path="/error.jsp" scope="request" scope="request" type="java.lang.Exctype="java.lang.Exception"/> eption"/>

</global-exceptions> </global-exceptions>

Java UseJava Use

public ActionForward execute( Exception ex, public ActionForward execute( Exception ex, ExceptionConfig exConfig, ActionMapping ExceptionConfig exConfig, ActionMapping mapping, ActionForm formInstance, mapping, ActionForm formInstance, HttpServletRequest request, HttpServletRequest request, HttpServletResponse response ) throws HttpServletResponse response ) throws ServletExceptionServletException

{{

handle error…handle error…

return return mapping.findForward(“errorPage”);mapping.findForward(“errorPage”);

}}

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

Problems with Page LayoutProblems with Page Layout

Struts 1.0 provided some page layout Struts 1.0 provided some page layout mechanism mechanism

Large application pages can get very Large application pages can get very complicated without a better layout complicated without a better layout mechanismmechanism

1.0 version could not change based 1.0 version could not change based on Localeon Locale

1.0 version did not have significant 1.0 version did not have significant re-use functionalityre-use functionality

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

1.1 Solution:Tiles 1.1 Solution:Tiles IntegrationIntegration

Tiles code has been integrated into Tiles code has been integrated into core Strutscore Struts

Tiles provides flexible page layout Tiles provides flexible page layout and re-use mechanismand re-use mechanism

Provides support for LocalesProvides support for Locales Tiles can be nestedTiles can be nested Tiles can be used as Struts Forwards Tiles can be used as Struts Forwards

by tile nameby tile name

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

Tiles ExampleTiles ExampleTiles-Config.xmlTiles-Config.xml

<definitions><definitions> <definition <definition

name=“home.default” name=“home.default” path=“/index.jsp”/>path=“/index.jsp”/><put name=“header” <put name=“header” value=“header.jsp”/>value=“header.jsp”/>

</definition></definition><definition <definition

name=“home.default” name=“home.default”

extends=“home.default”/extends=“home.default”/>><put name=“header” <put name=“header” value=“other.jsp”/>value=“other.jsp”/>

</definition></definition>

</definitions></definitions>

Index.jspIndex.jsp

<%@ taglib URI=“WEB-INF/tiles.tld” <%@ taglib URI=“WEB-INF/tiles.tld” name=“tiles%>name=“tiles%>

<html:html><html:html>

<body><body>

<tiles:insert attribute=“header”/><tiles:insert attribute=“header”/>

<tiles:insert attribute=“body”/><tiles:insert attribute=“body”/>

<tiles:insert attribute=“footer”/><tiles:insert attribute=“footer”/>

……

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

Validating Pages and FormsValidating Pages and Forms

Struts 1.0 only provided automatic Struts 1.0 only provided automatic support for validating form objects (by support for validating form objects (by calling the validate method)calling the validate method)

Programmers had to implement Programmers had to implement validation code inside of the class (ex. validation code inside of the class (ex. making sure phone was really a making sure phone was really a number) by handnumber) by hand

Client-side validation had to be Client-side validation had to be implemented by hand implemented by hand

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

1.1 Solution: Validator 1.1 Solution: Validator IntegrationIntegration

Validator integrated into core code Validator integrated into core code basebase

Use a xml file to declaratively validate Use a xml file to declaratively validate form objectsform objects

Validators are keyed against the form Validators are keyed against the form by nameby name

Comes with many pre-built validators Comes with many pre-built validators (e-mail, date, etc)(e-mail, date, etc)

Supports client-side (JavaScript) and Supports client-side (JavaScript) and server-side validationserver-side validation

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

Validator Config ExampleValidator Config Example

Struts-Config.xmlStruts-Config.xml<form <form

name=“shippingForm”>name=“shippingForm”>

<form-property <form-property name=“address” name=“address” type=“java.lang.String” type=“java.lang.String” />/>

</form></form>

Validator.xmlValidator.xml<form name=“shippingForm”><form name=“shippingForm”>

<field property=“address”<field property=“address”

depends=“required, mask”/>depends=“required, mask”/>

<arg0 key=“label.address”/><arg0 key=“label.address”/>

<var><var>

<var-name>mask</var-<var-name>mask</var-name>name>

<var-value>^\w+$</var-<var-value>^\w+$</var-value>value>

</var></var>

</field></field>

……

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

Validator JSP ExampleValidator JSP Example

Index.jspIndex.jsp

<%@ taglib URI=“WEB-INF/tiles.tld” name=“tiles%><%@ taglib URI=“WEB-INF/tiles.tld” name=“tiles%>

<html:html><html:html>

<body><body>

<html:form action=“logonSubmit” onsubmit=“validateLogonForm(this)”> <html:form action=“logonSubmit” onsubmit=“validateLogonForm(this)”>

(this makes the form call the validation JavaScript (this makes the form call the validation JavaScript onSubmit)onSubmit)

<table><table>

……etc.etc.

</table></table>

<validator:javascript formName=“logonForm”/> <validator:javascript formName=“logonForm”/> (This inserts the (This inserts the javascript which executes the validation)javascript which executes the validation)

……

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

Initializing Custom Initializing Custom FunctionalityFunctionality

Often custom applications need to Often custom applications need to initialize information when the initialize information when the application starts upapplication starts up

Since Struts runs off of a servlet this Since Struts runs off of a servlet this was very difficultwas very difficult

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

1.1 Solution: Plug-Ins1.1 Solution: Plug-Ins

Plug-Ins provide a means for Plug-Ins provide a means for initializing components on initializing components on application startupapplication startup

Provide means for Factory Provide means for Factory functionality or customizationfunctionality or customization

Init-parameters let you pass Init-parameters let you pass information into plug-ininformation into plug-in

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

Plug-in ExamplePlug-in Example

Struts-Config.xmlStruts-Config.xml<plug-in <plug-in

classname=“TestPluginclassname=“TestPlugin”>”>

<set-property <set-property property=“datasource” property=“datasource” value=“testData”/>value=“testData”/>

<set-property <set-property property=“user” property=“user” value=“user”/>value=“user”/>

</plug-in></plug-in>

TestPlugin CodeTestPlugin Codepublic void init(ActionServlet servlet, public void init(ActionServlet servlet,

ApplicationConfig config)ApplicationConfig config)

{{

perform initialization…perform initialization…

}}

Public void destroy()Public void destroy()

{{

perform shutdown…perform shutdown…

}}

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

Section 2 ConclusionSection 2 Conclusion Struts 1.1 has added significant functionality Struts 1.1 has added significant functionality

to help medium to large application teamsto help medium to large application teams Even large application teams should be able Even large application teams should be able

to use Struts without actually modifying the to use Struts without actually modifying the underlying Struts code:underlying Struts code:– Plug-ins allow initialization of advanced Plug-ins allow initialization of advanced

“patterns”“patterns”– Declarative error handling allows re-use of error Declarative error handling allows re-use of error

codecode– Dynamic forms save tremendous effortDynamic forms save tremendous effort– Validation saves significant effortValidation saves significant effort– Modules provide large team partitioningModules provide large team partitioning

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

Architectural ChallengesArchitectural Challenges

““Contextual” Pages & Declarative Contextual” Pages & Declarative BranchingBranching

Composable ActionsComposable Actions Re-UseRe-Use Event HandlingEvent Handling J2EE IntegrationJ2EE Integration Content Management IntegrationContent Management Integration

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

Contextual PagesContextual Pages

Sometimes the developer wants to Sometimes the developer wants to use JSPs in different locations in the use JSPs in different locations in the page flowpage flow

These pages and their associated These pages and their associated action classes must then have large action classes must then have large if/then blocksif/then blocks

Certain types of context tracking are Certain types of context tracking are almost impossible without extending almost impossible without extending the framework (dynamically the framework (dynamically modifying actions, etc)modifying actions, etc)

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

Contextual PagesContextual Pages

Login Index

SearchResults

Action=Search

ViewResult

Action=View

Edit Result

Action=Edit

Another Page Action=Edit

Start

Action=Save

Action=Save

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

Declarative BranchingDeclarative Branching

Cart

Basic Information

Action=Checkout

PaymentInformation

POInfo 1

Confirmation

Start

POInfo 2

Action=Finish

Action=Next

Action=Finish

Action=Next

If this is a company user goto the PO Wizard

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

Solutions: Context Solutions: Context SubsystemSubsystem

Create Context object which tracks Create Context object which tracks the “path” the user has traveledthe “path” the user has traveled

Create ContextualAction super-class Create ContextualAction super-class which adds the page and action to which adds the page and action to the contextthe context

Make all Action classes subclass Make all Action classes subclass ContextualAction and use path ContextualAction and use path history for logichistory for logic

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

Composable ActionsComposable Actions Actions are currently made up of only one class Actions are currently made up of only one class

filefile

To partition an Action the developer must use To partition an Action the developer must use large if/then blocks and Request propertieslarge if/then blocks and Request properties

The developer must put all of this code in one The developer must put all of this code in one action or write custom JavaScript to call different action or write custom JavaScript to call different actions based on the users mouse-clickactions based on the users mouse-click

This is not made easy for the developer and can This is not made easy for the developer and can cause problems if not thought through early oncause problems if not thought through early on

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

Composable ActionsComposable Actions

Cart Page

Checkout Action

Action=Checkout

Finish ActionAction=Finish

Final Page

CheckoutPage

Forward

ActionStep 1

ActionStep 2

ActionStep 1

Forward

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

Solution:Composable Solution:Composable ActionsActions

Some amount of “composition” can Some amount of “composition” can be achieved using local be achieved using local ActionForwardsActionForwards

Better composition can be achieved Better composition can be achieved with the with the Struts Action Plug-in Extension 1.0.2 by ASQdotCOM

Struts 2.0 should have a “workflow” component to compose actions from re-usable steps

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

Solution:Composable Solution:Composable ActionsActions

Step 1:Step 1:Write a Java class that implements the Write a Java class that implements the ActionPlugIn interface, to create an action plug-in.ActionPlugIn interface, to create an action plug-in.

Step 2:Step 2:Declare (and configure) the action plug-in Declare (and configure) the action plug-in that you created in step 1 in the action-plug-in-that you created in step 1 in the action-plug-in-config.xml configuration file.config.xml configuration file.

Step 3:Step 3:Add a Struts <plug-in> tag to the struts-Add a Struts <plug-in> tag to the struts-config.xml configuration file to get the action config.xml configuration file to get the action plug-in chain initialized on application start-up.plug-in chain initialized on application start-up.

Step 4:Step 4:Make sure that there is a <controller> tag Make sure that there is a <controller> tag specified in the struts-config.xml configuration specified in the struts-config.xml configuration file, that has its processClass attribute set to file, that has its processClass attribute set to be.ff.web.struts.action.ActionPlugInRequestProcesbe.ff.web.struts.action.ActionPlugInRequestProcessor.sor.

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

Solution:Composable Solution:Composable ActionsActions

Declaration

<action-plug-in-config><action-plug-in-config><action-plug-in> <action-plug-in>

<class>ActionPlugIn<class>ActionPlugIn</class> </class> <init-params> <init-params> <init-params> <init-params> <disabled-for> <disabled-for>

/logon/logon</disabled-for> </disabled-for>

</action-plug-in> </action-plug-in>

<action-plug-in> <action-plug-in> <class>WorkflowActi<class>WorkflowActionPlugIn</class> onPlugIn</class>

</action-plug-in></action-plug-in>

Java UseJava Use

public ActionForward public ActionForward executeexecute() throws () throws ActionPlugInException ActionPlugInException

{{UserAccount account = (UserAccount) UserAccount account = (UserAccount) session.getAttribute(accountSessionKey);session.getAttribute(accountSessionKey);

if (account == null) { if (account == null) {

return mapping.findForward("logon"); return mapping.findForward("logon");

} }

else { else {

return chain.execute(mapping, form, return chain.execute(mapping, form, request, response); request, response);

} }

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

Re-UseRe-Use There are two possibilities for re-use in Struts, There are two possibilities for re-use in Struts,

pages and actionspages 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 the granularity of the actions is too high (one action per page)action per page)

– The actions cannot be chained together or The actions cannot be chained together or segmentedsegmented

Re-use of pages in struts is very difficult Re-use of pages in struts is very difficult because:because:– There is no context object tracking location, etc.There is no context object tracking location, etc.– There is no way of overriding a page/form There is no way of overriding a page/form

declaration within a contextdeclaration within a context

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

Action Re-useAction Re-use

Cart Page

Checkout Action

Finish Action

ActionStep 1

ActionStep 2

ActionStep 1

Action Re-useAction Re-use

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

J2EE IntegrationJ2EE Integration

Struts does not provide any out of the Struts does not provide any out of the box functionality for J2EE integration box functionality for J2EE integration (EJBs, JDO, JDBC, JMS, JavaMail)(EJBs, JDO, JDBC, JMS, JavaMail)

Each project/development team must Each project/development team must decide ahead of time on a mechanism decide ahead of time on a mechanism to do J2EE integrationto do J2EE integration

Developers must build their own Developers must build their own frameworks for J2EE integration even frameworks for J2EE integration even in very simple applicationsin very simple applications

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

Solution: J2EE IntegrationSolution: J2EE Integration

Create Declarative support for long-Create Declarative support for long-lived model objects (beyond forms)lived model objects (beyond forms)

Create Struts J2EE integration Create Struts J2EE integration (session and entity beans) using (session and entity beans) using dynamic value objects and session dynamic value objects and session bean proxiesbean proxies

Create Struts JDO integrationCreate Struts JDO integration

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

Solution: J2EE IntegrationSolution: J2EE Integration

Java Application Server

Web Container

DB

Web Srvc

SessionBeans

MessageBeans

Entity Beans

CMP BMP

DB Pools

StrutsController

Logic

PersistenceManager

JCA

StrutsCustom

Tags

JMS

J2EEModel

Objects

JDO/JDBCModel

Objects

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

EventsEvents

Struts provides no mechanism for Struts provides no mechanism for event based functionalityevent based functionality

Though not strictly required, it is Though not strictly required, it is often much easier to write some often much easier to write some function using an eventfunction using an event

For example, if you want to run some For example, if you want to run some function every time the user goes to function every time the user goes to the edit page, but the edit page can the edit page, but the edit page can be reached by 10 different actions…be reached by 10 different actions…

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

Solution: Event SubsystemSolution: Event Subsystem

Create an Event Multicaster/Controller Create an Event Multicaster/Controller to manage event listeners and publish to manage event listeners and publish specific eventsspecific events

Most useful events are Navigation, Most useful events are Navigation, Action, Form eventsAction, Form events

Create Action Superclass which fires Create Action Superclass which fires navigation and action eventsnavigation and action events

Create Dynamic form superclass which Create Dynamic form superclass which fires form eventsfires form events

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

Solution: Event SubsystemSolution: Event Subsystem

Cart Page

Checkout Action

ActionStep 1

ActionStep 2 Page

Fire NavigateFire NavigateEventEvent

Fire ActionFire ActionEventEvent

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

Content IntegrationContent Integration

Struts provides very little in terms of Struts provides very little in terms of content management integration. content management integration.

If your team uses custom content If your team uses custom content management systems, you will have management systems, you will have to customize the own frameworkto customize the own framework

The message framework in Struts is The message framework in Struts is only acceptable for small to medium only acceptable for small to medium applicationsapplications

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

Solution: Content Solution: Content IntegrationIntegration

Customize Message Resources classes Customize Message Resources classes to handle more advanced inputto handle more advanced input

Customize Validator framework to Customize Validator framework to handle more advanced inputhandle more advanced input

Possible input mechanisms:Possible input mechanisms:– Xml filesXml files– Content Management SystemsContent Management Systems– DBMSDBMS

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

Section 3 ConclusionSection 3 Conclusion While Struts 1.1 provides excellent While Struts 1.1 provides excellent

features for web applications it may be features for web applications it may be deficient for Enterprise class architecturesdeficient for Enterprise class architectures

Large Application Teams may need to Large Application Teams may need to create a custom Struts implementation to create a custom Struts implementation to accomplish:accomplish:– Contextual Page Handling Contextual Page Handling – Dynamic Action Branching and page flowDynamic Action Branching and page flow– Composable Actions for Re-UseComposable Actions for Re-Use– Significant J2EE integrationSignificant J2EE integration– Event HandlingEvent Handling– Content Management IntegrationContent Management Integration

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

ResourcesResources

Struts website: Struts website: http://jakarta.apache.org/strutshttp://jakarta.apache.org/struts Struts Action Plug-in Extension 1.0.2: Struts Action Plug-in Extension 1.0.2:

http://www.asqdotcom.be/struts/http://www.asqdotcom.be/struts/ Struts workflow proposal: Struts workflow proposal:

http://jakarta.apache.org/struts/proposals/workflohttp://jakarta.apache.org/struts/proposals/workflow.htmlw.html

Struts Resources: Struts Resources: http://jakarta.apache.org/struts/resources/index.hhttp://jakarta.apache.org/struts/resources/index.htmltml