GWT in Practice GWT in Practice ROBERT COOPER CHARLES COLLINS MANNING Greenwich (74° w. long.) For online information and ordering of this and other Manning books, please visit www.manning.com. The publisher offers discounts on this book when ordered in quantity. For more information, please contact: Special Sales Department Manning Publications Co. Sound View Court 3B Fax: (609) 877-8256 Greenwich, CT 06830 email: [email protected] ©2008 by Manning Publications Co. All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by means electronic, mechanical, photocopying, or otherwise, without prior written permission of the publisher. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in the book, and Manning Publications was aware of a trademark claim, the designations have been printed in initial caps or all caps. Recognizing the importance of preserving what has been written, it is Manning’s policy to have the books we publish printed on acid-free paper, and we exert our best efforts to that end. Recognizing also our responsibility to conserve the resources of our planet, Manning books are printed on paper that is at least 15% recycled and processed elemental chlorine-free Manning Publications Co. Copyeditor: Andy Carroll Sound View Court 3B Typesetters: Denis Dalinnik Greenwich, CT 06830 Cover designer: Leslie Haimes ISBN 1-933988-29-0 Printed in the United States of America 1 2 3 4 5 6 7 8 9 10 – VHG – 12 11 10 09 08 contents preface xi acknowledgments xii about this book xiii about the cover illustration xvii PART 1 GETTING STARTED..............................................1 1 Introducing GWT 3 1.1 Why GWT 5 History 5 ■ Why Ajax matters 6 ■ Leveraging the web 7 Tooling and testing 7 ■ A single code base 8 ■ Limitations 8 1.2 What GWT includes 9 GWT compiler 9 ■ User Interface layer 10 ■ Remote Procedure Calls 10 ■ Additional utilities 11 ■ GWT shell 12 1.3 GWT basics 13 Modules and inheritance 13 ■ Host pages 14 Entry point classes 16 1.4 Working with the GWT shell 16 The logging console 17 ■ The hosted mode browser 18 v vi CONTENTS 1.5 Understanding the GWT compiler 19 JavaScript output style 20 ■ Additional compiler nuances 23 The compiler lifecycle 24 1.6 Summary 30 2 A New Kind of Client 32 2.1 Basic project structure and components 33 Generating a project 34 ■ The standard directory structure 35 GWT starting point files 36 ■ Host pages 37 ■ Modules 38 Entry points 40 2.2 Design patterns and GWT 40 MVC and GWT 41 ■ Creating a widget 42 ■ Communicating by observing events 46 ■ Operator strategy 48 ■ Controlling the action 51 2.3 Styling a GWT component 54 Providing a CSS file 54 ■ Connecting style names with Java 56 2.4 Running a completed project 56 Hosted mode and the GWT shell 57 ■ Web mode and the GWT compiler 58 2.5 Summary 59 3 Communicating with the Server 61 3.1 Making GWT Remote Procedure Calls 62 Starting the HelloServer project 62 ■ Defining GWT serializable data 64 ■ Creating RPC services 66 Expanding on RemoteServiceServlet 69 ■ Calling the server from the client 70 ■ Troubleshooting server communication 74 3.2 The development server—Tomcat Lite 75 The web.xml file 75 ■ The context.xml file 77 3.3 Using an external development server 79 3.4 Summary 80 PART 2 TASK-SPECIFIC ISSUES........................................83 4 Core Application Structure 85 4.1 Building a model 86 CONTENTS vii 4.2 Building view components 90 Extending widgets 90 ■ Extending composite 93 Binding to the model with events 95 4.3 The controller and service 98 Creating a simple controller 99 ■ JPA-enabling the model 100 Creating a JPA-enabled service 104 4.4 Summary 106 5 Other Techniques for Talking to Servers 107 5.1 Web development methods and security 108 Dealing with browser security 108 ■ Understanding XMLHttpRequest 110 ■ Coding asynchronously 110 Developing GWT applications in NetBeans 111 5.2 Enabling REST and POX communications 112 Making basic HTTP requests with GWT 112 ■ Making advanced HTTP requests with GWT 114 ■ Working with XML 115 5.3 Understanding Java-to-JavaScript interaction 116 Using GWT JavaDoc annotations to serialize collections 116 Using JSON 119 5.4 Creating a cross-domain SOAP client with Flash 121 Using Flash as a SOAP client 121 ■ Setting a Flash security context 130 ■ Drawbacks and caveats 131 5.5 Incorporating applets with GWT 131 Using Java as a SOAP client 131 ■ Signing JARs for security bypass 136 5.6 Streaming to the browser with Comet 137 5.7 Summary 147 6 Integrating Legacy and Third-Party Ajax Libraries 148 6.1 A closer look at JSNI 149 JSNI basics revisited 149 ■ Potential JSNI pitfalls 151 Configuring IntelliJ IDEA 153 6.2 Wrapping JavaScript libraries 155 Creating a JavaScript module 156 ■ Creating wrapper classes 156 ■ Using the wrapped packages 159 viii CONTENTS 6.3 Managing GWT-JavaScript interaction 162 Maintaining lookups 162 ■ Daisy-chaining Java listeners into JavaScript closures 166 ■ Maintaining listeners in Java 168 ■ Conversion between Java and JavaScript 172 6.4 Wrapping JavaScript with GWT-API-Interop 178 6.5 Summary 181 7 Building, Packaging, and Deploying 183 7.1 Packaging GWT modules 184 Building and packaging modules 184 ■ Sharing modules 186 7.2 Building and deploying applications 187 The client side 188 ■ The server side 188 ■ Manually building a WAR file 189 7.3 Automating the build 191 Extending the Ant build 191 ■ Using Maven 195 7.4 Managing Tomcat Lite from the build 205 7.5 Summary 210 8 Testing and Continuous Integration 211 8.1 GWT testing 212 Knowing what to test 212 ■ How GWT testing works 213 Testing gotchas 214 ■ Basic GWT tests 217 ■ Testing outside of GWT 224 8.2 Advanced testing concepts 226 Benchmarking 227 ■ Remote testing 229 Code coverage 231 ■ Coverage in an automated build 234 8.3 Continuous integration 239 Adding a GWT project to Hudson 240 8.4 Summary 245 PART 3 FULLY FORMED APPLICATIONS.........................247 9 Java Enterprise Reinvented 249 9.1 Constructing two models 251 9.2 Mapping to DTOs 257 9.3 Wiring applications with Spring 260 CONTENTS ix 9.4 Constructing the client application 265 The controller and global model 266 ■ The basic CRUD wrapper 269 ■ The BookEdit widget 272 9.5 Summary 279 10 Building the Storefront 281 10.1 Securing GWT applications 282 10.2 Building a drag-and-drop system 289 Enabling dragging 290 ■ Handling drops 293 10.3 JSNI special effects 296 10.4 Summary 299 11 Managing Application State 300 11.1 Overview of the sample application 301 11.2 Creating a basic messaging service 304 11.3 Handling messages on the client and server 310 Messages and CometEvents 310 ■ Streaming messages to the client 312 ■ Receiving images 315 11.4 Recording and playing back conversations 317 Capturing changes to the model layer 320 ■ Handling deep links 325 ■ When to use hyperlinks rather than history 326 11.5 Dealing with state on the server side 327 11.6 Adding a UI and cleaning up 330 Displaying events 330 ■ Sending events 331 Cleaning up 333 11.7 Summary 334 appendix A Notable GWT Projects 335 appendix B Quick Reference 338 index 351
Description: