8/8/2019 Tapestry WebTech
1/75
Tapestry: Java WebComponents
Howard M. Lewis [email protected]
8/8/2019 Tapestry WebTech
2/75
What If
you could build a web applicationwithout even thinking about URLs? you could use HTML from your HTMLdevelopers as-is? your web application would localize
itself (just add translations)?
8/8/2019 Tapestry WebTech
3/75
What If
your development team could work together easily ? you could really reuse code ? extending your application meantadding methods, not creating entire
servlets?
8/8/2019 Tapestry WebTech
4/75
What If
this technology existed now , wastested , documented, proven andready to go ?
Tapestry: Java WebComponentsTapestry: Java WebComponents
8/8/2019 Tapestry WebTech
5/75
Concept
Tapestry reconceptualizes webapplication development in
terms of Java objects, methodsand properties
8/8/2019 Tapestry WebTech
6/75
Concept
Removes URLs, query parameters,servlets, HttpSession from developerconsiderations
Application pages cooperate in terms of Java objects and methods, not URLsand query parameters
Still allows 100% control over HTMLproduction
8/8/2019 Tapestry WebTech
7/75
Concept
Build complex apps from simple,reusable components"Bake in" best practices
Error recovery, reportingLocalization / InternationalizationPersonalizationClustering for fail over / scalabilityBrowser-safe HTML
8/8/2019 Tapestry WebTech
8/75
Concept
Reduce amount of Java codingImprove robustness of applicationsSupport team developmentSimplify interaction between HTMLproducers and Java developers
Alternative to JSPs; more dynamic, nocode generation
8/8/2019 Tapestry WebTech
9/75
Concept
Truly separate HTML and Java codeNo Java code in HTML template -- everNormal, static HTML with someelements marked as "dynamic"Tapestry components provide dynamicbehaviorStill looks like normal HTML, no specialtags
8/8/2019 Tapestry WebTech
10/75
Concept
Empower Java DevelopersWrite less codeWrite interesting code
All code written in IDEProvide standardized approach to commonproblems
Break "tyranny of the URL"Develop in terms of objects and properties, notURLs and query parameters
8/8/2019 Tapestry WebTech
11/75
Concept
8/8/2019 Tapestry WebTech
12/75
Concept
Framework links response generation(including generating URLs for links andforms) to dispatch logic
Developers provide pointer toapplication-specific code, executingwhen link clicked or form submitted
Developer decides what and when ,framework provides how
8/8/2019 Tapestry WebTech
13/75
What is a Component?
GeneralEvery framework has own definition of "component"
Has to interact with application, othercomponents"Black-box"; shouldn't need to knowimplementation, just the interface
8/8/2019 Tapestry WebTech
14/75
What is a Component?
SwingResponsible for drawing in 2d spaceProperties configured by application (pushmodel)User interaction:
Event queue -> component Application as event notification listener
8/8/2019 Tapestry WebTech
15/75
What is a Component?
TapestryRenders a portion of (1 dimensional) HTMLstream
Has parameters that are "bound" toapplication properties (pull model)User interaction:
Servlet -> framework -> component Application as delegate to component
8/8/2019 Tapestry WebTech
16/75
What is a Component?
8/8/2019 Tapestry WebTech
17/75
Demo
8/8/2019 Tapestry WebTech
18/75
History
Open Source, LGPLStarted January 2000Moved to SourceForge.net, July 2000
1.0 Release, May 2001Java Report Article, September 2001OnJava Article, November 20012.0 Release, April 2002> 25000 Downloads from SourceForge
8/8/2019 Tapestry WebTech
19/75
Problems and Solutions
8/8/2019 Tapestry WebTech
20/75
8/8/2019 Tapestry WebTech
21/75
Solution: Tapestry
Minimal changes to HTML templates Addition of jwcid attribute Adds tags in some cases
HTML still previews properlyHTML Producers and Java Developers
can work without interfering with eachother
8/8/2019 Tapestry WebTech
22/75
Problem: ComplexityRequests are linked to pages (Sun Model 2)
Servlet (or Struts action) receives requestUpdates "business model"Invokes JSP to render responsePage-oriented dispatch model
ComponentsBehavior of component defined by component
Component wants to handle incoming requestComponent needs to know which page to renderresponse
8/8/2019 Tapestry WebTech
23/75
Problem: Complexity"Components" require special addressingPage dispatch logic
Additional query parameters to identify component Ad-hoc code to "find" component, invoke methodsSimilar code in each servlet or Action that includescomponent
Servlet / Action for componentReceives request, finds component in standardizedwayNeeds to be configured with page to renderresponseJSP may require special setup, known to servlet
8/8/2019 Tapestry WebTech
24/75
Problem: Complexity
Feature creep -- applications grow moreand more complexMany pages have similar functionality
Navigation barsSearch fieldsLook and Feel
Looks the same, acts the same ==component
8/8/2019 Tapestry WebTech
25/75
Solution: TapestryEverything is a componentFramework handles component addressing
Builds and interprets URLsFinds right component on incoming request,invokes component codeComplexity encapsulated by framework
Manages feature creepNo upper limit: Components per page or nesting level
Leverages component object model -- just plainworks
8/8/2019 Tapestry WebTech
26/75
Problem: ScalabilityJ2EE Approach: Clustering multiple serversData stored in HttpSession copied to otherservers in clusterLots of pitfalls
Forgetting to store valuesForgetting to delete unneeded values (bloat)References getting copied (can duplicate objects)EJB references must be converted to EJB Handles
Creating HttpSession too early inefficient, toolate is too late (lots of coding headaches)
8/8/2019 Tapestry WebTech
27/75
Solution: TapestrySingle object, "engine", stores all server-sidestate
Automatically stored into HttpSession at end
of request cycleHttpSession created only when neededRun with or without Cookies equally well
State appears as JavaBeans propertiesNo code to move values in or out of HttpSession
8/8/2019 Tapestry WebTech
28/75
Problem: Team Development /Integration
Lots of developers, each contributing "theirpiece"Interface a combination of:
Servlets or Struts ActionsURL query parametersJava code inside JSPJSP tags
Different developers, different "interfaces"Lack of standards for behavior, naming,errors
8/8/2019 Tapestry WebTech
29/75
Problem: Team Development /Integration
Code that builds URL, interprets URL caneasily get out of syncHard to get a "component" to work inside a"page"
Setup code inside Struts ActionMore code inside JSPJSP includes
Things have to be named "just so"Evolving a component requires changes toeverywhere component is used
8/8/2019 Tapestry WebTech
30/75
Solution: Tapestry
Everything is a componentComponents have named parametersthat are "bound" to page propertiesComponents can read or set propertiesof page via parameters
Consistent approach, easy to documentEclipse Plugin for Tapestry
8/8/2019 Tapestry WebTech
31/75
Problem: Internationalization
Applications need to be localizedHodge podge of techniquesStruts everywhere
Single message fileObscures HTML for HTML producersComplex linking between translated pages
More pages == more complexity == morebugsLots of code everywhere to "find" righttranslation
8/8/2019 Tapestry WebTech
32/75
Solution: TapestryEach page, or component may havelocalized templatesTapestry automatically selects correct
templateCan use message files insteadCan mix and matchCan localize assets (images,stylesheets, etc.) as well
8/8/2019 Tapestry WebTech
33/75
8/8/2019 Tapestry WebTech
34/75
Problem: DebuggingJSPs convert to ugly, unreadable codeHard to get IDE to "recognize" Java codefrom JSPsErrors in JSPs result in blank page or crypticerror messageThrown exceptions not reported well
Usually, only outermost exception displayed, wheninnermost exception most helpfulWith Struts, exception often "lost" and notreported
8/8/2019 Tapestry WebTech
35/75
Solution: Tapestry
No code generation! Ever!Multiple layers of exception handlingand reportingExceptions get very complete report
8/8/2019 Tapestry WebTech
36/75
Solution: TapestryException report:
All exceptions (outermost to innermost)Properties of exceptions
Stack trace of innermost exceptionComplete rundown of all objects:HttpServletRequest
HttpSessionServletContextJVM System Properties
8/8/2019 Tapestry WebTech
37/75
Solution: Tapestry
Example:Bad XML in Tapestry specification fileResult: Exception report includes
SAXException with exact position of errorSavings: Didn't have to start up debugger,restart application, reproduce activity,
analyze in debugger (five - ten minutesminimum)
8/8/2019 Tapestry WebTech
38/75
8/8/2019 Tapestry WebTech
39/75
Full Disclosure: Costs of UsingTapestry
8/8/2019 Tapestry WebTech
40/75
Learning CurveServlets, JSPs, Struts Actions are procedural
Start at top, work to bottom
Monolithic generated Java classMost developers, including Javadevelopers, think procedurally
Struts is "push" oriented -- store valuesinto request or session for later access byJSP
8/8/2019 Tapestry WebTech
41/75
Learning Curve
Tapestry is object orientedMany objects working togetherMore like event-driven GUI
Pull oriented: properties are drawn fromknown objectsMost page-page interaction in Java codeMust unlearn limitations of JSPs
8/8/2019 Tapestry WebTech
42/75
PerformanceSurprisingly similarJSPs
Slow initial start up (create and compile
class, load class)Slight edge under extreme loadTapestry
Quicker initial startup (parsing templates,specifications)
8/8/2019 Tapestry WebTech
43/75
PerformanceComparison
Not a fair comparison Tapestry doesmore
Still, neck-and-neck response time in testsTapestry may be faster than Struts
ConclusionDifferences irrelevant compared to costs of EJB access, database
8/8/2019 Tapestry WebTech
44/75
JSP / Tapestry Interaction
Tapestry needs to "run the show"Needs to generate entire pageLinks should be back into TapestryCan't "include" JSP easilyCan redirect to JSP to render response
8/8/2019 Tapestry WebTech
45/75
JSP / Tapestry Interaction
Would need special taglib toreference Tapestry pagesNot practical for JSP forms to behandled using Tapestry formcomponents
Could process query parameters indeveloper code
8/8/2019 Tapestry WebTech
46/75
Lightspeed Introduction toTapestry
A fast-paced overview of basicconcepts and components.
8/8/2019 Tapestry WebTech
47/75
BasicsTapestry is highly "pluggable"; limitations canalways be worked aroundTapestry uses large numbers of objects; anduses pools and caches for efficiencyTapestry is very structured; things are usually"defined" or "declared" (often in XML)
Tapestry doesn't hide the fact that it is builton servlets and HTTP
8/8/2019 Tapestry WebTech
48/75
BasicsLeverages JavaBeans properties
Adds "property paths"Dotted name sequenceEx: "page.user.name" ==getPage().getUser().getName()
Reads and writes propertiesProperties are not type-safe
Always uses actual, not declared, type
Properties are not the same as attributesJust in time calculationSynthetic properties
8/8/2019 Tapestry WebTech
49/75
Top Down View Application = servlet + engine + pagesServlet loads application specification on initialization
Application Specification:Name of application
Java class for engineName and specification path for each page
8/8/2019 Tapestry WebTech
50/75
Top Down ViewEngine
Central "brain" for running applicationLocated or created by servlet
Servlet defers to engineEngine works with pages and componentsto process requests, render responses
8/8/2019 Tapestry WebTech
51/75
Top Down ViewPage
Has unique name (in application specification)Instantiates as particular Java class
Extends BasePage, implements IPage
Special type of component (see next)Has HTML template (may be localized)Has component specification
Lists id, type of components contained on pageSets up (binds) parameters of components
8/8/2019 Tapestry WebTech
52/75
Top Down ViewComponents
Uses component specification, just like PageMay be container or containee or bothClass extends AbstractComponent orBaseComponent, implements IComponentMay have HTML template, or may generate HTMLdirectly in code
Sets id, type, parameters of any containedcomponents
8/8/2019 Tapestry WebTech
53/75
Top Down ViewComponents
About 30 pre-defined components Various types of links
Loops, conditionalsForms, form fieldsImages, Rollovers
Bunch of special-purpose
8/8/2019 Tapestry WebTech
54/75
HTML Templates
8/8/2019 Tapestry WebTech
55/75
p
Hello, John Q. Student ,today is Jan 1 2002 .
Hello, John Q. Student ,today is Jan 1 2002 .
HTML Template
Component Specification
Hello, Conan T. Barbarian,today is June 23, 2003.
Hello, Conan T. Barbarian,today is June 23, 2003.
HTML Output
8/8/2019 Tapestry WebTech
56/75
HTML TemplatesTapestry components can wrap around text and other
components Some components replace wrapped content completely
Insert component (previous page)
Wrapped text exists as sample, for WYSIWYG editorSome components integrate wrapped text & componentswith their own template
Kind of like putting JSP include before and after
contentWorks recursively, no depth limit!
8/8/2019 Tapestry WebTech
57/75
Basic InteractionSimple examples showing the basics
Page ComponentDirect Component
Form and TextField Components
8/8/2019 Tapestry WebTech
58/75
Page ComponentCreates a link to a new pageParameters
"page", name of page to go to
"disabled", (optional) can disable link If true, link doesn't render, but content does
8/8/2019 Tapestry WebTech
59/75
Page ComponentClick here for the next page.Click here for the next page.
HTML Template
NextPage
NextPage
Component Specification
Click here for the next page.Click here for the next page.
HTML Output
8/8/2019 Tapestry WebTech
60/75
Page ComponentPage component creates the full and correct URLWrites an tag, wrapping around the text("here")Could wrap around images or other components
Could easily use dynamic, not static, target pageExample: multi-page wizardExample: page selected based on user's role
Can easily disable link; useful for locking outoptions which aren't available
8/8/2019 Tapestry WebTech
61/75
Direct ComponentCreates a link, like PageWhen clicked, a listener method , provided bythe application, is invoked by the componentDelegation patternNo limitations on listener method; can invokemethods, EJBs, etc.Default is to render the page containing thecomponent for the responseListener can easily choose a differentresponse page
8/8/2019 Tapestry WebTech
62/75
You may Enroll in
Course Name .
You may Enroll in Course Name .
HTML Template
Component Specification
Direct Component
You may Enroll in20th Century Comedy.
You may Enroll in20th Century Comedy.
HTML Output
Direct Component
8/8/2019 Tapestry WebTech
63/75
public String getCourseName(){
return ...;}
public void processEnroll(IRequestCycle cycle){
// Invoke methods to handle enrolling
cycle.setPage("EnrollConfirm");}
public String getCourseName(){
return ...;}
public void processEnroll(IRequestCycle cycle){
// Invoke methods to handle enrolling
cycle.setPage("EnrollConfirm");}
Java Code
8/8/2019 Tapestry WebTech
64/75
Direct ComponentWhat is "listeners.processEnroll"?
Little bit of JavaBeans properties magic"listeners" is a bridge between the specificationand the Java code
Scan class for certain methodsExposes those methods as propertiesLooks for
public void method (IRequestCycle)
This is how the Direct component calls back into application specific code to do stuff
8/8/2019 Tapestry WebTech
65/75
Direct ComponentNot even a whiff of a URLListener method selected new page,"EnrollConfirm", as response page
(overriding default)
Form and TextField
8/8/2019 Tapestry WebTech
66/75
ComponentsForms can be very dynamic in Tapestry
Form component must wrap around othercomponentsForm coordinates things during render and again,when form submittedForm components read properties on render,update properties when form submittedForm's listener invoked after all components have
updated their propertiesTapestry includes validation framework for textfields
Form and TextField Components
8/8/2019 Tapestry WebTech
67/75
Enter your name:
Enter your name:
HTML Template
Component Specification
Enter your name:
Enter your name:
HTML Output (Partial)
Form and TextField
8/8/2019 Tapestry WebTech
68/75
public String getName(){
return name;}
public void setName(String value){
name = value;}
public void submit(IRequestCycle){
// Do something with name, it has been set
...
// Choose a page to render responsecycle.setPage(...);
}
public String getName(){
return name;
}public void setName(String value){
name = value;}
public void submit(IRequestCycle){
// Do something with name, it has been set
...
// Choose a page to render responsecycle.setPage(...);
}
Java CodeComponents
Form and TextField
8/8/2019 Tapestry WebTech
69/75
Components Additional components for
Text areaCheckboxRadio
SelectCan get complex, with looping andconditionals inside the Form
Note: names of form, elements generatedautomaticallyEnsured to be unique
8/8/2019 Tapestry WebTech
70/75
Wrap Up
8/8/2019 Tapestry WebTech
71/75
8/8/2019 Tapestry WebTech
72/75
Other ResourcesTapestry Home Page
http://tapestry.sf.net
Tapestry Mailing List
http://lists.sourceforge.net/lists/listinfo/tapestry-developer
OnJava.comhttp://www.onjava.com/pub/a/onjava/2001/11/21
/tapestry.html
8/8/2019 Tapestry WebTech
73/75
SummaryComponents are powerful, flexible
Code generation not needed or desirableComponent object model allows much
higher complexityComplexity managed by framework, notdevelopers
Specifications are almost self-documenting
8/8/2019 Tapestry WebTech
74/75
8/8/2019 Tapestry WebTech
75/75
Summary
Less code == less bugs ==good