Core JSF - JavaServer Faces

658 10.3K 0
Core JSF - JavaServer Faces

Đ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

JavaServer Faces (JSF) là công nghệ Java EE tiêu chuẩn để xây dựng giao diện người dùng web. Nó cung cấp một khuôn khổ mạnh mẽ cho việc phát triển các ứng dụng phía máy chủ, cho phép bạn riêng biệt trình bày trực quan và logic ứng dụng. JSF 2.0 là một nâng cấp lớn, không chỉ bổ sung thêm nhiều tính năng hữu ích mà còn làm đơn giản hóa mô hình lập trình bằng cách sử dụng các chú thích và "quy ước về cấu hình" cho công việc chung.Để giúp bạn nhanh chóng khai thác sức mạnh của JSF 2.0, phiên bản thứ ba của Core ™ JavaServer Faces đã được hoàn toàn cập nhật để sử dụng tối ưu của tất cả các tính năng mới. Cuốn sách bao gồmBa chương hoàn toàn mới về việc sử dụng Facelets thẻ cho khuôn mẫu , xây dựng các thành phần tổng hợp, và phát triển các ứng dụng Ajax - Hướng dẫn xây dựng các ứng dụng mạnh mẽ với tay mã hóa tối thiểu và tối đa năng suất - mà không cần bất kỳ kiến ​​thức của servlet hoặc cấp thấp khác " ống nước " - Một lời giải thích đầy đủ về cơ bản xây dựng các khối từ việc sử dụng thẻ JSF tiêu chuẩn, để làm việc với các bảng dữ liệu, và chuyển đổi và xác nhận đầu vào - Bảo hiểm của nhiệm vụ tiên tiến , chẳng hạn như xử lý sự kiện , mở rộng khuôn khổ JSF, và kết nối với các dịch vụ bên ngoài - Giải pháp cho một loạt các thách thức chung , bao gồm ghi chú trên gỡ lỗi và xử lý sự cố , ngoài chi tiết thực hiện và mã làm việc cho các tính năng mà bị thiếu từ JSF - Các giải pháp đã được chứng minh , gợi ý , thủ thuật , và "làm thế nào - TOS " chỉ cho bạn cách sử dụng JSF có hiệu quả trong các dự án phát triển của bạnJavaServer Faces lõi ™ , phiên bản thứ ba , cung cấp mọi thứ bạn cần để làm chủ các tính năng mạnh mẽ và tiết kiệm thời gian của JSF 2.0 và hướng dẫn hoàn hảo cho các lập trình phát triển ứng dụng Java EE 6 web trên Glassfish hoặc một Java EE máy chủ ứng dụng trong 6 tuân thủ, như cũng như vận động viên chạy servlet như Tomcat 6

ptg From the Library of Wow! eBook ptg FACELETS PAGE LAYOUT TEXT FIELD page.xhtml WEB-INF/classes/com/corejsf/SampleBean.java BUTTON page.xhtml WEB-INF/classes/com/corejsf/SampleBean.java The outcomes success and error can be mapped to pages in faces-config.xml. If no mapping is specified, the page /success.xhtml or /error.xhtml is displayed. GET REQUESTS Request parameters set bean properties before the page is rendered. The getContinueOutcome method is called when the button is rendered. The view parameters are added to the request URL. RADIO BUTTONS page.xhtml WEB-INF/classes/com/corejsf/SampleBean.java WEB-INF/classes/com/corejsf/Condiment.java CONVERSION The number is displayed with currency symbol and group separator: $1,000.00 VALIDATION Using the bean validation framework (JSR 303) Page-level validation and conversion Error messages RESOURCES page.xhtml resources/css/styles.css <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:f="http://java.sun.com/jsf/core" xmlns:h="http://java.sun.com/jsf/html" xmlns:ui="http://java.sun.com/jsf/facelets"> <h:head> </h:head> <h:body> <h:form> </h:form> </h:body> </html> <h:inputText value="#{bean1.luckyNumber}"> @Named("bean1") // or @ManagedBean(name="bean1") @SessionScoped public class SampleBean { public int getLuckyNumber() { } public void setLuckyNumber(int value) { } } <h:commandButton value="press me" action="#{bean1.login}"/> public class SampleBean { public String login() { if ( ) return "success"; else return "error"; } } <f:metadata> <f:viewParam name="item" value="#{bean1.currentItem}"/> <f:viewParam name="userId" value="#{bean1.user}"/> </f:metadata> <h:button value="Continue" outcome="#{bean1.continueOutcome}" includeViewParams="true"/> <h:selectOneRadio value="#{bean1.condiment}> <f:selectItems value="#{bean1.choices}" var="it" itemLabel="#{it.description}" itemValue="#{it.productId}"/> </h:selectOneRadio> public class SampleBean { public Collection<Condiment> getChoices() { } public int getCondiment() { } public void setCondiment(int value) { } } public class Condiment { public String getDescription() { } public int getProductId() { } } <h:outputText value="#{bean1.amount}"> <f:convertNumber type="currency"/> </h:outputText> public class SampleBean { @Max(1000) private BigDecimal amount; } <h:inputText value="#{bean1.amount}" required="true"> <f:validateDoubleRange maximum="1000"/> </h:inputText> Amount <h:inputText id="amt" label="Amount" value="#{bean1.amount}"/> <h:message for="amt"/> <h:outputStylesheet library="css" name="styles.css"/> <h:message for="amt" errorClass="errors"> .errors { font-style: italic; color: red; } From the Library of Wow! eBook ptg core JAVASERVER ™ FACES THIRD EDITION From the Library of Wow! eBook ptg This page intentionally left blank From the Library of Wow! eBook ptg DAVID GEARY CAY HORSTMANN Upper Saddle River, NJ • Boston • Indianapolis • San Francisco New York • Toronto • Montreal • London • Munich • Paris • Madrid Capetown • Sydney • Tokyo • Singapore • Mexico City core JAVASERVER ™ FACES THIRD EDITION From the Library of Wow! eBook ptg Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and the publisher was aware of a trademark claim, the designations have been printed with initial capital letters or in all capitals. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their respective owners. The authors and publisher have taken care in the preparation of this book, but make no expressed or implied warranty of any kind and assume no responsibility for errors or omissions. No liability is assumed for incidental or consequential damages in connection with or arising out of the use of the information or programs contained herein. This document is provided for information purposes only and the contents hereof are subject to change without notice. This doc- ument is not warranted to be error-free, nor subject to any other warranties or conditions, whether expressed orally or implied in law, including implied warranties and conditions of merchantability or fitness for a particular purpose. We specifically dis- claim any liability with respect to this document and no contractual obligations are formed either directly or indirectly by this document. This document may not be reproduced or transmitted in any form or by any means, electronic or mechanical, for any purpose, without our prior written permission. The publisher offers excellent discounts on this book when ordered in quantity for bulk purchases or special sales, which may include electronic versions and/or custom covers and content particular to your business, training goals, marketing focus, and branding interests. For more information, please contact: U.S. Corporate and Government Sales (800) 382-3419 corpsales@pearsontechgroup.com For sales outside the United States, please contact: International Sales international@pearsoned.com Visit us on the Web: informit.com/ph Library of Congress Cataloging-in-Publication Data Geary, David M. Core JavaServer faces / David Geary, Cay Horstmann.—3rd ed. p. cm. Includes index. ISBN 978-0-13-701289-3 (pbk. : alk. paper) 1. JavaServer pages. 2. Web site development. 3. Web sites—Design. I. Horstmann, Cay S., 1959- II. Title. TK5105.8885.J38G433 2010 006.7'8—dc22 2010011569 Copyright © 2010, Oracle and/or its affiliates. All rights reserved. 500 Oracle Parkway, Redwood Shores, CA 94065 All rights reserved. Printed in the United States of America. This publication is protected by copyright, and permission must be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permissions, write to: Pearson Education, Inc. Rights and Contracts Department 501 Boylston Street, Suite 900 Boston, MA 02116 Fax: (617) 671-3447 ISBN-13: 978-0-13-701289-3 ISBN-10: 0-13-701289-6 Text prin te d i n t he U nited Sta te s o n re cy cl ed p ap er a t E dw ard s B rot he rs i n A nn Ar bo r, Mich ig an . First printing, May 2010 From the Library of Wow! eBook ptg v Cont ents Contents Preface xv Acknowledgments xix 1 GETTING STARTED 2 Why JavaServer Faces? 3 A Simple Example 4 Ingredients 7 Directory Structure 8 Building a JSF Application 9 Deploying a JSF Application 11 Development Environments for JSF 13 An Analysis of the Sample Application 15 Beans 16 JSF Pages 17 Servlet Configuration 19 A First Glimpse of Ajax 21 JSF Framework Services 24 Behind the Scenes 26 Rendering Pages 27 From the Library of Wow! eBook ptg Contents vi Decoding Requests 28 The Life Cycle 29 Conclusion 31 2 MANAGED BEANS 32 Definition of a Bean 33 Bean Properties 36 Value Expressions 37 Backing Beans 38 CDI Beans 39 Message Bundles 40 Messages with Variable Parts 42 Setting the Application Locale 43 A Sample Application 45 Bean Scopes 51 Session Scope 52 Request Scope 53 Application Scope 54 Conversation Scope 54 View Scope 55 Custom Scopes 56 Configuring Beans 56 Injecting CDI Beans 56 Injecting Managed Beans 57 Bean Life Cycle Annotations 58 Configuring Managed Beans with XML 58 The Expression Language Syntax 63 Lvalue and Rvalue Modes 63 Using Brackets 64 Map and List Expressions 65 Calling Methods and Functions 66 Resolving the Initial Term 67 Composite Expressions 69 From the Library of Wow! eBook ptg Contents vii Method Expressions 70 Method Expression Parameters 71 Conclusion 71 3 NAVIGATION 72 Static Navigation 73 Dynamic Navigation 74 Mapping Outcomes to View IDs 75 The JavaQuiz Application 77 Redirection 86 Redirection and the Flash 87 RESTful Navigation and Bookmarkable URLs 88 View Parameters 89 GET Request Links 90 Specifying Request Parameters 91 Adding Bookmarkable Links to the Quiz Application 92 Advanced Navigation Rules 96 Wildcards 97 Using from-action 98 Conditional Navigation Cases 99 Dynamic Target View IDs 99 Conclusion 99 4 STANDARD JSF TAGS 100 An Overview of the JSF Core Tags 102 Attributes, Parameters, and Facets 104 An Overview of the JSF HTML Tags 105 Common Attributes 107 Panels 115 The Head, Body, and Form Tags 118 Form Elements and JavaScript 120 Text Fields and Text Areas 123 Hidden Fields 127 From the Library of Wow! eBook ptg Contents viii Using Text Fields and Text Areas 127 Displaying Text and Images 131 Buttons and Links 134 Using Buttons 136 Using Command Links 141 Selection Tags 145 Checkboxes and Radio Buttons 148 Menus and Listboxes 151 Items 153 Messages 171 Conclusion 177 5 FACELETS 178 Facelets Tags 179 Templating with Facelets 181 Building Pages from Common Templates 183 Organizing Your Views 187 Decorators 193 Parameters 195 Custom Tags 195 Components and Fragments 198 Loose Ends 198 <ui:debug> 198 <ui:remove> 200 Handling Whitespace 202 Conclusion 202 6 DATA TABLES 204 The Data Table Tag—h:dataTable 205 A Simple Table 207 h:dataTable Attributes 210 h:column Attributes 211 Headers, Footers, and Captions 212 From the Library of Wow! eBook [...]... com\corejsf\UserBean.java If you use Tomcat, use the following command to compile your code: javac -d / /web/WEB-INF/classes -classpath :jsf- ref-impl/lib /jsf- api.jar com/corejsf/UserBean.java 6 If you use Tomcat, you need to include the JSF libraries: mkdir / /web/WEB-INF/lib cp jsf- ref-impl/lib/*.jar / /web/WEB-INF/lib Skip this step if you use a Java EE 6 compliant application server 7 Run the following... / /web/WEB-INF/classes On Windows, use backslashes as file separators 5 If you use GlassFish, run javac -d / /web/WEB-INF/classes -classpath :glassfish/modules/\* com/corejsf/UserBean.java From the Library of Wow! eBook A Simple Example 11 On Windows, use a semicolon in the classpath, and don’t escape the * wildcard: javac -d \ \web\WEB-INF\classes -classpath ;glassfish\modules\* com\corejsf\UserBean.java... open-source frameworks Nearly all of the original authors of the aforementioned frameworks participated on the JSF 2 Expert Group, so JSF 2.0, unlike JSF 1.0, was forged from the crucible of real-world open-source projects that had time to mature JSF 2.0 is much simpler to use and better integrated into the Java EE technology stack than JSF 1.0 Almost every inch of JSF 1.0 has been transformed in JSF. .. the “stack trace from hell” JSF had one saving grace, however It was highly extensible, and therefore it was very attractive to framework developers Those framework developers xv From the Library of Wow! eBook xvi Preface built cutting edge open-source software that plugged into JSF, such as Facelets, Ajax 4jsf, Seam, JSF Templates, Pretty Faces, RichFaces, ICEFaces, and so on JSF 2.0, released in 2009,... tools when developing a JSF application? 604 How do I use Scala with JSF? 605 How do I use Groovy with JSF? 607 Conclusion 608 Index 609 From the Library of Wow! eBook Preface When we heard about JavaServer Faces (JSF) at the 2002 JavaOne conference, we were very excited Both of us had extensive experience with client-side Java programming—David in Graphic Java™, and Cay in Core Java™, both published... had to declare beans in a file WEB-INF/facesconfig.xml This is no longer necessary, and this application does not need a faces- config.xml file More advanced JSF applications have the same structure, but they can contain additional Java classes, such as event handlers, validators, and custom components Additional configuration parameters can be placed in a file WEB-INF/facesconfig.xml that we will describe... completed From the Library of Wow! eBook core JAVASERVER FACES ™ THIRD EDITION From the Library of Wow! eBook GETTING STARTED Topics in This Chapter • “Why JavaServer Faces? ” on page 3 • “A Simple Example” on page 4 • “Development Environments for JSF on page 13 • “An Analysis of the Sample Application” on page 15 • “A First Glimpse of Ajax” on page 21 • JSF Framework Services” on page 24 • “Behind... Chapter 1 Why JavaServer Faces? Nowadays, you can choose among many frameworks for developing the user interface of a web application JavaServer Faces (JSF) is a component-based framework For example, if you want to display a table with rows and columns, you do not generate HTML tags for rows and cells in a loop, but you add a table component to a page (If you are familiar with client-side Java development,... focus is on implementing user interfaces and business logic This is in stark contrast to the official JSF specification, a dense and pompously worded document whose principal audience is framework implementors, as well as long-suffering book authors JSF is built on top of servlets, but from the point of view of the JSF developer, this technology merely forms the low-level plumbing While it can't hurt... chapter For a simple application, this file is not required Listing 1–3 login/src/java/com/corejsf/UserBean.java 1 package com.corejsf; 2 3 import java.io.Serializable; 4 import javax.inject.Named; 5 // or import javax .faces. bean.ManagedBean; 6 import javax.enterprise.context.SessionScoped; 7 // or import javax .faces. bean.SessionScoped; 8 9 @Named("user") // or @ManagedBean(name="user") 10 @SessionScoped

Ngày đăng: 07/03/2014, 23:26

Mục lục

    Building a JSF Application

    Deploying a JSF Application

    Development Environments for JSF

    An Analysis of the Sample Application

    Definition of a Bean

    Messages with Variable Parts

    Setting the Application Locale

    Injecting CDI Beans: CDI

    Bean Life Cycle Annotations

    Configuring Managed Beans with XML

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan