Hiding Business Logic Using Beans


One of the first good principles of software design using JSP (and a core principle of Struts) is that you should keep business logic off the JSP page itself at all costs. This is for several reasons:

  • It limits reuse of the business logic.

  • It clutters up the JSP source code.

  • It exposes critical code to potential abuse or neglect by HTML and design staff.

It helps to think of the JSP page as the presentation layer of the application. It is responsible for the user interface but should leave the actual computation and other business- related actions for a lower level.

The way that JSP allows this is through the use of beans. Beans are simply Java classes that follow a few basic conventions. These are

  • Each attribute of the bean that will be exposed publicly should have at least a method called get X () . For example, an attribute called height should have a method called getHeight() .

  • If the bean will allow the attribute to be modified, it needs to provide a method called set X () .

  • The get X () method should return the same type value that the set X () method takes as an argument.

  • If a value is Boolean, it uses the accessor is X () rather than get X () .

JSP supports an introspection mechanism on beans that allows form values to be automatically populated into beans from a JSP page by using the jsp:setProperty tag. Listings 5.9, 5.10, and 5.11 show a sample application that shows how all this ties together.

Listing 5.9 Animal.java
 package demo; public class Animal {     String commonName = null;     String speciesName = null;     float adultHeight = 0;     float adultWeight = 0;     int topSpeed = 0;     String description;     public String getCommonName() {        return this.commonName;     }     public void setCommonName(String commonName) {        this.commonName = commonName;     }     public String getSpeciesName() {        return this.speciesName;     }     public void setSpeciesName(String speciesName) {        this.speciesName = speciesName;     }     public float getAdultHeight() {        return this.adultHeight;     }     public void setAdultHeight(float adultHeight) {        this.adultHeight = adultHeight;     }     public float getAdultWeight() {        return this.adultWeight;     }     public void setAdultWeight(float adultWeight) {        this.adultWeight = adultWeight;     }     public int getTopSpeed() {        return this.topSpeed;     }     public void setTopSpeed(int topSpeed) {        this.topSpeed = topSpeed;     }     public String getDescription() {        return this.description;     }     public void setDescription(String description) {        this.description = description;     } } 

This is a simple bean that implements a few properties of animal. Three types of properties are defined here: String properties such as species name ; float properties such as height and weight; and an integer property, the top speed of the animal.

Now you can create a form to enter the values you want to assign to an animal using the JSP page shown in Listing 5.10.

Listing 5.10 animalinput.jsp
 <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN"> <html> <head> <title>Input an Animal</title> </head> <body> <h1>Input an Animal</h1> <FORM action="animaldisplay.jsp" method="POST"> Common Name: <INPUT TYPE="text" name="commonName"><BR> Species Name: <INPUT TYPE="text" name="speciesName"><BR> Adult Height: <INPUT TYPE="text" name="adultHeight"><BR> Adult Weight: <INPUT TYPE="text" name="adultWeight"><BR> Top Speed: <INPUT TYPE="text" name="topSpeed"><BR> Description:<BR> <TEXTAREA rows="5" cols="50" name="description"> </TEXTAREA><BR> <INPUT TYPE="SUBMIT"> </FORM> </body> </html> 

This page is actually straight HTML, defining a standard form that takes the various properties of an animal. This page, after it's filled out, is shown in Figure 5.1. When the submit button is clicked, the values are sent to a second page, shown inListing 5.11.

Figure 5.1. Pointing your browser at animalinput.jsp .

graphics/05fig01.jpg

Listing 5.11 animaldisplay.jsp
 <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN"> <html> <head> <title>Display an Animal</title> </head> <body> <h1>Display an Animal</h1> <jsp:useBean id="animal" scope="request" class="demo.Animal"/> <jsp:setProperty name="animal" property="*"/> <H2><jsp:getProperty name="animal" property="commonName"/><H2> Species Name: <jsp:getProperty name="animal" property="speciesName"/><BR> Adult Weight: <jsp:getProperty name="animal" property="adultWeight"/> Kg (<%= animal.getAdultWeight() * 2.2 %> Lbs)<BR> Adult Height: <jsp:getProperty name="animal" property="adultHeight"/> m (<%= animal.getAdultHeight() * 3.28 %> ft)<BR> Top Speed: <jsp:getProperty name="animal" property="topSpeed"/> kph (<%= animal.getTopSpeed() * 0.621 %> mph)<BR> Description:<BR> <jsp:getProperty name="animal" property="description"/> </body> </html> 

This page is where all the interesting action occurs. First, the code uses jsp:useBean to create an instance of the Animal class, and associates it with the ID (which is to say, the local JSP variable name) animal .

The jsp:setProperty tag is a very powerful tool. When used as in the previous example, it looks at all the values available on the form that was just submitted, and then uses introspection to determine whether any of the property names match up with bean property names in the object specified by the name argument.

The result of this is that the newly created animal bean is populated with the values from the previous page. Type conversions of the object varieties ( String to int , String to float ) are handled automatically by the code. However, if the type conversion fails (if, for example, a float is typed into a field that is mapped into an int bean property), an exception is thrown ”something that your code should handle gracefully.

Note that the display code uses both the jsp:getProperty tag and the raw get X () calls to the object itself. You need to use the raw calls to compute the English unit equivalents of the metric values. An alternative is to provide a read-only get method in the class, such as the one shown in Listing 5.12.

Listing 5.12 Providing English Units in the Class
 public float getAdultWeight() {    return this.adultWeight; } public float getAdultWeightInLbs() {    return this.adultWeight * 2.2; } public void setAdultWeight(float adultWeight) {    this.adultWeight = adultWeight; 

Figure 5.2 shows the display page in operation. As you can see, it would look prettier if you did some number formatting on the float values to truncate the long decimal results.

Figure 5.2. Submitting the values to animaldisplay.jsp .

graphics/05fig02.jpg



Struts Kick Start
Struts Kick Start
ISBN: 0672324725
EAN: 2147483647
Year: 2002
Pages: 177

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