Java & J2EE Page 5 - Overview of Java Web Technologies, Part 2 |
To access a property in a bean, use the jsp:getProperty and jsp:setProperty action elements. The jsp:getProperty element obtains the value of an internal variable, and the bean must provide a getter method. A jsp:getProperty element returns the property value converted into String. The return value is then automatically fed into an out.print method, so it will be displayed in the current JSP page. The syntax of the jsp:getProperty element is as follows: The name attribute must be assigned the name of the bean instance from which the property value will be obtained. The property attribute must be assigned the name of the property. The jsp:setProperty action element sets the value of a property. Its syntax has four forms: In this book, you will learn about and use only the first form. The name attribute is assigned the name of the bean instance available in the current JSP page. In the first form of the syntax, the property attribute is assigned the name of the property whose value The following example demonstrates the use of the jsp:getProperty and jsp:setProperty action elements. Listing 8 shows a variation of the AdderBean used in the previous example. It has a private integer called memory. (Note that the variable name starts with a lowercase m.) It also has a getter method called getMemory and a setter method named setMemory. (Note that in both access methods, memory is spelled using an uppercase M.) Listing 8 The AdderBean with Access Methods Using the jsp:setProperty and jsp:getProperty action elements, you can set and obtain the value of memory, as demonstrated in the JSP page in Listing 9. Listing 9 Accessing a Bean Property Using jsp:setProperty and jsp:getProperty NOTE: For more information about JavaBeans, see
blog comments powered by Disqus |