Java Reference
In-Depth Information
This is the equivalent of the following expression:
<%= EmpBean.getEmpName() %>
Remember, the purpose of JSP tags is to make using the Java classes that comprise the Model easier for the page
designer. Notice that someone coding the bean tags doesn't need to comprehend objects, instantiation or Java's
syntax rules.
Tutorial: Using JSP Bean Tags
To make full use of JSP bean tags in our application, we need to create two new properties in the Employee class
for taxAmount and grossAmount. These are a little different from our previously defined properties in that they are
calculated values. Earlier we mentioned that properties don't have to have getters and setters. Bean properties can
be read-only or write-only and the new properties (gross salary and tax amount) are perfect examples of read-only
properties. In other words, no user of the bean should be able to set these values; they should always be calculated
based on the pay rate and exemptions properties.
1.
In src/c9java.Employee, create two new class private variables by adding the following
statements:
private String taxAmount;
private String grossAmount;
2.
Click Source and then Generate Getters and Setters... .
3.
Select the options to generate only getters for the properties taxAmount and grossAmount
and click the OK button.
RAD will generate two new methods that return the strings taxAmount and grossAmount.
4.
Replace the code in getGrossAmount with the following statements:
grossSalaryCalcFormatted();
return stringGrossSalary;
5.
Replace the code in getTaxAmount with the following statements:
fedTaxCalcFormatted();
return stringTaxAmt;
Now that wasn't too hard, was it? These getters simply invoke the methods to calculate and format the
appropriate value, then return the result.
We now need to restructure the application so that EnterEmpInfoJSP creates and populates the bean. Currently
this function is performed by each of the display JSPs when they invoke EmpExtractor. However, this is very
redundant. The application should create the bean once and then the various JSPs should access the bean. This is a
perfect function for the Controller to handle and, since EnterEmpInfoJSP is acting as the Controller, we will include
the JSP tags to define and populate the bean in EnterEmpInfoJSP. We will then add the tags in the display JSPs to
retrieve the correct properties.
6.
In the Design view of EnterEmpInfoJSP, click on Bean in the JSP tags tray.
 
Search WWH ::




Custom Search