OK to set the stage for what may just be about to happen – blogging is a form of self documentation for me. If I write it – it helps me remember it. I have said this before (multiple times) and failed, but I need to learn Java. It will probably fail again, let’s not kid ourselves, we all know Marky’s preferred language of choice (*coughs politely*). But I want to properly wrap my head around IBM Social Business Toolkit, and not just learn the limitations of the JavaScript API. To do this I need to learn Java and execute on it. So no promises, but we may get some blog posts on IBMSBT and my learning the Java way.
So with the help of Toby, the mockery of Brad and Kathy and the adulation of Eric, we will begin.
All of this code will be done in R9.0.1 (something)
Hello World (part 1)
1) Create a new Java Class in your database
There are apparently multiple ways to do this but I just went to the Java elements and clicked on the New Java Class button

from the resulting popup enter a package name – in this case com.xomino.sbt, and the name for the new class, HelloWorld
The normal format for naming convention is
- Package names are lower case and look like a website URL except backwards
- Classes are Propercase

Click OK and a new Class is created in Designer

and the crowd went wild……well ok maybe not but I know Paul and Russ are laughing their butts off right now.
2) Create a new XPage (xHelloWorld.xsp)
In there I am going to create a button which when clicked will display hello world on the screen. To do this I am going to need a button and a computed field.
When the button is clicked I will set a viewScope variable and refresh the field.
The field is bound to the viewScope variable which I hope to set in the Java code…..Clicking the button will call the com.xomino.sbt.HelloWorld.setMessage() method, which will set the viewScope variable value.
<xp:button value="Click me" id="button1">
<xp:eventHandler event="onclick" submit="true"
refreshMode="partial" refreshId="aWrapper">
<xp:this.action><![CDATA[#{javascript:com.xomino.sbt.HelloWorld().setMessage();}]]></xp:this.action>
</xp:eventHandler>
</xp:button>
<xp:div id="aWrapper">
<xp:text escape="true" id="computedField1"
value="#{javascript:viewScope.message}">
</xp:text>
</xp:div>
Once the viewScope variable has been set the aWrapper will be refreshed and I will see my value.
The class looks like this
package com.xomino.sbt;
import com.ibm.xsp.extlib.util.ExtLibUtil;
public class HelloWorld {
public static void setMessage(){
/*This is how you get XPages scopes in Java */
ExtLibUtil.getViewScope().put("message", "Hello World");
}
}
To enable the use of ExtLib code you must make sure the box is Checked in the database XSP properties
The output

In the dumbest way, this has made me very happy 🙂