CHAPTER 8 Using JavaBeans with tags One of the most common tasks in any Java program is to get and set properties of JavaBeans. Web development in Java is no different in that respect. In an object-oriented Java web application, your servlets and JSPs likely contain a good deal of code that interacts with JavaBeans. For servlets, working with JavaBeans is the same as is in any other Java program: beans are instantiated and the proper get or set methods are called. JSPs interact with JavaBeans differently, by using three standard tags that are defined in the JSP specification (, , and ). These tags, discussed in chapter 2, instantiate beans, introduce them to the page as scripting variables, and set properties in the beans in addition to printing properties back to the user. Unfortunately, the functionality exposed by the JavaBeans-related tags is rather limited. Two of the more troublesome limitations are: The tags cannot operate on objects unless they were introduced to the page via a JavaBeans-related JSP tag. The tags cannot manipulate indexed properties (properties that have multiple values and are referred to by an index like getSomeProperty(i)). These shortcomings render JavaBeans standard tags useless for solving many practical problems such as iterating over an indexed property of a bean, or printing an attribute value of a bean that we would rather not introduce as a JSP scripting variable. Not surprisingly, developers increasingly find themselves in need of a more versatile set of JavaBeans tags. In this chapter, we address that need. To this end we ll create a custom tag library that supports interaction with Java- Beans, free from the limitations of today s standard tags. We start with a brief discussion of the Java reflection API, which we ll soon see is crucial to building our JavaBean tags. We then move on to discuss the JavaBean component standard and how reflection assists us in getting and setting properties of Java Beans. We finish by creating our custom tag library which will consist of two tags: one that allows us to expose any JavaBean as a JSP scripting variable, and one that supports printing of the value of any JavaBean property to the user. 8.1 Java reflection If tags are to be useful, they must be created such that they are versatile enough to get and set the properties of any JavaBean. This requires that they be capable of calling a bean s get or set methods, simply by knowing the class name of the Java- Bean and the name of the property to be manipulated. If you ve heard of the Java reflection API, you know that it is a set of Java methods and classes that allows us to do just that. Using reflection, we can learn about the attributes of any Java class (including its methods) and even call any method of the class we choose. Before
Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost Java Web Hosting services
No comments yet.
Sorry, the comment form is closed at this time.