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 🙂
So… Marky’s bringin’ Java back?
Hardly………but I am going to talk about what I learn in the context of SBT in a manner I hope other people can follow along with.
And so I have a point of reference, cos you know I am old..
Excellent !! And seriously, I would never laugh at someone taking the time and effort to learn something new. I applaud your effort and your blogging the journey. Every programmer of a new language had to have their starting point. So YES the crowd ( me and russ at least when he returns from vacation ) have gone wild !! And look forward to the journey !!!
Thanks Paul – you have both been an encouragement and I really appreciate it 🙂
Dont feel bad – I resisted Java for quite awhile until I started being put in to projects where Java was required.
But – at this point – JavaScript being a server-side scripting language as well makes Java unnecessary in some cases. For instance – I am looking at a solution to integrate with Salesforce. Although I can do this in the main app code itself, it causes problems and poor performance but deploying the integration code to NodeJS and write the queries and oauth2 code there makes more sense. At that point my app can worry about just that.
Ok I went on a rant and this is my first time commenting on your blog. So i shall say – keep going and look forward to more posts.
Mark what seriously helped/made me learn Java was taking the basic certification, and I was proud of passing that exam because it wasn’t easy. Lotuscript kept me in a job for years, and oddly Java has enabled me to do the same. I uses it all the time now.
“I uses it” 🙂
Thanks Jason 🙂
Thank you for sharing Mark. I’ve wanted to learn Java in combination with XPages for year, but didn’t really have a use case for it, nor was I able to completely wrap my head around it for some odd reason (and I am usually really quick with picking up programming language).
It is not the syntax but rather the question “what does it all do” and just the start screen when you create a new java class seems to be challenging to me (what do I click, select, include), as we would say in Germany “Ich verstehe nur Bahnhof”.
So, if you don’t mind, I’ll tag along on your journey to Java and hopefully, it’ll make click on day :o)
Thanks Daniel – this will be an idiot’s guide so to speak, ‘cos really I am, and it is 🙂