16.6 Accessing Bean Properties

When you simply use ${ name } , the system finds the object named name , coerces it to a String , and returns it. Although this behavior is convenient , you rarely want to output the actual object that the MVC servlet stored. Rather, you typically want to output individual properties of that object.

The JSP expression language provides a simple but very powerful dot notation for accessing bean properties. To return the firstName property of a scoped variable named customer , you merely use ${customer.firstName} . Although this form appears very simple, the system must perform reflection (analysis of the internals of the object) to support this behavior. So, assuming the object is of type NameBean and that NameBean is in the coreservlets package, to do the same thing with explicit Java syntax, you would have to replace

 
 ${customer.firstName} 

with

 
 <%@ page import="coreservlets.NameBean" %> <% NameBean person =      (NameBean)pageContext.findAttribute("customer"); %> <%= person.getFirstName() %> 

Furthermore, the version with the JSP expression language returns an empty string if the attribute is not found, whereas the scripting element version would need additional code to avoid a NullPointerException in the same situation.

Now, JSP scripting elements are not the only alternative to use of the expression language. As long as you know the scope and fully qualified class name, you could replace

 
 ${customer.firstName} 

with

 
 <jsp:useBean id="customer" type="coreservlets.NameBean"              scope="  request, session, or application  " /> <jsp:getProperty name="customer" property="firstName" /> 

However, the expression language lets you nest properties arbitrarily. For example, if the NameBean class had an address property (i.e., a getAddress method) that returned an Address object with a zipCode property (i.e., a getZipCode method), you could access this zipCode property with the following simple form.

 
 ${customer.address.zipCode} 

There is no combination of jsp:useBean and jsp:getProperty that lets you do the same thing without explicit Java syntax.

Equivalence of Dot Notation and Array Notation

Finally, note that the expression language lets you replace dot notation with array notation (square brackets). So, for example, you can replace

 
 ${name.property} 

with

 
 ${name["property"]} 

The second form is rarely used with bean properties. However, it does have two advantages.

First, it lets you compute the name of the property at request time. With the array notation, the value inside the brackets can itself be a variable; with dot notation the property name must be a literal value.

Second, the array notation lets you use values that are illegal as property names . This is of no use when accessing bean properties, but it is very useful when accessing collections (Section 16.7) or request headers (Section 16.8).

An Example

To illustrate the use of bean properties, consider the BeanProperties servlet of Listing 16.3. The servlet creates an EmployeeBean (Listing 16.4), stores it in the request object with an attribute named employee , and forwards the request to a JSP page.

The EmployeeBean class has name and company properties that refer to NameBean (Listing 16.5) and CompanyBean (Listing 16.6) objects, respectively. The NameBean class has firstName and lastName properties and the CompanyBean class has companyName and business properties. The JSP page (Listing 16.7) uses the following simple EL expressions to access the four attributes:

 
 ${employee.name.firstName} ${employee.name.lastName} ${employee.company.companyName} ${employee.company.business} 

Figure 16-3 shows the results.

Listing 16.3 BeanProperties.java
 package coreservlets; /** Servlet that creates some beans whose properties will  *  be displayed with the JSP 2.0 expression language.  */ import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class BeanProperties extends HttpServlet {   public void doGet(HttpServletRequest request,                     HttpServletResponse response)       throws ServletException, IOException {  NameBean  name = new NameBean("Marty", "Hall");  CompanyBean  company =       new CompanyBean("coreservlets.com",                       "J2EE Training and Consulting");  EmployeeBean  employee = new EmployeeBean(name, company);     request.setAttribute("employee", employee);     RequestDispatcher dispatcher =       request.getRequestDispatcher("/el/bean-properties.jsp");     dispatcher.forward(request, response);   } } 
Listing 16.4 EmployeeBean.java
 package coreservlets; public class EmployeeBean {   private NameBean name;   private CompanyBean company;   public EmployeeBean(NameBean name, CompanyBean company) {     setName(name);     setCompany(company);   }   public NameBean  getName  () { return(name); }   public void setName(NameBean newName) {     name = newName;   }   public CompanyBean  getCompany  () { return(company); }   public void setCompany(CompanyBean newCompany) {     company = newCompany;   } } 
Listing 16.5 NameBean.java
 package coreservlets; public class NameBean {   private String firstName = "Missing first name";   private String lastName = "Missing last name";   public NameBean() {}   public NameBean(String firstName, String lastName) {     setFirstName(firstName);     setLastName(lastName);   }   public String  getFirstName  () {     return(firstName);   }   public void setFirstName(String newFirstName) {     firstName = newFirstName;   }   public String  getLastName  () {     return(lastName);   }   public void setLastName(String newLastName) {     lastName = newLastName;   } } 
Listing 16.6 CompanyBean.java
 package coreservlets; public class CompanyBean {   private String companyName;   private String business;   public CompanyBean(String companyName, String business) {     setCompanyName(companyName);     setBusiness(business);   }   public String  getCompanyName  () { return(companyName); }   public void setCompanyName(String newCompanyName) {     companyName = newCompanyName;   }   public String  getBusiness  () { return(business); }   public void setBusiness(String newBusiness) {     business = newBusiness;   } } 
Listing 16.7 bean-properties.jsp
 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD><TITLE>Accessing Bean Properties</TITLE> <LINK REL=STYLESHEET       HREF="/el/JSP-Styles.css"       TYPE="text/css"> </HEAD> <BODY> <TABLE BORDER=5 ALIGN="CENTER">   <TR><TH CLASS="TITLE">   Accessing Bean Properties </TABLE> <P> <UL>   <LI><B>First Name:</B>  ${employee.name.firstName}  <LI><B>Last Name:</B>  ${employee.name.lastName}  <LI><B>Company Name:</B>  ${employee.company.companyName}  <LI><B>Company Business:</B>  ${employee.company.business}  </UL> </BODY></HTML> 
Figure 16-3. You use dots or array notation to access bean properties.

graphics/16fig03.jpg



Core Servlets and JavaServer Pages (Vol. 1.Core Technologies)
Core Servlets and Javaserver Pages: Core Technologies, Vol. 1 (2nd Edition)
ISBN: 0130092290
EAN: 2147483647
Year: 2002
Pages: 194

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net