Skip Headers

Oracle® Application Server Containers for J2EE JSP Tag Libraries and Utilities Reference 10g Release 2 (10.1.2) B14016-02 |
| Library | Product | Contents | Index

Previous | Next |

# 2 JavaBeans for Extended Types

This chapter describes JavaBeans provided with OC4J for use as extended types. For JSP pages, these types offer advantages over Java primitive types or `java.lang` types.

The chapter consists of the following sections:

- Overview of JML Extended Types

- JML Extended Type Descriptions

## Overvi ew of JML Extended Types

JSP pages generally rely on core Java types in representing scalar values. However, neither of the following type categories is fully suitable for use in JSP pages:

- Primitive types such as `int`, `float`, and `double`

  Values of these types cannot have a specified scope. They cannot be stored in a JSP scope object (for `page`, `request`, `session`, or `application` scope), because only objects can be stored in a scope object.

- Wrapper classes in the standard `java.lang` package, such as `Integer`, `Float`, and `Double`

  Values of these types are objects, so they can theoretically be stored in a JSP scope object. However, they cannot be declared in a `jsp:useBean` action, because the wrapper classes do not follow the JavaBean model and do not provide zero-argument constructors.

  Additionally, instances of the wrapper classes are immutable. To change a value, you must create a new instance and assign it appropriately.

On JDK 1.4 or earlier, JML extended types provide a convenient option to work around these limitations. OC4J provides the following JavaBean classes in the `oracle.jsp.jml` package to act as wrappers for the most common Java types:

- `JmlBoolean` to represent a `boolean` value

- `JmlNumber` to represent an `int` value

- `JmlFPNumber` to represent a `double` value

- `JmlString` to represent a `String` value

Each of these classes has a single attribute, `value`, and includes methods to get the value, set the value from input in various formats, test whether the value is equal to a value specified in any of several formats, and convert the value to a string.

Alternatively, instead of using the `getValue()` and `setValue()` methods, you can use the `jsp:getProperty` and `jsp:setProperty` tags, as with any other bean.

The following example creates a `JmlNumber` instance called `count` that has `application` scope:

    <jsp:useBean id="count" class="oracle.jsp.jml.JmlNumber" scope="application" />

Later, assuming that the value has been set elsewhere, you can access it as follows:

    <h3> The current count is <%=count.getValue() %> </h3>

The following example creates a `JmlNumber` instance called `maxSize` that has `request` scope, and sets it using `setProperty`:

    <jsp:useBean id="maxSize" class="oracle.jsp.jml.JmlNumber" scope="request" >
        <jsp:setProperty name="maxSize" property="value" value="<%= 25 %>" />
    </jsp:useBean>

## JML Exte nded Type Descriptions

This section documents the public methods of the four extended types—`JmlBoolean`, `JmlNumber`, `JmlFPNumber`, and `JmlString`—followed by an example.

Note: To use the JML extended types, verify that the `ojsputil.jar` file is installed and in your classpath. This file is supplied with OC4J.

### Type Jm lBoolean

A `JmlBoolean` object represents a Java `boolean` value.

The `getValue()` and `setValue()` methods get or set the `value` property of the bean as a Java `boolean` value.

- `boolean getValue()`

- `void setValue(boolean)`

The `setTypedValue()` method has several signatures and can set the `value` property from a string (such as "`true`" or "`false`"), a `java.lang.Boolean` value, a Java `boolean` value, or a `JmlBoolean` value. For the string input, conversion of the string is performed according to the same rules as for the `valueOf()` method of the `java.lang.Boolean` class.

- `void setTypedValue(String)`

- `void setTypedValue(Boolean)`

- `void setTypedValue(boolean)`

- `void setTypedValue(JmlBoolean)`

The `equals()` method tests whether the `value` property is equal to the specified Java `boolean` value.

- `boolean equals(boolean)`

The `typedEquals()` method has several signatures and tests whether the `value` property has a value equivalent to a specified string (such as "`true`" or "`false`"), `java.lang.Boolean` value, or `JmlBoolean` value.

- `boolean typedEquals(String)`

- `boolean typedEquals(Boolean)`

- `boolean typedEquals(JmlBoolean)`

The `toString()` method returns the `value` property as a `java.lang.String` value, either "`true`" or "`false`".

- `String toString()`

### Type Jm lNumber

A `JmlNumber` object represents a 32-bit number equivalent to a Java `int` value.

The `getValue()` and `setValue()` methods get or set the `value` property of the bean as a Java `int` value.

- `int getValue()`

- `void setValue(int)`

The `setTypedValue()` method has several signatures and can set the `value` property from a string, a `java.lang.Integer` value, a Java `int` value, or a `JmlNumber` value. For the string input, conversion of the string is performed according to the same rules as for the `decode()` method of the `java.lang.Integer` class.

- `void setTypedValue(String)`

- `void setTypedValue(Integer)`

- `void setTypedValue(int)`

- `void setTypedValue(JmlNumber)`

The `equals()` method tests whether the `value` property is equal to the specified Java `int` value.

- `boolean equals(int)`

The `typedEquals()` method has several signatures and tests whether the `value` property has a value equivalent to a specified string (such as "`1234`"), `java.lang.Integer` value, or `JmlNumber` value.

- `boolean typedEquals(String)`

- `boolean typedEquals(Integer)`

- `boolean typedEquals(JmlNumber)`

The `toString()` method returns the `value` property as an equivalent `java.lang.String` value (such as "`1234`"). This method has the same functionality as the `toString()` method of the `java.lang.Integer` class.

- `String toString()`

### Type Jm lFPNumber

A `JmlFPNumber` object represents a 64-bit floating point number equivalent to a Java `double` value.

The `getValue()` and `setValue()` methods get or set the `value` property of the bean as a Java `double` value.

- `double getValue()`

- `void setValue(double)`

The `setTypedValue()` method has several signatures and can set the `value` property from a string (such as "`3.57`"), a `java.lang.Integer` value, a Java `int` value, a `java.lang.Float` value, a Java `float` value, a `java.lang.Double` value, a Java `double` value, or a `JmlFPNumber` value. For the string input, conversion of the string is according to the same rules as for the `valueOf()` method of the `java.lang.Double` class.

- `void setTypedValue(String)`

- `void setTypedValue(Integer)`

- `void setTypedValue(int)`

- `void setTypedValue(Float)`

- `void setTypedValue(float)`

- `void setTypedValue(Double)`

- `void setTypedValue(double)`

- `void setTypedValue(JmlFPNumber)`

The `equals()` method tests whether the `value` property is equal to the specified Java `double` value.

- `boolean equals(double)`

The `typedEquals()` method has several signatures and tests whether the `value` property has a value equivalent to a specified string (such as "`3.57`"), `java.lang.Integer` value, Java `int` value, `java.lang.Float` value, Java `float` value, `java.lang.Double` value, Java `double` value, or `JmlFPNumber` value.

- `boolean typedEquals(String)`

- `boolean typedEquals(Integer)`

- `boolean typedEquals(int)`

- `boolean typedEquals(Float)`

- `boolean typedEquals(float)`

- `boolean typedEquals(Double)`

- `boolean typedEquals(JmlFPNumber)`

The `toString()` method returns the `value` property as a `java.lang.String` value (such as "`3.57`"). This method has the same functionality as the `toString()` method of the `java.lang.Double` class.

- `String toString()`

### Type Jm lString

A `JmlString` object represents a `java.lang.String` value.

The `getValue()` and `setValue()` methods get or set the `value` property of the bean as a `java.lang.String` value. If the input in a `setValue()` call is null, then the `value` property is set to an empty (zero-length) string.

- `String getValue()`

- `void setValue(String)`

The `toString()` method is functionally equivalent to the `getValue()` method.

- `String toString()`

The `setTypedValue()` method sets the `value` property according to the specified `JmlString` value. If the `JmlString` value is `null`, then the `value` property is set to an empty (zero-length) string.

- `void setTypedValue(JmlString)`

The `isEmpty()` method tests whether the `value` property is an empty (zero-length) string: ""

- `boolean isEmpty()`

The `equals()` method has two signatures and tests whether the `value` property is equal to a specified `java.lang.String` value or `JmlString` value.

- `boolean equals(String)`

- `boolean equals(JmlString)`

### JML Exte nded Types Example

This example illustrates the use of JML extended type JavaBeans for management of simple types at scope. The page declares four session objects, one for each JML type. The page presents a form that enables you to enter values for each of these types. Once new values are submitted, the form displays both the new values and the previously set values. In the process of generating this output, the page updates the session objects with the new form values.

    <jsp:useBean id = "submitCount" class = "oracle.jsp.jml.JmlNumber" scope = "session" />

    <jsp:useBean id = "bool" class = "oracle.jsp.jml.JmlBoolean" scope = "session" >
            <jsp:setProperty name = "bool" property = "value" param = "fBoolean" />
    </jsp:useBean>

    <jsp:useBean id = "num" class = "oracle.jsp.jml.JmlNumber" scope = "session" >
            <jsp:setProperty name = "num" property = "value" param = "fNumber" />
    </jsp:useBean>

    <jsp:useBean id = "fpnum" class = "oracle.jsp.jml.JmlFPNumber" scope = "session" >
            <jsp:setProperty name = "fpnum" property = "value" param = "fFPNumber" />
    </jsp:useBean>

    <jsp:useBean id = "str" class = "oracle.jsp.jml.JmlString" scope = "session" >
            <jsp:setProperty name = "str" property = "value" param = "fString" />
    </jsp:useBean>

    <HTML>

    <HEAD>
            <META HTTP-EQUIV="Content-Type" CONTENT="text/html;CHARSET=iso-8859-1">
            <META NAME="GENERATOR" Content="Visual Page 1.1 for Windows">
            <TITLE>Extended Datatypes Sample</TITLE>
    </HEAD>

    <BODY BACKGROUND="images/bg.gif" BGCOLOR="#FFFFFF">

    <% if (submitCount.getValue() > 1) { %>
            <h3> Last submitted values </h3>
            <ul>
                    <li> bool: <%= bool.getValue() %>
                    <li> num: <%= num.getValue() %>
                    <li> fpnum: <%= fpnum.getValue() %>
                    <li> string: <%= str.getValue() %>
            </ul>
    <% }

       if (submitCount.getValue() > 0) { %>

            <jsp:setProperty name = "bool" property = "value" param = "fBoolean" />
            <jsp:setProperty name = "num" property = "value" param = "fNumber" />
            <jsp:setProperty name = "fpnum" property = "value" param = "fFPNumber" />
            <jsp:setProperty name = "str" property = "value" param = "fString" />

            <h3> New submitted values </h3>
            <ul>
                    <li> bool: <jsp:getProperty name="bool" property="value" />
                    <li> num: <jsp:getProperty name="num" property="value" />
                    <li> fpnum: <jsp:getProperty name="fpnum" property="value" />
                    <li> string: <jsp:getProperty name="str" property="value" />
            </ul>
    <% } %>

    <jsp:setProperty name = "submitCount" property = "value"
                     value = "<%= submitCount.getValue() + 1 %>" />

    <FORM ACTION="index.jsp" METHOD="POST" ENCTYPE="application/x-www-form-urlencoded">
    <P> <pre>
     boolean test: <INPUT TYPE="text" NAME="fBoolean" VALUE="<%= bool.getValue() %>" >
      number test: <INPUT TYPE="text" NAME="fNumber" VALUE="<%= num.getValue() %>" >
    fpnumber test: <INPUT TYPE="text" NAME="fFPNumber" VALUE="<%= fpnum.getValue() %>" >
      string test: <INPUT TYPE="text" NAME="fString" VALUE= "<%= str.getValue() %>" >
    </pre>

    <P> <INPUT TYPE="submit">

    </FORM>

    </BODY>

    </HTML>