31,19 €
In Detail
This book is a practical step-by-step tutorial for those who want to build contemporary, real-life web applications with Tapestry 5, the Apache open-source framework for creating dynamic, robust, highly scalable web applications in Java. It shows the path of least resistance, so that the reader can learn all the essential skills quickly and easily. To give the reader an initial practical experience, a simple but useful web application is built throughout the chapters.
Who this book is for
This book is for those who want to build sophisticated Java web applications quickly and easily. It assumes that the reader is reasonably comfortable with the Java programming language, but no knowledge of web technologies is needed. For experienced Servlet, JSP, or Struts developers, the book will show an alternative way that will allow them to raise their productivity to an incomparable level. With this book you will see that a contemporary component-based framework can be easy to learn and a pleasure to work with.
Das E-Book können Sie in Legimi-Apps oder einer beliebigen App lesen, die das folgende Format unterstützen:
Seitenzahl: 301
Veröffentlichungsjahr: 2008
Copyright © 2007 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, Packt Publishing, nor its dealers or distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.
First published: January 2008
Production Reference: 1090108
Published by Packt Publishing Ltd.
32 Lincoln Road
Olton
Birmingham, B27 6PA, UK.
ISBN 978-1-847193-07-0
www.packtpub.com
Cover Image by Nilesh R. Mohite (<[email protected]>)
Author
Alexander Kolesnikov
Reviewers
Howard Lewis Ship
Massimo Lusetti
Acquisition Editor
Viraj Joshi
Technical Editor
Sarvesh Shanbhag
Editorial Team Leader
Mithil Kulkarni
Project Manager
Abhijeet Deobhakta
Project Coordinator
Abhijeet Deobhakta
Indexer
Monica Ajmera
Proofreader
Angie Butcher
Production Coordinator
Shantanu Zagade
Cover Work
Shantanu Zagade
Alexander Kolesnikov is an author and software developer from Greenock, Scotland. He wrote his first program in FORTRAN back in 1979 for a computer that occupied several rooms. He currently works as a Java Web Developer for CIGNA International. A Soviet military researcher in the past, he recently graduated as a Master of Science with Distinction in Enterprise Systems Development from Glasgow Caledonian University and has also gained a number of professional certifications from Sun Microsystems (SCJP, SCWCD, SCBCD). His first book on software development was "Java Drawing With Apache Batik" (BrainySoftware, 2007).
He is interested in many things, ranging from the most recent web technologies to alternative medicine and wishes wholeheartedly that a day was at least three times longer than it is.
I am grateful to the people who made my Tapestry quest happen, and thanks to whom it goes on and on: Howard Lewis Ship, the creator of Tapestry, with special thanks for finding time to review this book; Brian Hainey, my tutor at Glasgow Caledonian, for suggesting Tapestry as a topic for my MSc dissertation; Ian McClure, my manager at CIGNA, who bravely accepted Tapestry as the framework for our next major project, and Viraj Joshi from Packt Publishing whose insightfulness made this book possible.
Howard M. Lewis Ship is the creator of Tapestry, and a leading authority on Java development for the enterprise. He’s worked at a handful of software and consulting companies in the greater Boston area before becoming an independent consultant in 2003, specializing in Tapestry development and training. Howard now lives in Portland, Oregon with his wife Suzanne, a novelist. Howard has helped review a number of books, and wrote his own book in 2003, Tapestry in Action, covering Tapestry 3. He currently works for Formos Software Development in Vancouver, Washington as a Tapestry Architect and Evangelist.
Massimo Lusetti is a 33 year old IT passionate. He started off playing with Spectrum 80 home computer and having fun with Basic and Z80 registers. Before becoming a Java and Unix addicted developer, he gained some experience with different operating systems. He used OS/2 Warp and Rexx till IBM abandoned it, then started using Linux (1.x version) in 1996, lately in 2000 he discover the BSD world and never looked back.
He started working with Java in 1998 just when Java Swing was released with Java 1.2. Since then he adopted Java for various things—from GUI applications to distributed ones. Besides programming, he developed an interest in Unix programming—particularly in security issues.
In 2000 he founded his own company DataCode SRL which delivers custom software solutions.
Tapestry is an open-source framework for creating dynamic, robust, and highly scalable web applications in Java. It divides a web application into a set of pages, each constructed using components. It provides various features such as, allowing the Tapestry framework to assume responsibility for key concerns such as user input validation, localization/internationalization, and exception reporting. It involves creating HTML templates using plain HTML, and combining the templates with small amounts of Java code using (optional) XML descriptor files. Tapestry is designed to handle anything from tiny applications all the way up to massive applications consisting of hundreds of individual pages, developed by large, diverse teams.
Chapter 1 looks at the overall features of Tapestry and tells you why Tapestry is special. It talks about the place of Tapestry in the world of Java web development. It also shows the journey from Tapestry 3 to the current Tapestry 5.
Chapter 2 deals with creating a working environment. To develop a Tapestry application, you need to download and install the Java Development Kit (JDK), a project management tool like Maven, and an Integrated Development Environment (IDE) like NetBeans or Eclipse. It also gives a brief description of the procedure to to follow when creating a Tapestry project.
Chapter 3 describes the various features of Tapestry like page templates and page classes, expansions, components, and so on. It actually makes you comfortable working with Tapestry, creating a bare-bones project, handling expansions and components, navigating between pages, and creating and using Application State Objects (ASOs).
Chapter 4 examines some of the components in Tapestry like TextField, PasswordField, Label, PageLink, ActionLink, Loop, If, Output, RadioGroup and Radio, Checkbox, Submit and Select. These are simple components which map almost directly to HTML controls and elements. These are normally used on every page of a Tapestry web application and help you in creating a functionally-rich user interface.
Chapter 5 examines some of the more advanced components such as Grid, BeanEditForm, DateField and FCKEditor. Using these, you can build some functionally-rich user interfaces. You can easily implement repeating patterns like tables and dates on a web page.
Chapter 6 covers that feature of Tapestry 5 which prevents a user to input erroneous data on a web page. This feature, which is called user input validation, not only identifies the erroneous field and marks it in some way, but may also explain why it is erroneous.
Chapter 7 examines internationalization and localization. These are used to make a web application multi-lingual and display it in different locales.
Chapter 8 deals with creating a custom component to enable reusability of code by way of packaging repeating patterns into one single custom component.
Appendix A takes you through the basics of Java for developing web applications. It examines the standard structure of a Java web application, the basics of deployment descriptor and WAR files, and Java enabled services.
Appendix B tells you how to use db4o to create a real data source. It allows you to store even the most complex object structures with ease, while achieving the highest level of performance.
Appendix C gives you some references for obtaining extra information about Tapestry 5 and earlier versions.
This book is for those who want to build sophisticated Java web applications quickly and easily. It assumes that the reader is reasonably comfortable with the Java programming language, but no knowledge of web technologies is needed. For experienced Servlet, JSP, or Struts developers, the book will show an alternative way that will allow them to raise their productivity to an incomparable level. With this book you will see that a contemporary component-based framework can be easy to learn and a pleasure to work with.
Feedback from our readers is always welcome. Let us know what you think about this book, what you liked or disliked. Reader feedback is important in order for us to develop titles that you get the most out of.
To send us general feedback, simply send an email to <[email protected]>, making sure to mention the book title in the subject of your message.
If there is a book that you need and would like to see us publish, please send us a note in the SUGGEST A TITLE form on www.packtpub.com or email <[email protected]>.
If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide on www.packtpub.com/authors.
Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.
Visit http://www.packtpub.com/support, and select this book from the list of titles to download any example code or extra resources for this book. The files available for download will then be displayed.
The downloadable files contain instructions on how to use them.
Although we have taken every care to ensure the accuracy of our contents, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in text or code—we would be grateful if you would report this to us. By doing this you can save other readers from frustration, and help to improve subsequent versions of this book. If you find any errata, report them by visiting http://www.packtpub.com/support, selecting your book, clicking on the Submit Errata link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata are added to the list of existing errata. The existing errata can be viewed by selecting your title from http://www.packtpub.com/support.
You can contact us at <[email protected]> if you are having a problem with some aspect of the book, and we will do our best to address it.
I think we may be on to something here.
You see, Tapestry has been around, in one form or another, for about eight years now. Eight years. A lot can happen in eight years. I've gotten married, relocated across country, written a book, worked for several companies, and consulted at dozens more. I've made many new friends, especially that raucous pirate crew that speaks at the No Fluff Just Stuff software symposiums. I've criss-crossed the country dozens of times, always with a laptop in hand, always tinkering away at the Tapestry source code. Meanwhile, we've seen waves of Java hype and anti-hype, the coming of Ruby and Rails, the growth of the Java language in ways most welcome (and some less so), and the new breed of JVM languages such as Groovy, JRuby and Scala. In fact, the only constants for me over the last eight years have been my wife Suzanne, and Tapestry.
I've been feeling something building. We're in a new age of coding now, an age in which the reach of an individual coder has been greatly extended. There's a universe of Java code libraries waiting to be downloaded and used. Java's cross platform capabilities mean we can develop and test on a personal computer or laptop and then push the exact same binaries to a server for production. If you can't appreciate how wonderful that is, you may be missing the forest for the trees. We've taken Java's strengths for granted and focused on its weaknesses, and too many are waiting with baited breath for The Next Big Thing.
I don't think there's going to be one Next Big Thing. Java's arrival was a Next Big Thing, and though it's unlikely to be the Last Next Big Thing, the rapid rise of Java was a special event, a coincidence of timing that could only happen by riding in the wake of the first introduction of the Internet and the World Wide Web. But I think there's going to be a lot of Next Little Things and we're all free to combine a few of those into our own personal Next Big Thing.
I think Tapestry is destined to be one of the key Next Little Things, one that's going to really change how we all do our jobs. Many a Next Thing is an experiment waiting to be proved valid. Tapestry went through that stage nearly eight years ago, but now benefits from all those years of experience: not just my personal involvement, but the experience of the rest of the Tapestry developers, and the experience of the Tapestry user community. It's got a proven track record for its basic concepts, its governing principles, and its overall approach, even as the code base has been revitalized (ok, rewritten) for Tapestry 5.
But code, design, architecture ... that's all technology, and the best technology in the universe doesn't guarantee a win. Community is what defines success, a concept well known (if poorly applied) in the open source world, and slowly dawning in the proprietary world. That's where Alexander comes in. Community is best expressed when individuals do ridiculous things for the benefit of others in the community. For example, writing a fourteen (fourteen!) part on-line tutorial about Tapestry 4, and now following up with this book on Tapestry 5.
I think you'll see that Alexander has a real handle on what Tapestry is, and more importantly, how best to use it. Tapestry isn't a Swiss Army knife or even a toolbox; it's an entire workshop for creating web applications in Java. This book is a great guided tour of that workshop, showing where all the bits and pieces are stored and how they can be combined to get things done. Dive in, get smart and start having fun with Tapestry!
Howard M. Lewis Ship
Jan 2, 2008
Portland, Oregon
Programming for the Web is very rewarding. Your application, once deployed, becomes available to millions of people, and it doesn't matter which the kind of computers they are using. If they have a reasonably new web browser, they have a good chance of being able to fully appreciate your creation, whether they are running Windows, Linux, Mac OS, or UNIX on their workstation or hand held device.
To become a web developer, one would normally need to be intimately familiar with the strange world of HTTP protocol—requests and responses flying across the globe, special places for storing information like session or application context, and so on. In short, one would have to go to a much lower level of software development.
This change of level is especially striking for those who have had some experience with a Rapid Application Development (RAD) environment, like Borland Delphi or Microsoft Visual Basic. When using such an environment, it seems so natural that when a button is pressed on a form the desktop application is immediately ready to do something in response to the user's action and all you need to do as a programmer is to provide some code that will run in response.
However, when it comes to web development, a developer needs to keep in mind a long chain of low level processes that run before and after he or she is able to do something useful in response to a button press on a web page. Is this a GET or a POST request? How do we extract useful information from the request? How do we store this information? How do we create a response?
Working at that level means a lot of time is spent in solving relatively simple problems. As web applications become more and more popular it becomes obvious that a more efficient, higher level approach to web development is needed.
Nowadays, several solutions are offered to solve this problem on different software development platforms. In the world of Java, the most efficient and elegant solution is presented by Apache Tapestry. Tapestry is an open-source Java web framework, conceived by Howard Lewis Ship and currently developed by a team of devoted Java pundits from around the world.
Tapestry is an example of a framework that was created keeping the developer in mind.
First of all, its paradigm is very close to that of the RAD environment. A button on the Tapestry page has an event handler method associated with it in a declarative way, and the method is invoked when the button is pressed. The Tapestry web developer doesn't need to remember that the button on the page in a user's web browser and the code of the event handler method on the web server can be thousands miles apart, and doesn't need to care about which communication protocol is being used.
This natural approach makes Tapestry significantly easier to learn than any other web development framework, and it makes the process of development much more efficient. However, there are a number of other attractive features, the combination of which makes Tapestry unique.
Every web application has to deal with HTML in one or another way. No matter what language is used on the server or what intricate logic is employed to produce the page, what actually gets sent to the user's browser is an HTML page. To make that page dynamic, web applications use some kind of HTML template, or specially prepared chunks of HTML markup, and employ some programming logic to fill that template with dynamically generated data, or manipulate HTML in a desired way.
To make this possible, virtually all web frameworks create a tight mixture of HTML markup and some other kind of code, whether it is Java, PHP, VBScript, JSP tags, JSF, ASP.NET components, or something else. Such an approach has a number of significant disadvantages.
First of all, the more complicated the page design and programming logic, the more difficult it is to weave them together. Second, this tight conglomeration of different kinds of code is extremely difficult to maintain and update. Debugging the programming logic of such a page can be quite a challenge.
Contemporary technologies like Sun's JavaServer Faces or Microsoft's ASP.NET rely on specialized development environments to handle the complexities of mixing a dynamic logic with HTML markup, but design capabilities of such specialized environments are unavoidably limited. As a result, the page design remains quite simplistic and less impressive. Code that is automatically generated by these development environments is usually far from being perfect as it has much duplication and is hard to maintain.
For many applications, it often makes sense to have a special team of designers or to use the services of an external team in order to have a professional looking interface. Unfortunately, in most web technologies the conversion of a thoroughly designed page into an HTML template is a one way procedure. The result becomes incomprehensible for any designer not familiar with programming techniques. So when it comes to a significant change in page design, it might be easier to discard the existing page and start from scratch.
Tapestry solves all these problems in the most elegant way. In Tapestry 3 and 4, templates are valid HTML documents that can be easily read and edited by any designer using a common designer's software, without breaking the logic of the page. In Tapestry 5, templates are XML documents, but they are very close to HTML. With minor reservations, all the advantages of the previous versions apply to them too. The main point is that all versions of Tapestry provide the highest possible level of separation between HTML markup and programming logic.
The use of components allows you to significantly increase the productivity of web development—this is why all the new frameworks, including Tapestry, JavaServer Faces, and ASP.NET are component-based.
However, the choice of core components that come with the framework is always limited, and it is important to give developers an opportunity to create their own custom components. Naturally, such an opportunity is provided by all component-based frameworks, but the level of effort required to create a custom component differs from framework to framework quite significantly.
For instance, to create a custom JavaServer Faces component, the developer needs to know the intricate inner structure of the framework quite well, so this is a task for advanced developers only.
In Tapestry, however, creation of custom components does not require any advanced knowledge, and is perfectly possible for beginners. We will be creating non-trivial custom components in Chapter 8 of this book.
Contemporary web applications are unthinkable without attractive and clever features powered by JavaScript and are commonly known as DHTML and AJAX. However, JavaScript has browser incompatibilities. So Tapestry comes up with a number of components that make the miracles of AJAX available to the application, but, fortunately for developers, all JavaScript is thoroughly hidden inside the framework. AJAX-enabled components are as easy to use as any other Tapestry component, so you can make your application Web 2.0-ready without ever seeing a single line of JavaScript code. Well, at least this is true for the previous version of the framework, 4.1. As of this writing, the AJAX features are currently under development (and will likely be in a finalized state by the time you read this), but it is promised that they will be even more powerful and easier to use than in Tapestry 4.1.
User input validation is a must for almost every web application, as users are mere mortals and will always try to submit some erroneous data. A friendly application should clearly explain to the user exactly which piece of input is wrong, but creating an input validation and error reporting subsystem can require quite a lot of work.
Tapestry 4 already had a powerful built-in infrastructure for input validation, but in Tapestry 5 it goes much further. You as a developer will have to do very little, usually simply declaring what exactly you want, while your application will not only clearly mark the erroneous fields, but also automatically display an appropriate message in the language preferred by the current user.
A Java platform itself provides substantial support for internationalization and localization but, in Tapestry we do not need to know the details of that support. We just use a few simple hooks provided by the framework to have our application displayed in as many different languages as we wish.
It is actually quite amazing to see how one mouse click totally changes the language of the application. Everything—text, images, different controls, suddenly begin to speak in a foreign language! It is an impressive feature, considering how little we have to do to enable this flexibility.
Inversion of Control (IoC) and Dependency Injection are amongst the most popular software development paradigms these days, and they deserve their popularity. In simple terms, Inversion of Control allows developers to save effort and time by allowing some other software to take care of different routine things and provide them services whenever needed.
However, to use the benefits of Inversion of Control in an application, one might need to learn and use an additional framework like Spring. Not in Tapestry though.
Tapestry has an Inversion of Control subsystem of its own, and quite a good one. As a result, Tapestry applications contain very little Java code. All you need to write is business logic while all the infrastructure issues are handled automatically. Aditionally, the Tapestry 5 IoC container additionally provides an opportunity to implement and enforce some of the best design patterns, again, with absolute minimum code serving this purpose.
Spring framework is very popular, and working on a large-scale enterprise application, we often need to make use of some sort of back end, implemented in Spring. Thankfully, Tapestry has a special subsystem for integration with Spring. Using this subsystem in my work, I had an impression that it is easier to use Spring beans in Tapestry than in Spring itself.
More or less the same can be said about integration with another very popular framework, Hibernate. There is a special subsystem in Tapestry responsible for this integration and it is very easy to use.
To summarize, Tapestry is a mature contemporary web development framework with all the features that would be expected from such a framework and with all the power of Java platform to support it. But additionally, Tapestry is based on a unique, developer-friendly paradigm, which makes it especially easy to learn and use.
Java technology is traditionally strong on the server side. However, in the last couple of years one might notice some kind of stagnation in the area of Java web development. To explain what I mean, I will need to show you a brief history of Java web development.
In the first stage of web development, it was typical to have some code that a web server could invoke to produce dynamic content. The traditional solution is to have a program written in Perl or C and to invoke it through a CGI interface. The Java solution was different however, and that solution was rather advanced for its time. Java code was running on the server as a servlet, a kind of plug-in maintained by a servlet container.
The servlet way was superior because it ensured higher scalability, but perhaps even more importantly from the developer's point of view, a servlet developer could spend more time writing application-specific code, since many of the lower level issues were handled by the servlet container. At that time, Java technology was ahead of the rest of the web development world.
The next generation of web technology was about embedding pieces of code into an HTML page in order to make it dynamic. You can see this approach in JSP, ASP, PHP and ColdFusion.
Again, Java technology was ahead, because JavaServer Pages took special care about separation between the application code and its presentation logic. Ideologically fortified by Model-View-Controller design pattern, this approach culminated in Apache Struts framework.
Basically, a Struts application is a traditional servlet/JSP application, but this framework offered a number of efficient solutions for common problems of web development. Struts developers could avoid reinventing the wheel and reuse existing solutions by modifying them with application-specific code. This significantly increased the efficiency of large-scale web development, and Struts framework became extremely popular, once again ensuring the leading position of Java technologies.
However, around the same time Microsoft came up with an alternative technological solution, ASP.NET. This was a component-based approach which was supported by a sophisticated integrated development environment (IDE), Visual Studio, ASP.NET resembles RAD solutions that brought desktop development to a new level of productivity some years ago. An ASP.NET developer can drop a few components on the page, set their properties in a visual property editor and run the application—all in just a few seconds.
This is when the feeling appeared that the Java approach is not quite the best anymore. ASP.NET was quickly gaining popularity, while the mainstream Java approach was exemplified by a Struts application, not component-based, and was still quite a low-level solution.
The Java world tried to retaliate by creating JavaServer Faces specification. Externally, it resembles ASP.NET, but is built on top of the existing Java-specific solutions.
JSF is supported and promoted by major software vendors like IBM, Oracle, and BEA, and it's implemented in their state-of-the-art commercial IDEs. Its approach was very similar to the Microsoft approach, but somehow JavaServer Faces didn't gain the popularity it was expected to gain. Maybe that is because to create the JSF specification, so many different parties had to come to a compromise.
Or maybe the problem is in the fact that the JavaServer Faces specification is created using existing Java solutions and has inherited their weaknesses. For example, JavaServer Pages technology, used by JSF to render pages, was very sound for its time, but a JSP page can easily become an unintelligible mixture of HTML, standard tags, custom tags, and EL expressions. Also, JSF has some older features from Struts, like its navigation configured in a lengthy XML file. As a result, JSF technology is difficult to comprehend, learn and work.
Or perhaps the problem with JSF is that it mimics so closely the Microsoft solution, while the Java world has its own laws, and something that is good for Microsoft might not be equally good here?
My point is that if we look at Tapestry, the framework that was never supported or promoted by billionaire corporations, we might notice that it can easily compete with the most recent versions of ASP.NET in many respects.
Features like code-behind in ASP.NET that allows the separation of code of the page from its template, is an integral part of Tapestry from the very beginning of the framework. In fact, in Tapestry you cannot just mix code and HTML since all Java code goes into the page class.
In fact, many solutions that are promoted as advanced features of ASP.NET can be found either in the core Tapestry framework or in a library of custom Tapestry components, such as Tacos.
It may well happen that with time, as more people realize the benefits of Tapestry, it might become the model for the next generation of a Java web framework specification.
However, nothing is perfect, and Tapestry has its downsides too. One of them is the fact that the framework's development goes ahead in huge steps, which makes the next version very different from the previous one. To fully appreciate what this means, let's have a look at a brief history of Tapestry.
Tapestry was originally conceived by Howard Lewis Ship around the year 1999, but the early versions of the framework are not widely known.
Tapestry 3 became the first well known version, and I used it for my first Tapestry application that I created in the summer of 2005. Tapestry was a great relief after my experience with servlets and JavaServer Pages. Its approach was very easy for me to understand, maybe because in the past I had plenty of experience with Borland Delphi, a very popular Rapid Application Development environment.
However, some features were not fully implemented in Tapestry 3. Most notably the validation subsystem was somewhat deficient. Also, some obvious components were missing, and I had to create a number of custom components myself (although that wasn't too difficult, frankly speaking). All in all, I was very happy that I had learned Tapestry, and I was going to use it for years.
However, when Tapestry 4 appeared in January 2006, it was so different that I had to learn the framework again. It was not a problem though as Tapestry 4 had streamlined and made many features easier than in the previous version. Most significantly, Tapestry 4 was built on top of Hivemind, another Open Source project created by Howard Lewis Ship. Hivemind made it possible to easily extend and configure Tapestry, but most notably, it allowed users to inject anything into the page, as Hivemind was (and is) quite a capable Inversion of Control container. Also, Tapestry introduced a brand new user input validation subsystem, flexible and easy to use.
On a negative note, Tapestry 4 was not compatible with Tapestry 3, and upgrading an application to the new version required a substantial amount of effort.
Tapestry 4.1 became another major step in the development of the framework. It came with Dojo JavaScript toolkit built into it, and as a result it made AJAX functionality easily available to Tapestry developers. It looks like version 4.1 will stay here for a while and allow many people to benefit from using it for building their web applications. It still has a few places where it could be streamlined or enhanced, but, it is a powerful and developer friendly framework.
