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

What Is a JSP Page?

A Simple JSP Page Example

The Example JSP Pages

The Life Cycle of a JSP Page

Translation and Compilation

Execution

Buffering

Handling JSP Page Errors

Creating Static Content

Response and Page Encoding

Creating Dynamic Content

Using Objects within JSP Pages

Using Implicit Objects

Using Application-Specific Objects

Using Shared Objects

Unified Expression Language

Immediate and Deferred Evaluation Syntax

Immediate Evaluation

Deferred Evaluation

Value and Method Expressions

Value Expressions

Method Expressions

Defining a Tag Attribute Type

Deactivating Expression Evaluation

Literal Expressions

Resolving Expressions

Process of Expression Evaluation

EL Resolvers

Implicit Objects

Operators

Reserved Words

Examples of EL Expressions

Functions

Using Functions

Defining Functions

JavaBeans Components

JavaBeans Component Design Conventions

Creating and Using a JavaBeans Component

Setting JavaBeans Component Properties

Retrieving JavaBeans Component Properties

Using Custom Tags

Declaring Tag Libraries

Including the Tag Library Implementation

Reusing Content in JSP Pages

Transferring Control to Another Web Component

jsp:param Element

Including an Applet

Setting Properties for Groups of JSP Pages

Deactivating EL Expression Evaluation

Declaring Page Encodings

Defining Implicit Includes

Eliminating Extra White Space

Further Information about 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

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

JavaBeans Components

JavaBeans components are Java classes that can be easily reused and composed together intoapplications. Any Java class that follows certain design conventions is a JavaBeans component.

JavaServer Pages technology directly supports using JavaBeans components with standard JSP language elements.You can easily create and initialize beans and get and set the valuesof their properties.

JavaBeans Component Design Conventions

JavaBeans component design conventions govern the properties of the class and govern thepublic methods that give access to the properties.

A JavaBeans component property can be:

  • Read/write, read-only, or write-only

  • Simple, which means it contains a single value, or indexed, which means it represents an array of values

A property does not have to be implemented by an instance variable. Itmust simply be accessible using public methods that conform to the following conventions:

  • For each readable property, the bean must have a method of the form:

    PropertyClass getProperty() { ... }
  • For each writable property, the bean must have a method of the form:

    setProperty(PropertyClass pc) { ... }

In addition to the property methods, a JavaBeans component must define a constructorthat takes no parameters.

The Duke’s Bookstore application JSP pagesbookstore.jsp,bookdetails.jsp,catalog.jsp, andshowcart.jsp, alllocated attut-install/javaeetutorial5/examples/web/bookstore2/web, use thetut-install/javaeetutorial5/examples/web/bookstore2/src/java/com/sun/bookstore2/database/BookDB.java JavaBeans component.

BookDB provides a JavaBeans component front end to the access objectBookDBAO. TheJSP pagesshowcart.jsp andcashier.jsp access the beantut-install/javaeetutorial5/examples/web/bookstore/src/com/sun/bookstore/cart/ShoppingCart.java, which represents auser’s shopping cart.

TheBookDB bean has two writable properties,bookId anddatabase, and threereadable properties:bookDetails,numberOfBooks, andbooks. These latter properties do not correspond toany instance variables but rather are a function of thebookId anddatabaseproperties.

package database;public class BookDB {    private String bookId = "0";    private BookDBAO database = null;    public BookDB () {    }    public void setBookId(String bookId) {    this.bookId = bookId;    }    public void setDatabase(BookDBAO database) {    this.database = database;    }    public Book getBook() throws         BookNotFoundException {        return (Book)database.getBook(bookId);     }    public List getBooks() throws BooksNotFoundException {        return database.getBooks();    }    public void buyBooks(ShoppingCart cart)         throws OrderException {        database.buyBooks(cart);    }    public int getNumberOfBooks() throws BooksNotFoundException {        return database.getNumberOfBooks();    }}

Creating and Using a JavaBeans Component

To declare that your JSP page will use a JavaBeans component, you useajsp:useBean element. There are two forms:

<jsp:useBean    scope="scope"/>

and

<jsp:useBean    scope="scope">    <jsp:setProperty .../></jsp:useBean>

The second form is used when you want to includejsp:setProperty statements, describedin the next section, for initializing bean properties.

Thejsp:useBean element declares that the page will use a bean thatis stored within and is accessible from the specified scope, which can beapplication,session,request, orpage. If no such bean exists, the statement createsthe bean and stores it as an attribute of the scope object (seeUsing Scope Objects). The value of theid attribute determines thename of the bean inthe scope and theidentifier used to reference the bean in EL expressions,other JSP elements, and scripting expressions (seeChapter 9, Scripting in JSP Pages). The value supplied for theclassattribute must be a fully qualified class name. Note that beans cannot bein the unnamed package. Thus the format of the value must bepackage-name.class-name.

The following element creates an instance ofmypkg.myLocales if none exists, stores itas an attribute of the application scope, and makes the bean available throughoutthe application by the identifierlocales:

<jsp:useBean scope="application"   />

Setting JavaBeans Component Properties

The standard way to set JavaBeans component properties in a JSP page isby using thejsp:setProperty element. The syntax of thejsp:setProperty element depends onthe source of the property value.Table 5-6 summarizes the various ways to set aproperty of a JavaBeans component using thejsp:setProperty element.


Note -

Syntax rules of attribute values used in this table:

  1. beanName must be the same as that specified for theid attribute in auseBean element.

  2. There must be asetPropName method in the JavaBeans component.

  3. paramName must be a request parameter name.


Table 5-6 Valid Bean Property Assignments from String Values

Value Source

Element Syntax

String constant

<jsp:setProperty name="beanName"   property="propName" value="string-constant"/>

Request parameter

<jsp:setProperty name="beanName"   property="propName" param="paramName"/>

Request parameter name that matches bean property

<jsp:setProperty name="beanName"   property="propName"/><jsp:setProperty name="beanName"   property="*"/>

Expression

<jsp:setProperty name="beanName"   property="propName" value="expression"/><jsp:setProperty name="beanName"   property="propName" >   <jsp:attribute name="value">expression   </jsp:attribute></jsp:setProperty>

A property set from a constant string or request parameter must have oneof the types listed inTable 5-7. Because constants and request parameters are strings,the web container automatically converts the value to the property’s type; the conversionapplied is shown in the table.

String values can be used to assign values to a property that hasaPropertyEditor class. When that is the case, thesetAsText(String) method is used.A conversion failure arises if the method throws anIllegalArgumentException.

The value assigned to an indexed property must be an array, andthe rules just described apply to the elements.

You use an expression to set the value of a property whose typeis a compound Java programming language type. The type returned from an expressionmust match or be castable to the type of the property.

Table 5-7 Valid Property Value Assignments from String Values

Property Type

Conversionon String Value

Bean Property

UsessetAsText(string-literal)

boolean orBoolean

As indicated injava.lang.Boolean.valueOf(String)

byte orByte

Asindicated injava.lang.Byte.valueOf(String)

char orCharacter

As indicated injava.lang.String.charAt(0)

double orDouble

As indicated injava.lang.Double.valueOf(String)

int orInteger

As indicated injava.lang.Integer.valueOf(String)

float orFloat

As indicated injava.lang.Float.valueOf(String)

long orLong

As indicated injava.lang.Long.valueOf(String)

short orShort

As indicated injava.lang.Short.valueOf(String)

Object

newString(string-literal)

The Duke’s Bookstore application demonstrates how to use thesetProperty element toset the current book from a request parameter in the database bean intut-install/javaeetutorial5/examples/web/bookstore2/web/books/bookdetails.jsp:

<c:set var="bid" value="${param.bookId}"/><jsp:setProperty name="bookDB" property="bookId"    value="${bid}" />

The following fragment from the pagetut-install/javaeetutorial5/examples/web/bookstore2/web/books/bookshowcart.jsp illustrates how to initialize aBookDB bean with adatabase object. Because the initialization is nested in auseBeanelement, it is executed only when the bean is created.

<jsp:useBean scope="page">    <jsp:setProperty name="bookDB" property="database"         value="${bookDBAO}" /></jsp:useBean>

Retrieving JavaBeans Component Properties

The main way to retrieve JavaBeans component properties is by using the unifiedEL expressions. Thus, to retrieve a book title, the Duke’s Bookstore application usesthe following expression:

${bookDB.bookDetails.title}

Another way to retrieve component properties is to use thejsp:getProperty element.This element converts the value of the property into aString andinserts the value into the response stream:

<jsp:getProperty name="beanName" property="propName"/>

Note thatbeanName must be the same as that specified for theidattribute in auseBean element, and there must be agetPropName method in theJavaBeans component. Although the preferred approach to getting properties is to use anEL expression, thegetProperty element is available if you need to disable expressionevaluation.

PreviousContentsNext

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


[8]ページ先頭

©2009-2025 Movatter.jp