1. Trang chủ
  2. » Công Nghệ Thông Tin

Prentice hall core javaserver faces jun 2004 ISBN 0131463055

1,6K 77 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 1.569
Dung lượng 10,37 MB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

JavaServer Faces was designed to support drag and drop development of server-side... JavaServer Faces also comes with server-side components and an event model, which are fundamentally s

Trang 1

browsers and Web servers It also automates low-level, boring details like control flow and moving code between web forms and

business logic.

JavaServer Faces was designed to support drag and drop development of server-side

Trang 2

applications, but you can also think of it as a conceptual layer on top of servlets and

JavaServer Pages (JSP) Experienced JSP

developers will find that JavaServer Faces

provides much of the plumbing that they

currently have to implement by hand If you already use a server-side framework such as Struts, you will find that JavaServers Faces uses a similar architecture, but is more

flexible and extensible JavaServer Faces also comes with server-side components and an event model, which are fundamentally similar

Trang 3

demonstrates how to use JSF with Tiles to build consistent user interfaces

automatically

provides hints, tips, and explicit "how-to" information that allows you to quickly

become more productive

explains how to integrate JSF with

databases, use directory services, wireless apps, and Web services

teaches best practices and good habits like using style sheets and message bundles covers all of the JSF tags and how to

create new tag libraries

Trang 7

by the United States Government is subject to the restrictionsset forth in DFARS 252.227-7013 (c)(1)(ii) and FAR 52.227-19

The products described may be protected by one or more U.S.patents, foreign patents, or pending applications

TRADEMARKSHotJava, Java, Java Development Kit, J2EE, JPS,JavaServer Pages, Enterprise JavaBeans, EJB, JDBC, J2SE,

Solaris, SPARC, SunOS, and Sunsoft are trademarks of Sun

Microsystems, Inc All other products or services mentioned inthis book are the trademarks or service marks of their

respective companies or organizations

Prentice Hall PTR offers excellent discounts on this book when ordered in quantity for bulk purchases or special sales For more information, please contact U.S.

Corporate and Government Sales, 1-800-382-3419,

corpsales@pearsontechgroup.com For sales outside of the U.S., please contact International Sales, 1-317-581-

3793, international@pearsontechgroup.com

Trang 10

When we heard about JavaServer™ Faces (JSF) at the 2002Java One conference, we were very excited Both of us had

publisher, who promptly suggested that we should jointly writethe Sun Microsystems Press book on this technology

It took the JSF expert group (of which David is a member)

another two years to release the JSF 1.0 specification and

reference implementation This release fulfills many of the

original promises You really can design web user interfaces byputting components on a form and linking them to Java objects,without having to write any code at all The framework was

designed for tool support, and the first batch of drag-and-dropGUI builders is now emerging The framework is extensibleyouare not limited to the standard set of HTML components, andyou can even use completely different rendering technologies,

to support, for example, wireless devices And finally, unlikecompeting technologies that let you tumble down a deep cliffonce you step beyond the glitz, JSF supports the hard

stuffseparation of presentation and business logic, navigation,connections with external services, and configuration

management

Of course, being a 1.0 release, the current version of JSF is farfrom perfect Some of the APIs are awkward We supply you

Trang 11

features Of course, we expect the next release of JSF to

remedy many of these shortcomings

We are still excited about JSF, and we hope you will share thisexcitement when you learn how this technology makes you amore effective web application developer

Trang 12

This book is suitable for web developers whose main focus isuser interface design, as well as for programmers who

implement reusable components for web applications This is instark contrast to the official JSF specification, a dense and

The first part of the book covers these topics:

Setting up your programming environment (Chapter 1)Connecting JSF tags to application logic (Chapter 2)

Trang 13

a peek at that chapter as soon as you become comfortable withthe basics of JSF There are helpful notes on debugging andlogging, and we also give you implementation details and

working code for features that are missing from JSF 1.0, such

as file uploads, popup menus, and a pager component for longtables

JSF is built on top of servlets and JSP, but from the point of

view of the JSF developer, these technologies merely form thelow-level plumbing While it can't hurt to be familiar with otherweb technologies such as servlets, JSP, or Struts, we do notassume any such knowledge

Trang 14

All software that you need for this book is freely available Youneed the Java Software Development Kit from Sun

Microsystems, a servlet container such as Tomcat, and, of

course, a JSF implementation, such as Sun's reference

implementation The software runs identically on Linux, Mac OS

X, Solaris, and Windows We used the 1.4.2 J2SE and Tomcat5.0.19 on both Linux and Mac OS X to develop the code

examples in the book

We also expect that integrated environments will become

commercially available in the near future

Trang 15

The web page for this book is http://corejsf.com It containsThe source code for all examples in this book

Useful reference material that we felt is more effective inbrowseable form than in print

A list of known errors in the book and the code

A form for submitting corrections and suggestions

Trang 16

First and foremost, we'd like to thank Greg Doench, our editor

at Prentice Hall, who has shepherded us through this project,never losing his nerve in spite of numerous delays and

complications Thanks to Mary Lou Nohr for editing the

manuscript, and to Patti Guerrieri for her production work Wevery much appreciate our reviewers who have done a splendidjob, finding errors and suggesting improvements in variousdrafts of the manuscript They are:

Stephen Stelting, Sun Microsystems, Inc

Trang 17

Michael Yuan, author of Enterprise J2ME

Finally, thanks to our families and friends who have supported

us through this project and who share our relief that it is finallycompleted

Trang 19

Judging from the job advertisements in employment web sites,there are currently two popular techniques for developing webapplications

The "rapid development" style, using a visual developmentenvironment such as Microsoft ASP.NET

On the other hand, ASP.NET makes it easy to create attractiveuser interfaces without tedious programming Of course,

programmers want both: a high-performance back end and

easy user-interface programming

The promise of JavaServer Faces is to bring rapid user-interfacedevelopment to server-side Java

If you are familiar with client-side Java development, you canthink of JSF as "Swing for server-side applications." If you haveexperience with JavaServer Pages (JSP), you will find that JSFprovides much of the plumbing that JSP developers have to

implement by hand If you already know a server-side

framework such as Struts, you will find that JSF uses a similararchitecture

NOTE

Trang 20

in order to use this book We assume basic familiarity only with Java and HTML.

JSF has these parts:

A set of prefabricated UI components

An event-driven programming model

A component model that enables third-party developers tosupply additional components

JSF contains all the necessary code for event handling andcomponent organization Application programmers can be

blissfully ignorant of these details and spend their effort on theapplication logic

For the promise of JSF to be fully realized, we need integrateddevelopment environments that generate JSF applications As

we write this chapter, these IDEs are just beginning to be

developed For that reason, we start this tutorial chapter byshowing you how to compose a JSF application by hand Whenreading the instructions in this chapter, consider that many ofthe steps can and will be automated in the future

Trang 21

Download and unzip Tomcat, the JSF reference implementation,and the sample code Place them into directories of your choice.(As always, it is best to avoid path names with spaces, such asc:\Program Files.)

In the rest of the book, we refer to these directories as tomcat,

jsf and corejsf-examples For example, if we refer to

Trang 22

directory such as /usr/local/jakarta-tomcat-5.0.19/conf

or c:\jakarta-tomcat-5.0.19\conf

If you use Windows, you can choose a Windows installer forTomcat instead of the platform-independent ZIP file Downloadand execute the installer program When you are prompted forthe installation directory, we suggest that you use c:\tomcat

instead of the default location inside the c:\Program Filesdirectory

NOTE

You can run JSF applications with any servlet container that supports the Servlet 2.3 and JSP 1.2 specifications.

To keep the instructions simple, we cover only Tomcat 5.

If you prefer another servlet container, simply follow the standard procedure for deploying web applications on your server.

NOTE

You can also obtain JSF from other sources, such as the J2EE SDK ( http://java.sun.com/j2ee/download.html ) or the Java Web Services Development Pack

( http://java.sun.com/webservices/webservicespack.html ).

However, these bundles contain many other software components and have more complex configurations We suggest that you move on to more sophisticated

environments after becoming comfortable with Tomcat and the JSF reference implementation.

Trang 23

tomcat/bin/startup.sh

(See Figure 1-1.) On Windows, launch

tomcat\bin\startup.bat

Figure 1-1 Starting Tomcat

Trang 24

Tomcat, you can use the "Start Tomcat" menu item in the startmenu

To test that Tomcat runs properly, point your browser to

http://localhost:8080 You should see a welcome page (see

Figure 1-2)

Figure 1-2 Tomcat Welcome Page

Trang 25

tomcat/bin/shutdown.sh

or, on Windows,

Trang 26

Alternatively, if you used the Windows installer to install

Tomcat, you can shut down the running server by right-clickingthe Tomcat icon in the system tray

Trang 27

Listing 1-1 login/index.jsp

Trang 28

2 <%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>

3 <%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %> 4

Trang 30

These are JSF tags The two taglib declarations declarethe JSF tag libraries

The h:inputText, h:inputSecret, and h:commandButtontags correspond to the text field, password field, and submitbutton in Figure 1-3

The input fields are linked to object properties For

example, the attribute value="#{user.name}" tells the JSFimplementation to link the text field with the name property

of a user object We discuss this linkage in more detail later

in this chapter

When the user enters the name and password and clicks thelogin button, a welcome screen appears (see Figure 1-4)

Figure 1-4 A Welcome Screen

Trang 31

A bean that manages the user data (in our case, username

and password) A bean is simply a Java class that exposesproperties, usually by following a simple naming conventionfor the getter and setter methods The code is in the fileUserBean.javasee Listing 1-2 Note that the class is

contained inside the com.corejsf package

A configuration file for the application that lists bean

resources and navigation rules By default, this file is calledfaces-config.xml

Miscellaneous files that are needed to keep the servlet

container happy: the web.xml file and an index.html filethat redirects the user to the correct URL for the login page

More advanced JSF applications have the same structure, butthey can contain additional Java classes, such as event

handlers, validators, and custom components

Trang 33

Figure 1-5 Directory Structure of the Sample

WAR File

Trang 34

Figure 1-6 Directory Structure of the Sample

Application

Trang 35

We now walk you through the steps required for building JSFapplications with your bare hands At the end of this chapter,

Trang 37

(Note the period at the end of the command, indicating thecurrent directory.)

subdirectories) to the tomcat/webapps directory.

Remember to compile the source files in the INF/classes directory.

WEB-For more complex programs that reference the JSF libraries, thecompilation step is more complex Your class path must include

Trang 39

(On Windows, use semicolons to separate the path elements.)

Trang 40

approach is seductive since it is easy to produce simple

applications in a single file But for serious applications, mixingmarkup and code poses considerable problems Professionalweb designers know about graphics design, but they typicallyrely on tools that translate their vision into HTML They wouldcertainly not want to deal with embedded code On the otherhand, programmers are notoriously unqualified when it comes

to graphic design (The example programs in this book bearample evidence.) Thus, for designing professional web

Beans

A Java bean is a class that exposes properties and events to an environment such as JSF A property is a named value of a

given type that can be read and/or written The simplest way todefine a property is to use a standard naming convention for

Trang 41

For example, the UserBean class has two properties: name andpassword, both of type String

}

The get/set methods can carry out arbitrary actions In manycases, they simply get or set an instance field But they mightalso access a database or a JNDI directory

NOTE

Trang 42

According to the bean specification, it is legal to omit a read or write method For example, if getPassword is omitted, then password is a write-only property That might indeed be desirable for security reasons However, JSF 1.0 deals poorly with this situation For now, it is best

to give read/write access to all bean properties.

In JSF applications, you use beans for all data that needs to beaccessible from a page The beans are the conduits between theuser interface and the back end of the application

JSF Pages

You need a JSF page for each browser screen Depending onyour development environment, JSF pages typically have

extension jsp or jsf At the time of this writing, the

extension jsp requires less configuration effort when used

with Tomcat For that reason, we use the jsp extension in theexamples of this book

process sounds rather byzantine, but it is necessary to implement JSF on top of the servlet technology.

Trang 44

All JSF tags are contained inside an f:view tag

Instead of using an HTML form tag, you enclose all of theJSF components inside an h:form tag

<h:inputText value="#{user.name}"/>

You will see the definition of the user bean in the next section.The #{ } delimiters are explained in Chapter 2

When the page is displayed, the getName method is called toobtain the current property value When the page is submitted,the setName method is invoked to set the value that the user

Trang 45

5 <f:view>

Trang 46

XML-savvy readers will want to do a better job First, it is desirable to use proper XML for the tag library declarations, eliminating the <% %> tags Moreover, you will want to emit a proper DOCTYPE declaration for the generated HTML document.

The following format solves both issues:

Trang 48

in the preceding note.

Navigation

To complete our JSF application, we need to specify the

Ngày đăng: 26/03/2019, 16:09

TỪ KHÓA LIÊN QUAN