Using Java Beans


You can use Java Beans in your ColdFusion pages in the same way that you use ordinary Java classes. For writing ColdFusion code, there isn't much difference between a Bean and a normal class. You still use <cfobject> or CreateObject() to load the class, and then call its methods using <cfset> or other tags, as shown in the examples from the preceding section of this chapter.

The only difference is that any properties of the Bean can be accessed by name. Java Bean properties are the Bean's properly named getter and setter methods to store and retrieve the value of the property. So, if a Bean has a property called modelYear (which means nothing more than that getModelYear() and setModelYear() methods have been implemented within the Bean itself), then you could output the value of the property like so:

 <cfset myNovaBean = CreateObject("java", "chevy.cars.NovaBean")> ...other lines of code... <cfoutput>#myNovaBean.modelYear#</cfouptput> 

When your code refers to the Bean's modelYear property as shown just above, you are implicitly calling the getModelYear() method behind the scenes. It's really just a more convenient, natural-looking syntax for calling the getter function. Similarly, to set the model year (assuming that the Bean allows such a thing) you could use a line such as the following:

 <cfset myNovaBean.modelYear = 1986> 

This causes ColdFusion to call the Bean's setModelYear() method behind the scenes. The following line would be functionally equivalent:

 <cfset myNovaBean.setModelYear(1986)> 

Again, there is no particular technical advantage in referring to the property name instead of explicitly calling the getter and setter methods. It just looks nicer to some developers. Use or ignore the option as you wish.



Advanced Macromedia ColdFusion MX 7 Application Development
Advanced Macromedia ColdFusion MX 7 Application Development
ISBN: 0321292693
EAN: 2147483647
Year: 2006
Pages: 240
Authors: Ben Forta, et al

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