| < Day Day Up > |
|
The <bean:define /> tag is used to retrieve the value of a named bean property and define it as a scripting variable, which is will be stored in the scope specified by the toScope attribute. The retrieved object will perform type conversion on the returned property value, unless it is a Java primitive type, in which case it is wrapped in the appropriate wrapper class (for example, int is wrapped by java.lang.Integer).
This <bean:define /> tag has a body type of JSP and supports seven attributes, described in Table 20.2.
Attribute | Description |
---|---|
id | Specifies the scripting variable that will be created and stored in a scoped attribute that will be made available with the value of the indicated property. (Required) |
name | Specifies the attribute name of the bean whose property is retrieved to define a new scoped attribute. You must include the name attribute, unless you specify a value attribute. (Optional) |
property | Identifies the property of the bean, specified by the name attribute, that is being retrieved. If the property attribute is not specified, then the bean identified by the name attribute is given a new reference to the object identified by the id attribute. (Optional) |
scope | Identifies the scope of the bean specified by the name attribute. If the scope attribute is not specified, then the tag will search for the bean in the scopes, in the order of page, request, session, and application. (Optional) |
toScope | Identifies the scope of the newly defined bean. The default scope is page. (Optional) |
type | Provides the fully qualified class name of the value to be exposed as the id attribute. The default type is java.lang.String if a value attribute is specified; otherwise, the object will be of type java.lang.Object. (Optional) |
value | Contains a string value to which the exposed bean should be set. You must include the value attribute, unless you specify the name attribute. (Optional) |
An example of using the <bean:define /> tag is shown here:
<jsp:useBean scope="page" /> <bean:define name="user" property="firstName"/> Welcome: <%= name %>
In this example, we have user, a page-level object of type com.wrox.User. We then use the <bean:define /> tag to retrieve the user property firstName and store this value in the scripting variable named name. We conclude this snippet by printing the contents of the newly created name object.
| < Day Day Up > |
|