Mô tả:
Table of Contents Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ix 1. Setup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 Requirements 1 Installing Tomcat 2 Installing Ant 3 2. JavaScript for Ajax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 Creating the Application 4 Running the Example 11 3. A Simple Ajax Servlet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 Building and Deploying the Ajax Application 16 Running the Example 17 4. XML and JSON for Ajax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 The Character Decoder 19 Setting Up a Simple XML Document 20 Back on the Client: Mining the XML 28 Building the Application 34 Running the Application on Tomcat 36 Passing Data with JSON 37 Summary 40 5. Getting Useful Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 Form Entry with Ajax 41 Building a Suggestion Field 50 6. Ajax Libraries and Toolkits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62 Using the Dojo Toolkit 63 Using the Rico Toolkit 69 Using DWR with Ajax 75 Drag ’n’ Drop with Scriptaculous and Prototype 80 7. Ajax Tags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99 Creating a Tag Library 99 Third-Party Tag Libraries 110 8. Ajax on Struts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135 Struts-Layout 135 Adding Ajax to Struts with DWR 148 Ajax with Struts: What Have We Learned Here? 162 9. JavaServer Faces and Ajax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163 The JSF Lifecycle 164 Writing a Custom JSF Component 165 Developing a Custom JSF Tag 169 Handling JSF Input by Extending HtmlInputText 177 Writing the JSF Support for Ajax 179 Summary 182 10. Google Web Toolkit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183 Getting Started with GWT 183 Debugging the Application 188 Fleshing Out the Application: The Client 191 Supplying Services to the Client 196 Testing ZipCodes with the Service 201 GWT Widgets 205 Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209 Preface1 “This is cool, look!” I told a group of coworkers. “What is it?” one of them asked. “It’s Google Maps, and it uses Ajax,” I said. “What’s Ajax?” “It stands for Asynchronous JavaScript and XML. It allows a request from a web page to go to the server, get data, and display it without the user hitting submit and waiting for a page refresh.” “Wow, that could give my application the responsiveness of a desktop application!” Until now, the choice for web developers has been between thin-client web applica tions and rich applications that require installs. With Ajax, you can build web appli cations that have the responsiveness of rich applications, without the overhead of keeping the end user up-to-date with the latest software. This is truly a great oppor tunity for web developers to write more responsive applications. Ajax: Some History In the beginning there was HTML, and the world saw that it was good. Soon after came web applications, and the world was overjoyed with the ability to interact with data. There were search engines, online bill paying services, stock trading sites, inter active games, online shopping facilities, and much, much more. So what’s missing from the world of web applications? The answer is responsive ness. Back in 1984, I experienced my first real intuitive interaction with a computer. I was in college, and in the dorm was a study lab that had just been equipped with Apple Computer, Inc.’s new product: the Macintosh. These computers had a defi nite wow effect on the students. There was only one program, MacWrite, but that was enough for me. I was immediately sold on the friendly, easy-to-use experience that the MacWrite application gave me, as were many other students. Until recently, browser-based web applications haven’t been able to deliver the kind of experience users expect from desktop applications. Sure, some web applications do it with a rich client. But rich clients require overhead not present in browser based applications. For ease of deployment and of keeping users current with the lat est version, nothing beats a browser-based application. What would be ideal would be a browser-based application with a rich-client feel. Meet Ajax. Some of you probably know that Ajax technology has been around for a while, and that it wasn’t always called Ajax. The term Ajax (Asynchronous JavaScript and XML) was coined by Jesse James Garrett of Adaptive Path in his article “Ajax: A New Approach to Web Applications” (http://www.adaptivepath.com/publications/ essays/archives/000385.php). After that article appeared, there were plenty of com ments about the fact that the approach wasn’t really “new”; many developers were creating asynchronous applications before XMLHttpRequest became available. Java applets, despite their shortcomings, could deliver web applications that felt more like desktop applications. So could Flash applications. So what’s changed? What’s the big deal? Well, now we at least have a name for the practice. That may not seem like much, but it gives us common ground to discuss it. Just as design patterns give us names to use when discussing programming tech niques, the name Ajax instantly tells us which web programming technique is being used. Since Garrett’s article was published, there has been much discussion of how to use Ajax and of its capabilities and shortcomings. The appearance of articles, tools, and information relating to Ajax has lead to an explosion of interest. As information about Ajax becomes more and more widely available, Ajax techniques and usage will become more mainstream and will come to be expected by the users of the web applications we write. That is the power of a name. Ajax narrows the gap between a rich client application and a thin, browser-based cli ent application. This book will introduce you to Ajax by illustrating how to create Ajax applications in a server-side Java environment: how to add Ajax features to servlet-based applications, JSPs, JSF applications, and so on. So, join me in this exciting endeavor: let’s strive to make our web applications more interactive, less boring, and more efficient by avoiding redundant data entry and long wait times between page loads—in short, to create a user experience closer to that of a real desktop application. These are some of the promises of Ajax technology.