background image

Setting JavaBeans Component Properties

<< Creating and Using JavaBeans Components | Valid Bean Property Assignments >>
<< Creating and Using JavaBeans Components | Valid Bean Property Assignments >>

Setting JavaBeans Component Properties

The jsp:useBean element declares that the page will use a bean that is stored within and is
accessible from the specified scope, which can be application, session, request, or page. If
no such bean exists, the statement creates the bean and stores it as an attribute of the scope
object (see
"Using Scope Objects" on page 105
). The value of the id attribute determines the
name of the bean in the scope and the identifier used to reference the bean in EL expressions,
other JSP elements, and scripting expressions (see
Chapter 9, "Scripting in JSP Pages"
). The
value supplied for the class attribute must be a fully qualified class name. Note that beans
cannot be in the unnamed package. Thus the format of the value must be
package-name.class-name.
The following element creates an instance of mypkg.myLocales if none exists, stores it as an
attribute of the application scope, and makes the bean available throughout the application by
the identifier locales:
<jsp:useBean id=
"locales" scope="application"
class=
"mypkg.MyLocales"/>
Setting JavaBeans Component Properties
The standard way to set JavaBeans component properties in a JSP page is by using the
jsp:setProperty
element. The syntax of the jsp:setProperty element depends on the source
of the property value.
Table 5­6
summarizes the various ways to set a property of a JavaBeans
component using the jsp:setProperty element.
Note ­
Syntax rules of attribute values used in this table:
1. beanName must be the same as that specified for the id attribute in a useBean element.
2. There must be a setPropName 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"/>
JavaBeans Components
Chapter 5 · JavaServer Pages Technology
169