Book Image

Learning Jakarta Struts 1.2: a concise and practical tutorial

By : Stephan Wiesner
Book Image

Learning Jakarta Struts 1.2: a concise and practical tutorial

By: Stephan Wiesner

Overview of this book

<p>Jakarta Struts is an Open Source Java framework for developing web applications. By cleanly separating logic and presentation, Struts makes applications more manageable and maintainable.<br />Since its donation to the Apache Foundation&nbsp; in 2001, Struts has been rapidly accepted as the leading Java web application framework, and community support and development is well established.<br /><br />Struts-based web sites are built from the ground up to be easily modifiable and maintainable, and internationalization and flexibility of design are deeply rooted. Struts uses the Model-View-Controller design pattern to enforce a strict separation between processing logic and presentation logic, and enables efficient object re-use.<br /><br />The book is written as a structured tutorial, with each chapter building on the last. The book begins by introducing the architecture of a Struts application in terms of the Model-View-Controller pattern. Having explained how to install Jakarta and Struts, the book then goes straight into an initial implementation of the book store. The well structured code of the book store application is explained and related simply to the architectural issues.<br /><br />Custom Actions, internationalization and the possibilities offered by Taglibs are covered early to illustrate the power and flexibility inherent in the framework. The bookstore application is then enhanced in functionality and quality through the addition of logging and configuration data, and well-crafted forms. At each stage of enhancement, the design issues are laid out succinctly, then the practical implementation explained clearly. This combination of theory and practical example lays a solid understanding of both the principles and the practice of building Struts applications.</p>
Table of Contents (20 chapters)
Learning Jakarta Struts 1.2
Credits
Preface
About the Book
Glossary
Literature

Preface

It’s a long journey via instructions, Short and effective via examples.

Seneca

This book offers step-by-step instructions on the Jakarta Struts Framework as well as on building a web application based on this Open Source software. As the book progresses, we will develop a web shop: the classic book store. This web application will help us to integrate the individual components of Struts into a complete and extensive package.

The crucial point lies in the practical application and not so much in the theoretical background. This book will first demonstrate the process of developing an application accompanied by short explanations. You can then experiment with the given code.

Struts is Simple

Struts, just like Java, is fundamentally simple. Any technology becomes simple when you understand it well. You must properly solve a problem in order to thoroughly understand it. Then you examine your old code, change it a little bit, apply few classes and you’ll be glad how fast you make progress. It’s difficult only the first time—something that holds true for most things even outside computer world.

Time and again we encounter the following obstacles:

  • The same problem rarely occurs twice.

  • One must learn constantly.

  • New things are usually poorly documented.

All these reasons lead to frustration and prejudices people against trying out new things.

This book offers an entry point into Struts; it cannot possibly cover everything. To make learning this new technology easier, this book consists of numerous examples and code. It follows the "Learning by doing" philosophy and I sincerely hope that you will find it useful and engaging.

What You Should Know Beforehand

The intended audience for this book is students with some computer science knowledge and interested Java developers who want to develop a small web project. The goal is to impart as much practice-oriented knowledge as possible. This tutorial is not aimed for theoretical training and does not claim to be a complete reference book.

The book expects a certain basic familiarity with Java, JSP and servlets (uncommon elements will, of course, be explained). JSPs and servlets are Java classes and it does not make sense to try to understand them without the required knowledge as a foundation.

This book is for anyone who is looking for an entry-level book to start working with Struts. Therefore no previous knowledge of Struts is required.

It is assumed that you have a PC at your disposal and you are allowed to set up and operate a server on it. A connection to the Internet is not required. If you are using a PC at a university or office, please make sure you have the rights to install software and operate a server (eventually you’ll have to use other ports).

Operating System and Tools

Windows XP is the operating system used in this book. Users of different systems must revise path names to some extent but other than that, most key points are operating system independent.

However, Linux/UNIX users must forgive me if I don’t always differentiate strictly between upper and lower case!

The browsers used are MS Explorer version 6.x and Mozilla 1.4. The software has been developed with www.jedit.org and www.eclipse.org. I describe installation processes for any required software. All references to file paths use the defaults for these installations. If you want to use other paths, then you have to revise them accordingly.

Software Versions

The following software versions are used:

Code

The code examples aim to promote understanding and help to better explain the facts. Complete code listings as well as code fragments are given. At some places, only completed versions of code are given in the book and intermediate steps have been omitted. Full code listings are available on the book’s web site.

The individual context (see Glossary) for Tomcat is called ShopX, where X is a sequential number. This means that you can follow the development of the application and override your older versions. There are hints in the text for applying new contexts.

The code should always be well commented. I haven’t included many comments in this book because the accompanied text has detailed documentation.

In many places, I purposely employ a less-than-ideal approach. Instead of giving a long explanation of why one solution works better than the other, I simply demonstrate it: You will come across sudden problems in the text later, which could have been avoided early on. In keeping with the saying, "A person learns through mistakes", one must rewrite the code. The main drawback of this approach is that you might consider this book as a reference book. But in my opinion, you will be in a position to solve the problems by referring to the API yourself after you are done with the book, plus you can always look up certain points in the index from time to time.

The language files (see Chapter 4, Internationalization and Custom Actions) become more comprehensive as the book progresses. Their content is explicitly explained only in the first lesson. Then you can copy the versions from the big Shop-version or let it grow bit by bit. When an entry fails, Struts displays corresponding error messages. Please make yourself familiar with these messages as early on as possible. The most frequently asked questions on the Struts mailing list are related to these error messages.

I have developed the majority of the code examples myself, while other developers have come up with the rest of the examples. In such cases, the original source will be named. In the first instance, you can use the code without any restrictions. In the second case, the rights of the owner of the code may apply. Please confirm how far you are allowed to use any code before you use it.

You can download the software that accompanies this book from the Packt website as explained in the Downloading the Example Code for the Book section in the Introduction. It should help you, should you get stuck on a particular problem. Copy and paste has its own essential humble learning effects, but that does not mean that you should not try to solve problems by yourself first!

Exercises

This book contains a huge number of exercises. Most of them are fairly small, and can be solved in a few minutes. They help you to absorb the material better and should encourage you to think and scrutinize a little more deeply. You will be doing yourself a great favor if you try to solve the exercises all by yourself.

For some exercises, solutions are given in the appendix. Most of them are simply "Test yourself" exercises, because it does not make sense to present all the solutions to you on a silver platter. You should have fun while developing the software and testing it, so play with it a little bit!

Struts as Magic Bullet

I was not directly involved in development of Struts and do not have any personal or financial interest in Struts ‘taking over the world’. I regard it as a meaningful framework for developing extensive applications, but there are a few points where I would prefer different mediums and techniques. That is why I compare many different methods without favoring Struts, so that you can think over what the best solution is and form your own opinion.

It is true that no one technology represents the best solution for all applications.