Movatterモバイル変換


[0]ホーム

URL:


Document Information

Preface

Part I Introduction

1.  Overview

2.  Using the Tutorial Examples

Part II The Web Tier

3.  Getting Started with Web Applications

4.  Java Servlet Technology

5.  JavaServer Pages Technology

6.  JavaServer Pages Documents

7.  JavaServer Pages Standard Tag Library

8.  Custom Tags in JSP Pages

9.  Scripting in JSP Pages

10.  JavaServer Faces Technology

11.  Using JavaServer Faces Technology in JSP Pages

12.  Developing with JavaServer Faces Technology

13.  Creating Custom UI Components

Determining Whether You Need a Custom Component or Renderer

When to Use a Custom Component

When to Use a Custom Renderer

Component, Renderer, and Tag Combinations

Understanding the Image Map Example

Why Use JavaServer Faces Technology to Implement an Image Map?

Understanding the Rendered HTML

Understanding the JSP Page

Configuring Model Data

Summary of the Application Classes

Steps for Creating a Custom Component

Creating Custom Component Classes

Specifying the Component Family

Performing Encoding

Performing Decoding

Enabling Component Properties to Accept Expressions

Saving and Restoring State

Delegating Rendering to a Renderer

Creating the Renderer Class

Identifying the Renderer Type

Handling Events for Custom Components

Creating the Component Tag Handler

Retrieving the Component Type

Setting Component Property Values

Getting the Attribute Values

Setting the Component Property Values

Providing the Renderer Type

Releasing Resources

Defining the Custom Component Tag in a Tag Library Descriptor

14.  Configuring JavaServer Faces Applications

15.  Internationalizing and Localizing Web Applications

Part III Web Services

16.  Building Web Services with JAX-WS

17.  Binding between XML Schema and Java Classes

18.  Streaming API for XML

19.  SOAP with Attachments API for Java

Part IV Enterprise Beans

20.  Enterprise Beans

21.  Getting Started with Enterprise Beans

22.  Session Bean Examples

23.  A Message-Driven Bean Example

Part V Persistence

24.  Introduction to the Java Persistence API

25.  Persistence in the Web Tier

26.  Persistence in the EJB Tier

27.  The Java Persistence Query Language

Part VI Services

28.  Introduction to Security in the Java EE Platform

29.  Securing Java EE Applications

30.  Securing Web Applications

31.  The Java Message Service API

32.  Java EE Examples Using the JMS API

33.  Transactions

34.  Resource Connections

35.  Connector Architecture

Part VII Case Studies

36.  The Coffee Break Application

37.  The Duke's Bank Application

Part VIII Appendixes

A.  Java Encoding Schemes

B.  About the Authors

Index

 

The Java EE 5 Tutorial

Java Coffee Cup logo
PreviousContentsNext

Delegating Rendering to a Renderer

BothMapComponent andAreaComponent delegate all of their rendering to a separate renderer.The sectionPerforming Encoding explains howMapRenderer performs the encoding forMapComponent. This sectionexplains in detail the process of delegating rendering to a renderer usingAreaRenderer,which performs the rendering forAreaComponent.

To delegate rendering, you perform these tasks:

Creating the Renderer Class

When delegating rendering to a renderer, you can delegate all encoding and decodingto the renderer, or you can choose to do part of itin the component class. TheAreaComponent class delegates encoding to theAreaRenderer class.

To perform the rendering forAreaComponent,AreaRenderer must implement anencodeEnd method. TheencodeEndmethod ofAreaRenderer retrieves the shape, coordinates, and alternative text values stored in theImageArea bean that is bound toAreaComponent. Suppose that thearea tag currently beingrendered has avalue attribute value of"fraA". The following line fromencodeEnd gets the value of the attribute"fraA" from theFacesContext instance.

ImageArea ia = (ImageArea)area.getValue();

The attribute value is theImageArea bean instance, which contains theshape,coords,andalt values associated with thefraAAreaComponent instance.Configuring Model Data describes how theapplication stores these values.

After retrieving theImageArea object, it renders the values forshape,coords, andaltby simply calling the associated accessor methods and passing the returned values totheResponseWriter instance, as shown by these lines of code, which write outthe shape and coordinates:

writer.startElement("area", area);writer.writeAttribute("alt", iarea.getAlt(), "alt");writer.writeAttribute("coords", iarea.getCoords(), "coords");writer.writeAttribute("shape", iarea.getShape(), "shape");

TheencodeEnd method also renders the JavaScript for theonmouseout,onmouseover, andonclickattributes. The page author need only provide the path to the images thatare to be loaded during anonmouseover oronmouseout action:

<bookstore:area value="#{fraA}"     onmouseover="/template/world_france.jpg"     onmouseout="/template/world.jpg" targetImage="mapImage" />

TheAreaRenderer class takes care of generating the JavaScript for these actions, asshown in the following code fromencodeEnd. The JavaScript thatAreaRenderer generates fortheonclick action sets the value of the hidden field to the valueof the current area’s component ID and submits the page.

sb = new StringBuffer("document.forms[0][’").    append(targetImageId).append("’].src=’");sb.append(getURI(context,     (String) area.getAttributes().get("onmouseout")));sb.append("’");writer.writeAttribute("onmouseout", sb.toString(),     "onmouseout");sb = new StringBuffer("document.forms[0][’").    append(targetImageId).append("’].src=’");sb.append(getURI(context,     (String) area.getAttributes().get("onmouseover")));sb.append("’");writer.writeAttribute("onmouseover", sb.toString(),     "onmouseover");sb = new StringBuffer("document.forms[0][’");sb.append(getName(context, area));sb.append("’].value=’");sb.append(iarea.getAlt());sb.append("’; document.forms[0].submit()");writer.writeAttribute("onclick", sb.toString(), "value");writer.endElement("area");

By submitting the page, this code causes the JavaServer Faces life cycle toreturn back to the restore view phase. This phase saves any state information,including the value of the hidden field, so that a new request componenttree is constructed. This value is retrieved by thedecode method of theMapComponent class. Thisdecode method is called by the JavaServer Faces implementation duringthe apply request values phase, which follows the restore view phase.

In addition to theencodeEnd method,AreaRenderer contains an empty constructor. Thisis used to create an instance ofAreaRenderer so that it can beadded to the render kit.

Identifying the Renderer Type

During the render response phase, the JavaServer Faces implementation calls thegetRendererType methodof the component’s tag handler to determine which renderer to invoke, if thereis one.

ThegetRendererType method ofAreaTag must return the type associated withAreaRenderer. Youidentify this type when you registerAreaRenderer with the render kit, as described inRegistering a Custom Renderer with a Render Kit. Here is thegetRendererType method from theAreaTag class:

public String getRendererType() { return ("DemoArea");}

Creating the Component Tag Handler explains more about thegetRendererType method.

PreviousContentsNext

Copyright © 2010, Oracle and/or its affiliates. All rights reserved.Legal Notices


[8]ページ先頭

©2009-2025 Movatter.jp