what the heck is a bean?
Category java xpages
If you're learning about XPage development, particularly if you're pushing past "drag and drop" development, it's likely that you've encountered the term "bean". If so, it's also likely that you've seen this term in the context of a larger concept: "managed beans". For the purposes of this post, let's ignore the "managed" portion for now and just focus on what it means for a Java class to be considered a "bean".
Given the coffee motif of the Java language, the term "bean" is used simply to indicate the role a class plays as a unit of Java. You may have also encountered the acronym POJO: "Plain Old Java Object". As Wikipedia indicates, a POJO is just any Java class that doesn't conform to any specific conventions. A bean, on the other hand, conforms to very specific - but very easy - conventions:
Why is it crucial to understand the nature of beans when developing XPages, even if you're not specifically writing Java code? Because darn near everything in an XPage is a bean. Every control - every inputText, every panel, every repeat - is a bean. This means that your code can interact with control instances in a predictable fashion. An inputText has a title property that stores a String, therefore it must have a getTitle() method that returns a String, and a setTitle() method that accepts a String. Just about every control has a styleClass String property, so it's generally safe to assume that, even in SSJS, you can call getStyleClass() and setStyleClass() regardless of which control you're interacting with. If you've attended my session on advanced theme development (and/or downloaded the presentation), you know that you can customize nearly any property of a control via a theme... this is why: every control is a bean, so the theme manager knows predictably how to inspect and modify any control attribute without having to hardcode specific behavior for each.
There are many other advantages to the extent to which everything in XPages is based on beans, like using abbreviated EL (expression language) syntax to bind read/write control attributes to a property of another bean (this is a topic for another day). But the key point to remember is that, even if you're not writing any Java yourself, everything you're interacting with is some Java bean... so, as long as you know what properties that object supports (and whether or not each property is a boolean), you know how to retrieve or modify those properties programmatically... even without any explicit documentation.
If you're learning about XPage development, particularly if you're pushing past "drag and drop" development, it's likely that you've encountered the term "bean". If so, it's also likely that you've seen this term in the context of a larger concept: "managed beans". For the purposes of this post, let's ignore the "managed" portion for now and just focus on what it means for a Java class to be considered a "bean".
Given the coffee motif of the Java language, the term "bean" is used simply to indicate the role a class plays as a unit of Java. You may have also encountered the acronym POJO: "Plain Old Java Object". As Wikipedia indicates, a POJO is just any Java class that doesn't conform to any specific conventions. A bean, on the other hand, conforms to very specific - but very easy - conventions:
- It includes an argumentless constructor. For those of you with a LotusScript background, this is similar to how you can Dim a variable as a New NotesSession() or a New NotesUIWorkspace()... you don't have to pass any parameters, or arguments, to that declaration... you just create the new object, then interact with it later. NOTE: this doesn't mean that a bean can't also have constructors that do accept arguments; Java, after all, does support method overloading. But it means that, at a minimum, the class supports instantiation with no arguments.
- Attributes of the object are accessible via predictably named "getters" and "setters". For example, if I create a House bean, and one of its properties is "address", the class will include a getAddress() method and a setAddress() method. Similar to the first point, the getter accepts no arguments, and the setter accepts one: the new value of the property. One might well ask, why not just make the property public, if you're always going to provide public getters and setters? The answer is simple: forcing reads/writes to call methods provides the luxury of doing other stuff™, if desired. This provides an opportunity to sanitize incoming data, for example, to ensure a property's value is not being set to invalid data. Also worthy of note is the one deviation from the getPropertyName / setPropertyName convention: if the property is a boolean (true/false) value, the getter uses an "is" prefix instead - for example, isForeclosed() - but the setter always uses "set".
- The class is serializable. Serialization is the process of storing the state of an object somewhere else. This could be in some flat file on the hard drive, a database record, or even just a different in-memory format. Deserialization, then, is the reconstruction of the object state from an alternate location. This is actually a key reason for the previous two conventions: some process external to the class definition can parse the serialized state of an object and restore its state predictably... construct an instance with no arguments, then call the setters for each of the attribute values it finds.
Why is it crucial to understand the nature of beans when developing XPages, even if you're not specifically writing Java code? Because darn near everything in an XPage is a bean. Every control - every inputText, every panel, every repeat - is a bean. This means that your code can interact with control instances in a predictable fashion. An inputText has a title property that stores a String, therefore it must have a getTitle() method that returns a String, and a setTitle() method that accepts a String. Just about every control has a styleClass String property, so it's generally safe to assume that, even in SSJS, you can call getStyleClass() and setStyleClass() regardless of which control you're interacting with. If you've attended my session on advanced theme development (and/or downloaded the presentation), you know that you can customize nearly any property of a control via a theme... this is why: every control is a bean, so the theme manager knows predictably how to inspect and modify any control attribute without having to hardcode specific behavior for each.
There are many other advantages to the extent to which everything in XPages is based on beans, like using abbreviated EL (expression language) syntax to bind read/write control attributes to a property of another bean (this is a topic for another day). But the key point to remember is that, even if you're not writing any Java yourself, everything you're interacting with is some Java bean... so, as long as you know what properties that object supports (and whether or not each property is a boolean), you know how to retrieve or modify those properties programmatically... even without any explicit documentation.
Comments
This text should be added to the Designer help: if I now search for "bean" in the XPages section of the help I get exactly zero results.
Looking forward to your themes-session at the UKLUG.
Mark
Posted by Mark Leusink At 03:51:24 AM On 05/05/2011 | - Website - |
Posted by Wayne At 07:41:33 AM On 05/05/2011 | - Website - |
This is a pretty good background/introduction for the Java Bean and XPages Videos that Jeremy did a while back ({ Link }
Posted by David Leedy At 08:10:07 AM On 05/05/2011 | - Website - |
Posted by Roy Rumaner At 09:55:29 AM On 05/05/2011 | - Website - |
Posted by Miroslav Navratil At 02:50:36 AM On 06/05/2011 | - Website - |
Recently I tried to work out a solution that would allow me to bind single checkbox to bean property of Boolean type. I have to say that I failed and gave it up.
While reading your post I though that the different naming of the getter for property of Boolean type is the missing piece in the puzzle so I returned to it.
I tried to bind the checkbox to Boolean property (now with the getter starting with 'is') while using my own String <--> Boolean converter but I am not able to make it work. The XPage rendering for the initial request fails. If I replace 'is' with 'get' for the getter name the page is rendered properly.
Am I missing something?
Posted by Miroslav Navratil At 05:12:42 AM On 06/05/2011 | - Website - |
I have a problem when writing a bean, my bean is accessing the ACL and try to modify the entry (for example: delete an existing Role), but I get permission problem, is this possible for the bean to execute that kind of operation? and How can I do it?
Thanks in advance!
Posted by Thanh Nguyen At 03:40:43 AM On 06/07/2011 | - Website - |
Posted by Vinay At 10:26:09 AM On 10/03/2011 | - Website - |