2014年4月17日 星期四

Setting Bean Properties and Initializing Beans

Setting Bean Properties
Bean properties are set using the setProperty element. This element requires a bean
name (from the ID in the useBean element), a property, and a value attribute, as shown
in the following:
<jsp:setProperty name=”<bean name>”
➥ property=”<property name>” value=”<expression>”/>
To set the name of the example NameBean to winston, you would use the following:
<jsp:setProperty name=”myBean” property=”name” value=”winston”/>
As with getProperty, the bean method can be called explicitly from a Java scriptlet:
<% myBean.setName(“winston”); %>

A useful feature of the setProperty tag is that bean properties can also be initialized
from the HTTP request parameters. This is accomplished by using a param attribute
rather than the value attribute:
<jsp:setProperty name=”<bean name>” property=”<property name>” param=”<name>”/>
The value of the named parameter is used to set the appropriate bean property. To use a
request parameter called name to set the NameBean property of the same name, you could
use the following:

<jsp:setProperty name=”myBean” property=”name” param=”name”/>

In fact, the param attribute can be omitted if the property name is the same as the request
parameter name. So the previous example could have put more succinctly as follows:
<jsp:setProperty name=”myBean” property=”name”/>
A last form of the setProperty bean is employed when multiple parameters are used to
initialize several bean properties. If the property name is set to *, all of the form request
parameters are used to initialize bean properties with the same name:
<jsp:setProperty name=”myBean” property=”*”/>

Initializing Beans 

Some beans require properties to be defined to initialize the bean. There is no mecha-
nism for passing in initial values for properties in the jsp:useBean element, so a syntac-
tic convention is used instead.

Conventionally, if a bean must have properties defined before it can be used on the Web
page, the jsp:useBean is defined with an element body and the jsp:setProperty tags
are defined in the useBean body to initialize the required properties.

For example, assuming that the simple NameBean example requires the name to be initial-
ized, the following useBean syntax would be used:
<jsp:useBean id=”myBean” class=”NameBean” scope=”request” >
<jsp:setProperty name=”myBean” property=”name” value=”winston”/>
</jsp:useBean>


In fact, the param attribute can be omitted if the property name is the same as the request
parameter name. So the previous example could have put more succinctly as follows:
<jsp:setProperty name=”myBean” property=”name”/>
A last form of the setProperty bean is employed when multiple parameters are used to
initialize several bean properties. If the property name is set to *, all of the form request
parameters are used to initialize bean properties with the same name:
<jsp:setProperty name=”myBean” property=”*”/>
The bean must define a property for every parameter in the HTTP request;
otherwise, an error occurs.

Initializing Beans 
Some beans require properties to be defined to initialize the bean. There is no mecha-
nism for passing in initial values for properties in the jsp:useBean element, so a syntac-
tic convention is used instead.

Conventionally, if a bean must have properties defined before it can be used on the Web
page, the jsp:useBean is defined with an element body and the jsp:setProperty tags
are defined in the useBean body to initialize the required properties.
For example, assuming that the simple NameBean example requires the name to be initial-
ized, the following useBean syntax would be used:
<jsp:useBean id=”myBean” class=”NameBean” scope=”request” >
<jsp:setProperty name=”myBean” property=”name” value=”winston”/>
</jsp:useBean>

Using a Bean with the Agency Case Study

The next example uses the Agency case study code and refactors the name.jsp Web page
shown in Listing 13.6 to use a JavaBean. This time, you will use a bean to hide the com-
plex JNDI lookup and type casting needed to access the agency Session EJB.
The new JSP page is shown in Listing 13.9.


1: <HTML>
2: <TITLE>Agency Name</TITLE>
3: <BODY>4: <jsp:useBean id=”agency” class=”web.AgencyBean” scope=”request” />
5: <H1><jsp:getProperty name=”agency” property=”agencyName”/></H1>
6: </BODY>
7: </HTML>
This is much simpler for a non-Java developer to work with. All of the code required to
create the EJB using its JNDI name has been spirited away into a JavaBean of class
web.AgencyBean. This bean does not have any properties but simply defines a large num-
ber of business methods whose only purpose is to delegate behavior to the underlying
agency Session bean.


LISTING 13.10 Full Text of web.AgencyBean.java
1: package web;
2:
3: import java.rmi.*;
4: import java.util.* ;
5: import javax.ejb.* ;
6: import javax.naming.* ;
7:
8: import agency.*;
9:
10: public class AgencyBean
11: {
12:     Agency agency;
13:
14:     public AgencyBean ()
➥ throws NamingException, RemoteException, CreateException {
15:         InitialContext ic = null;
16:         ic = new InitialContext();
17:         AgencyHome agencyHome =
➥ (AgencyHome)ic.lookup(“java:comp/env/ejb/Agency”);
18:         agency = agencyHome.create();
19:     }
20:
21:     public String getAgencyName() throws RemoteException {
22:         return agency.getAgencyName();
23:     }
24:
25:     public Collection findAllApplicants() throws RemoteException {
26:         return agency.findAllApplicants();
27:     }
28:    

沒有留言:

張貼留言