Book Image

Apache MyFaces 1.2 Web Application Development

Book Image

Apache MyFaces 1.2 Web Application Development

Overview of this book

Hypes and trends (such as Web 2.0) cause a change in the requirements for user interfaces every now and then. While a lot of frameworks are capable of meeting those changing requirements, it often means you as a developer need in-depth knowledge of web standards, such as XHTML and JavaScript. A framework like Apache MyFaces that hides all details of how the page is rendered at the client and at the same time offers a rich set of tools and building blocks could save you a lot of time, not only when you're building a brand new application but also when you're adapting an existing application to meet new user interface requirements.This book will teach you everything you need to know to build appealing web interfaces with Apache MyFaces and maintain your code in a pragmatic way. It describes all the steps that are involved in building a user interface with Apache MyFaces. This includes building templates and composition components with Facelets, using all sorts of specialized components from the Tomahawk, Trinidad, and Tobago component sets and adding validation with MyFaces Extensions Validator.The book uses a step-by-step approach and contains a lot of tips based on experience of the MyFaces libraries in real-world projects. Throughout the book an example scenario is used to work towards a fully functional application when the book is finished.This step-by-step guide will help you to build a fully functional and powerful application.
Table of Contents (22 chapters)
Apache MyFaces 1.2
Credits
About the Author
Acknowledgement
About the Reviewers
Preface
Trinidad Tags
Trinidad Text Keys
Default JSF Error Messages
ExtVal Default Error Messages

Preface

Hypes and trends (such as Web 2.0) cause a change in the requirements for user interfaces every now and then. Although a lot of frameworks are capable of meeting these changing requirements, they often mean that you, as a developer, need in-depth knowledge of web standards, such as XHTML and JavaScript. Apache MyFaces hides all of the details of how the page is rendered at the client, and at the same time offers a rich set of tools and building blocks. This can save you a lot of time not only when you’re building a brand-new application, but also when you’re adapting an existing application to meet new user interface requirements.

This book will teach you everything that you need to know in order to build appealing web interfaces with Apache MyFaces, and to maintain your code in a pragmatic way. It describes all of the steps that are involved in building a user interface with Apache MyFaces. This includes building templates and composition components with Facelets, and using all sorts of specialized components from the Tomahawk and Trinidad component sets. Adding validation with MyFaces Extensions Validator as well as using MyFaces Orchestra to manage transactions in a page flow, are also covered.

Unlike comparable books, this book not only introduces Facelets as an alternative to JSP, but actually uses Facelets in all the examples throughout this book. This makes the book a valuable resource for Facelets examples. The book also shows how various components of the MyFaces project can be used together, in order to deliver the functionality of the new JSF 2.0 standard, in current projects, without the need to upgrade your project to JSF 2.0.

This book uses a step-by-step approach, and contains a lot of tips based on experience of the MyFaces libraries in real-world projects. Throughout the book, an example scenario is used to work towards a fully-functional application by the end of this book.

This step-by-step guide will help you to build a fully-functional and powerful application.

What this book covers

Chapter 1, Introduction, introduces the Apache MyFaces project and all of its subprojects. Forward references to other chapters are given wherever applicable.

Chapter 2, Getting Started, discusses downloading and installing the MyFaces libraries. The set-up of two specific IDEs is discussed, as well as the set-up of an application server for testing. This chapter also covers the use of Maven and the Maven artifacts that are provided by the MyFaces project.

Chapter 3, Facelets, covers the installation of Facelets into our project. It discusses the benefits of Facelets over JavaServer Pages as a view technology for JavaServer Faces. This chapter also introduces the most important features of Facelets. By the end of the chapter, we have created a layout template that we can use throughout the book, when developing our application. We will also have learned the basic Facelets techniques that we will use in all examples throughout the book.

Chapter 4, Tomahawk, looks at the Tomahawk component set that is a part of MyFaces. Some of the most important components from the set are covered, and we will learn how we can use these in an optimal way, in combination with Facelets. This chapter gives us enough information to build fully-functional JSF pages by using Tomahawk components.

Chapter 5, Trinidad—the Basics, is the first of three chapters covering MyFaces Trinidad. This chapter introduces a lot of Trinidad components, including the data input and output components. Special attention is given to the many layout components that are available in the Trinidad library. As with Tomahawk, we will see how we can get the most out of the combination of Trinidad and Facelets.

Chapter 6, Advanced Trinidad, introduces some more advanced features of the Trinidad library. This includes the charting component that can be used to easily create nice looking charts. Also, Trinidad’s page flow scope feature, which enables us to create page flows more easily, is introduced. This chapter also discusses the AJAX or Partial Page Rendering capabilities of Trinidad, including client-side validation and conversion. The Trinidad dialog framework is also covered.

Chapter 7, Trinidad Skinning and Tuning, is an introduction to the advanced skinning framework that is a part of Trinidad. This chapter also discusses the most important tuning parameters of Trinidad.

Chapter 8, Integrating with the Backend, discusses how we can integrate the frontend that we created with some backend system, in a standard way. This chapter gives us some basic knowledge about the Model-View-Controller architecture, and about important standards such as Enterprise Java Beans (EJB) and the Java Persistence API (JPA). We will use the knowledge and examples from this chapter as a starting point for the more advanced integration topics discussed in the subsequent chapters.

Chapter 9, MyFaces Orchestra, introduces the MyFaces Orchestra library. This chapter starts with a very brief introduction to the Spring framework, as Orchestra is based on parts of that framework. We see how we can create a Spring application context and then how we should use such a context in combination with Orchestra. Some key concepts of Orchestra are introduced, such as the Orchestra ViewController concept and the concept of conversations. This chapter concludes with a quick view of Orchestra’s DynaForm component.

Chapter 10, Extensions Validator, is about one of the latest additions to the MyFaces project: the Extensions Validator, or ExtVal for short. This chapter starts by teaching us how to configure our project to use ExtVal. We see how JPA annotations can be used to automatically generate JSF validations. This chapter also shows us the extra annotations that ExtVal offers to complement the JPA annotations. This chapter also shows how we can use Bean Validation (JSR 303) annotations as an alternative to JPA and ExtVal annotations. As a whole, this chapter is a good introduction to this very flexible and versatile member of the MyFaces family.

Chapter 11, Best Practices, is the last chapter of this book. It discusses some best practices with JSF in general and MyFaces in particular. This chapter describes a way to prevent direct access to page definitions, as well as a way to enable container-based security in our JSF application. This chapter also shows how to create a login page by using JSF components, and discusses how to use component bindings wisely. This chapter concludes by discussing how to save the state of request-scoped components in an elegant way.

Appendix A, XHTML Entities, lists all of the numeric entities that can be used in XML documents. This list may be needed because Facelets files must be valid, plain XML files, and can’t contain named entities that can be used in normal XHTML files.

Appendix B, Trinidad Tags, gives a list of all of the tags from the Trinidad library. This can be referred to if you forget the exact name of one of the many tags. It can also be used to determine if a certain Trinidad tag is a naming container.

Appendix C, Trinidad Text Keys, lists the keys that Trinidad uses to lookup the default texts that are displayed on components. These keys can be used to customize or translate the default texts.

Appendix D, Default JSF Error Messages, gives an overview of the default error messages that are defined in the JSF standard. This list can be used if you want to customize or translate the default messages. This may also be useful in combination with the MyFaces Extensions Validator.

Appendix E, ExtVal Default Error Messages, contains a list of the default error messages that are defined by the Extensions Validator for use with its custom validation annotations. This can be useful if you want to override the default messages.

Note

The Appnedices are available for free at http://www.packtpub.com/files/3254-Appendices.pdf.

What you need for this book

In order to be able to build the example application that is used throughout this book, you should have a Java development environment. Typically this means that you need an Integrated Development Environment (IDE), but only a Java Development Toolkit (JDK, at least 1.5.x) and your favorite text editor may also be enough. To test the application, a Java EE 5 compliant application server is also needed. It is suggested that you use a development environment and application server that you’re familiar with. However, you may find it useful to know that the author used Eclipse Galileo (3.5) with the Web Tools Platform enabled when writing this book and creating the example application. The example application was tested using a GlassFish (v2) application server. This is all open source software that can be freely downloaded from the Internet. Chapter 2 gives some more details about the required tools.

Who this book is for

This book is intended for Java developers who want to develop web frontends for their enterprise applications, by using Apache MyFaces as a JSF implementation. Some basic knowledge of JavaServer Faces (JSF) is expected.

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: "There are two important configuration files for a JSF application—web.xml and faces-config.xml ."

A block of code is set as follows:

<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>
javax.faces.webapp.FacesServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

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

<sun-web-app>
<security-role-mapping>
<role-name>user</role-name>
<group-name>miasusers</group-name>
</security-role-mapping>

<class-loader delegate="false"/>
<property name="useMyFaces" value="true"/>
</sun-web-app>

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

connect 'jdbc:derby://localhost:1527/test;create=true';

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 the text like this: "Under the Web Tier node, we select the JSF node and then on the right-hand side, we select JSF Page and click on OK ."

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 send an e-mail to , and mention the book title via 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 e-mail .

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book on, 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.

Note

Downloading the example code for the book

Visit http://www.packtpub.com/files/code/3254_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 content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the 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 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 will be uploaded on our website, or added to any list of existing errata, under the Errata section of that title. 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.