Book Image

Grails 1.1 Web Application Development

By : Jon Dickinson
Book Image

Grails 1.1 Web Application Development

By: Jon Dickinson

Overview of this book

Web development is trickyóeven a simple web application has a number of context changes ready to trip up the unwary. Grails takes the everyday pain out of web application development, allowing us to focus on delivering real application logic and create seamless experiences that will address the needs of our users. This book will take the pain out of Grails by showing you exactly how to build a web application with a minimum of fuss. With this book, even if you are new to Grails, you will be up and running before you know it. You will be able to code faster and your code will be better. This clear and concise book is packed with examples and clear instructions to help you build your first Grails application and gives you the skills to speed up your application development by adding a different angle for learning about the topic. After a brief introduction to the dynamic JVM-based Groovy programming language, which teaches you enough about Groovy to understand the relationship between Grails and the Groovy scripting language, it shows how to use Grails and a number of key plug-ins to deliver valuable web applications. It also takes you through creating, developing, testing, and deploying an example team collaboration application in Grails. Using an incremental and iterative approach you will learn how to build a basic web application with secure authentication and different levels of authorization. You will learn how to handle file upload allowing users to share files. Some advanced features of object-oriented persistence will be introduced through adding tags for messages and files to giving users a robust categorization system. You will then build on the basic application to enhance the user experience through AJAX and the RichUI plug-in. You will take a further step into the world of Web 2.0 by adding an RSS feed and a REST service to the application. Once the entire application is up and running, you will learn how to create your own plug-in for tagging. Finally, you will learn how to deploy this application to a production environment.
Table of Contents (20 chapters)
Grails 1.1 Web Application Development
Credits
About the author
Acknowledgement
About the reviewers
Preface

Preface

The expectations of our users are increasing, and rightly so. The Internet is no longer the playground of geeks and nerds. Businesses use it to communicate with and support their customers; families use it to keep in touch while whole communities share their experiences with like-minded people. The democratization of the Internet has brought a new wave of software into the lives of people who would otherwise rarely use computers. The most successful of the new generation of web applications have not been written for the expert user, but for the casual user, focusing on ease of use. Web application development frameworks that focus on developer productivity have improved the ability of developers to respond to the demands of their users. Simpler and more intuitive frameworks have allowed the rapid development and refinement of new features.

Java web application development has something of a checkered history; simple isn't it. There may be more than one way to skin a cat, but there are almost infinite numbers of ways to build a Java web application. The options that are available are mind-boggling. Which database server to use? What about the application server? You also better choose an MVC framework while you're at it. Should you use a persistence framework, or hand code SQL statements? If you use an ORM framework, which one is best? Don't forget to choose a logging library. How should the project be laid out? Once you've finished making all these decisions, it's time to start on the configuration to get all of these disparate frameworks working together. Now, eventually you are ready to start coding! No wonder the latest and greatest web applications are built in PHP and Ruby on Rails.

Java still has a lot to offer. It is faster than most other languages that are used for web application development. It is an extremely mature platform, with lots of high quality libraries available. Moreover, its static, strong typing gives you less rope to hang yourself with. However, Java developers need to find technologies that deal with the common activities of web development. Sometimes we need a few choices taken away to help us focus on the problem at hand, creating great software that provides value to our users at less cost to the people paying the bills.

Grails does just this. It removes the need for reams of configuration through a convention-based approach that constrains developers in a healthy way.The decisions concerning project layout and which frameworks to use are removed.This leaves the developers free to use their creative talents for producing great software, rather than tweaking configuration files.

Throughout this book, you will learn how to build a web application using Grails and a number of key plug-ins. You will see that it is possible to achieve a great deal with very little code. Who knows, you may even rediscover your passion for web development on the Java platform!

What this book covers

Chapter 1 presents a short state of the nation of Java web development and makes the case for a framework like Grails. At the end of the chapter, we will install and create a Grails project.

Chapter 2 covers the use of Grails scaffolding to generate some simple pages to manage users and roles for our application.

Chapter 3 shows how to post messages, where we write the first basic functionality for the application by allowing users to post messages that can be shared with other users. This chapter introduces a number of basic concepts for Grails development including: controllers, validation, Groovy Server Pages (GSP), and Grails Object-Relational Mapping (GORM).

Chapter 4 covers an introduction to Groovy. Here we take a short break from the Grails framework to get a better understanding of the Groovy programming language. We will cover just enough of the language to be able to proceed through the rest of the book.

Chapter 5 shows how to use our first external plug-in to add authentication and authorization to the application.

Chapter 6 covers testing, where we introduce the different levels of automated testing that are available in the Grails framework. We see how to write, unit tests with new support for testing in Grails 1.1. We also cover integration tests, and install a functional testing plug-in.

Chapter 7 covers file sharing, where we allow users to share files through the application by introducing file uploads.

Chapter 8 covers some advanced querying techniques, using Hibernate criteria support in GORM, to implement file version history.

Chapter 9 introduces Grails services in more depth. We see how to extract logic from our controllers into services to keep the application maintainable.

Chapter 10 introduces more advanced GORM techniques, such as: persisting inheritance and performing polymorphic queries to enable tagging. We also delve into GSP a bit more by using templates to encapsulate view components.

Chapter 11 covers AJAX and RIA Frameworks — Where we improve the user experience with AJAX to allow users to edit tags in-line and use the RichUI plug-in to create tag clouds and perform auto suggestion when editing tags.

Chapter 12 shows us how to use the Searchable plug-in to add a search feature to our site in a matter of minutes. We also provide an RSS feed and a REST based API for managing messages.

Chapter 13 show us how to build our own plug-in, where we follow the example of the Grails plug-in community and extract our tagging code into a plug-in that we can use on future projects.

Chapter 14 shows how to package and deploy the application to a production ready for use in a production environment. We then discuss some next steps that may be worth investigating to handle real world situations.

What you need for this book

To implement the example code in this book, you will need the Java SDK 5 or above. More importantly, you will need to have some experience of web development on the Java platform.

Who this book is for

This book is aimed at Java web developers looking for ways to speed up development of web applications on the Java platform. If you are frustrated with integrating the many different frameworks that are available for web development and want to get on with building slick web applications for your users, then this book is for you.

Grails is built on the Groovy language, but experience in Groovy is not required, as you will learn enough about Groovy to understand how to use Grails.

Conventions

In this book, you will find a number of styles of text that distinguish between different kinds of information. Here are some examples of these styles, and an explanation of their meaning.

Code words in text are shown as follows: "Create the saveNewVersion method to link the usage of the createNewVersion and applyNewVersion methods."

A block of code will be set as follows:

def saveNewVersion( params, multipartFile ) {
def version = createVersionFile( params, multipartFile )
def file = applyNewVersion( params.fileId, version )
file.save()
return file;
}

When we wish to draw your attention to a particular part of a code block, the relevant lines or items will be shown in bold:

package app
class FileController {
def fileService
def save = {
def multipartFile = request.getFile(‘fileData.data')

Any command-line input or output is written as follows:

# grails create-domain-class app.User

New terms and important words are shown in bold. Words that you see on the screen, in menus or dialog boxes for example, appear in our text like this: "clicking the Next button moves you to the next screen".

Note

Warnings or important notes appear in a box like this.

Note

Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book — what you liked or may have disliked. Reader feedback is important for us to develop titles that you really get the most out of.

To send us general feedback, simply drop an email to , and 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 .

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.

Customer support

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.

Downloading the example code for the book

Visit http://www.packtpub.com/files/code/6682_Code.zip to directly download the example code.

The downloadable files contain instructions on how to use them.

Errata

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 so, you can save other readers from frustration, and help us to improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/support, selecting your book, clicking on the let us know link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata added to any list of existing errata. Any existing errata can be viewed by selecting your title from http://www.packtpub.com/support.

Piracy

Piracy of copyright material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at with a link to the suspected pirated material.

We appreciate your help in protecting our authors, and our ability to bring you valuable content.

Questions

You can contact us at if you are having a problem with any aspect of the book, and we will do our best to address it.