[ Team LiB ] 10.1 How Beans Are Implemented Internally, a bean is an instance of a Java class, although in common terminology, the class itself may also be referred to as a bean. The most basic kind of bean exposes a number of properties by following a few simple rules regarding method names. In general, a bean provides two methods for each property: a method to get the property and one to set the property, corresponding directly to the jsp:getProperty and jsp:setProperty tags. Together, these methods are known as accessors. Listing 10.1 shows a very simple bean with two properties. Listing 10.1 A simple bean package com.awl.jspblog.ch10; public class SimpleBean { private int age; private String name; public int getAge() {return age;} public void setAge(int age) {this.age = age;} public String getName() {return name;} public void setName(String name) {this.name = name;} } This bean could be used in a JSP just like any other that has appeared throughout this blog: In general, for a property named foo of type type, the get method will return an element of type and will be called getFoo(). The one exception is that if the type is boolean, the getmethod may be called isFoo(). For example, if it needs to keep track of whether it is ready to perform an action, a bean might have a ready property, and the method could be called isReady(). Whether getFoo() or isFoo() is used, it is this method that is called by the jsp:getProperty tag, as well as any tag, such as c:out or c:if, that obtains a value from a bean. Similarly, the set method will accept an argument of type, will be called setFoo(), and will be called by jsp:setProperty and c:set tags. There is no restriction on the type; it may be something simple, such as an integer or String, or it may be a class or interface type. The type may also be an array of another type, in which case the property is called an indexed property. In this case, the accessor methods operate on the whole array, and the bean may wish to provide methods to operate on the individual elements, as in Listing 10.2. Listing 10.2 A bean with an array property package com.awl.jspblog.ch10; public class ArrayBean { private String things[]; public String[] getThings() {return things;} public void setThings(String things[]) { Page 183
Hint: If you are looking for good and high quality web space to host and run your java application check Vision java web hosting services
This entry was posted
on Saturday, January 27th, 2007 at 8:39 am and is filed under jsp.
You can follow any responses to this entry through the RSS 2.0 feed.
Responses are currently closed, but you can trackback from your own site.