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

Creating the Component Tag Handler

Now that you’ve created your component and renderer classes, you’re ready to definehow a tag handler processes the tag representing the component and renderer combination.If you’ve created your own JSP custom tags before, creating a component taghandler should be easy for you.

In JavaServer Faces applications, the tag handler class associated with a component drivesthe render response phase of the JavaServer Faces life cycle. For more informationon the JavaServer Faces life cycle, seeThe Life Cycle of a JavaServer Faces Page.

The first thing that the tag handler does is to retrieve thetype of the component associated with the tag. Next, it sets the component’s attributesto the values given in the page. It then returns the typeof the renderer (if there is one) to the JavaServer Faces implementation sothat the component’s encoding can be performed when the tag is processed. Finally, itreleases resources used during the processing of the tag.

The image map custom component includes two tag handlers:AreaTag andMapTag.To see how the operations on a JavaServer Faces tag handler are implemented,let’s take a look atMapTag.

TheMapTag class extendsUIComponentELTag, which supportsjsp.tagext.Tag functionality as well as JavaServerFaces-specific functionality.UIComponentELTag is the base class for all JavaServer Faces tags thatcorrespond to a component. Tags that need to process their tag bodies shouldinstead subclassUIComponentBodyELTag.

Retrieving the Component Type

As explained earlier, the first thingMapTag does is to retrieve thetype of the component. It does this by using thegetComponentType operation:

    public String getComponentType() {        return ("DemoMap");    }

The value returned fromgetComponentType must match the value configured for the componentwith thecomponent-type element of the application’s application configuration resource file.Registering a Custom Component explainshow to configure a component.

Setting Component Property Values

After retrieving the type of the component, the tag handler sets the component’sproperty values to those supplied as tag attributes values in the page. Thissection assumes that your component properties are enabled to accept expressions, as explainedinEnabling Component Properties to Accept Expressions.

Getting the Attribute Values

Before setting the values in the component class, theMapTag handler first getsthe attribute values from the page by means of JavaBeans component properties thatcorrespond to the attributes. The following code shows the property used to accessthe value of theimmediate attribute.

private javax.el.ValueExpression immediate = null;public void setImmediate(javax.el.ValueExpression immediate) {    this.immediate = immediate;}

As this code shows, thesetImmediate method takes aValueExpression object. This meansthat theimmediate attribute of themap tag accepts value expressions.

Similarly, thesetActionListener andsetAction methods takeMethodExpression objects, which means that theseattributes accept method expressions. The following code shows the properties used to access thevalues of theactionListener and theaction attributes

private javax.el.MethodExpression actionListener = null;public void setActionListener(    javax.el.MethodExpression actionListener) {        this.actionListener = actionListener;}private javax.el.MethodExpression action = null;public void setAction(javax.el.MethodExpression action) {        this.action = action;}
Setting the Component Property Values

To pass the value of the tag attributes toMapComponent, the tag handlerimplements thesetProperties method. The waysetProperties passes the attribute values to thecomponent class depends on whether the values are value expressions or method expressions.

Setting Value Expressions on Component Properties

When the attribute value is a value expression,setProperties first checks if itis not a literal expression. If the expression is not a literal,setProperties stores the expression into a collection, from which the component class canretrieve it and resolve it at the appropriate time. If the expression isa literal,setProperties performs any required type conversion and then does one ofthe following:

  • If the attribute is renderer-independent, meaning that it is defined by the component class, thensetProperties calls the corresponding setter method of the component class.

  • If the attribute is renderer-dependent,setProperties stores the converted value into the component’s map of generic renderer attributes.

The following piece of theMapTag handler’ssetProperties method sets the renderer-dependentproperty,styleClass, and the renderer-independent property,immediate:

if (styleClass != null) {    if (!styleClass.isLiteralText()) {        map.setValueExpression("styleClass", styleClass);    } else {        map.getAttributes().put("styleClass",             styleClass.getExpressionString());    }}...if (immediate != null) {    if (!immediate.isLiteralText()) {        map.setValueExpression("immediate", immediate);    } else {        map.setImmediate(new             Boolean(immediate.getExpressionString()).                booleanValue());    }}
Setting Method Expressions on Component Properties

The process of setting the properties that accept method expressions is done differentlydepending on the purpose of the method. TheactionListener attribute uses amethod expression to reference a method that handles action events. Theaction attributeuses a method expression to either specify a logical outcome or to referencea method that returns a logical outcome, which is used for navigation purposes.

To handle the method expression referenced byactionListener, thesetProperties method must wrapthe expression in a special action listener object calledMethodExpressionActionListener. This listener executes themethod referenced by the expression when it receives the action event. ThesetPropertiesmethod then adds thisMethodExpressionActionListener object to the list of listeners to benotified when the event of a user clicking on the map occurs.The following piece ofsetProperties does all of this:

if (actionListener != null) {    map.addActionListener(        new MethodExpressionActionListener(actionListener));}

If your component fires value change events, your tag handler’ssetProperties methoddoes a similar thing, except it wraps the expression in aMethodExpressionValueChangeListener objectand adds the listener using theaddValueChangeListener method.

In the case of the method expression referenced by theaction attribute, thesetProperties method uses thesetActionExpression method ofActionSource2 to set the corresponding propertyonMapComponent:

if (action != null) {    map.setActionExpression(action);}

Providing the Renderer Type

After setting the component properties, the tag handler provides a renderer type (ifthere is a renderer associated with the component) to the JavaServer Faces implementation.It does this using thegetRendererType method:

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

The renderer type that is returned is the name under which therenderer is registered with the application. SeeDelegating Rendering to a Renderer for more information.

If your component does not have a renderer associated with it,getRendererTypeshould returnnull. In this case, therenderer-type element in the applicationconfiguration file should also be set tonull.

Releasing Resources

It’s recommended practice that all tag handlers implement arelease method, which releasesresources allocated during the execution of the tag handler. The release method ofMapTag as follows:

public void release() {    super.release();    current = null;    styleClass = null;    actionListener = null;    immediate = null;    action = null;}

This method first calls theUIComponentTag.release method to release resources associated withUIComponentTag. Next, the method sets all attribute values tonull.

PreviousContentsNext

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


[8]ページ先頭

©2009-2025 Movatter.jp