Getting To The Java Roots of XPages – Part 5

So in the last part we looked at how to call our new java class from ssjs but that still introduces a tiny bit of overhead as the ssjs interpreter is called to kick off the java code. To avoid using SSJS totally we need to look at something called Expression Language and Beans.

Lets not worry about Expression Language just yet, all you need to know is that you need a bean to make use of it for our purposes.

If you have been doing any XPage development you may have already heard the terms Java Beans and Managed Beans and already think that they are scary things that only the experts know about but you would be wrong. A java bean is just another name for a Class that sits in a package, in fact we have already written one in the previous parts, we just need to make a few small changes to it to become a proper bean.

The first thing you need to know about Beans is that they have to be serializable. What this means is that the xpage processor needs to be able to save state of the bean to either memory or disk and then restore it back whenever needed. So we need to tell our java class that we know about serializing things by using a special keywork on the class called implements.

public class AboutMe implements Serializable {

When you add this to the class the editor will prompt you for two things. First you need to import the package that Serializable exists in

import java.io.Serializable;

and in the class itself you need to declare a variable called serialversionID. The editor will help you with this automatically and this is what it looks like…

private static final long serialVersionUID = 1L;

Eeek! Scary Java Alert! What does all that mean?

Private means that this variable can only be seen by this class. It is not visible outside the class.

Static means that is variable will only exist once in memory. If you have multiple beans pointing to this java class then this particular variable will only exist in memory once.

Final means that this value will never change once assigned ( which we do after the = sign ). This allows the java compiler to better optimize it’s code because it now know that the value will never change.

Long is just the data type ( like int, String etc ).

and 1L is the value we are assigning. The L is shorthand for Long. Without the L it would try assign an int of 1 to the variable.

See that’s not so scary.

Next we need what is called an empty constructor. Every java class that needs to be uses as a bean needs one. When the bean is first called the constructor is automatically called with no parameters, thus the name Empty Constructor.

Inside the your class you need to add

public AboutMe() {
	// Constructor Code Here	
}

Now the name Empty Constructor seems to imply that it can’t have any code in it but that is not 100% correct. You could put some code in here, it just can’t accept any parameters and it doesn’t return any values.

Do we have a bean yet? Not really, we have a java class that can be used as a bean. In the next part we will show how to use this java class as a bean in the xpage environment.

Advertisements
Tagged with: ,
Posted in None
Archives
%d bloggers like this: