Saturday, May 8, 2010

Using Java Methods In XPages

I know how useful Java is as a programming language. But how far can it be integrated with XPages. Yes I am speaking about the custom java classes that the user can create and use on XPages. Upon exploring for the same, I found a way to do it and I am amazed. I feel that I can do almost any thing with XPages now provided that java methods from my custom classes can be called directly into them.

The following is a sample flavor of what I am speaking about.

1. Create a new database or open an existing one in the domino designer 8.5.1

2. Click Windows -> Open Perspective -> Other, a pop up wold appear. Choose Java.


3. Now on the left margin you will be able to see the Package Explorer. Expand the entry corresponding to the database that you just created or opened (in step 1)

4. Navigate to the WebContent folder and expand it

5. Right Click on the WEB-INF folder under the above mentioned entry and Click New-> Package

6. Name the new package as myJavaMethods



7. Now create a class under the package by the name ProcessXPageCall and put the following code in it.

package myJavaMethods;

public class ProcessXPageCall {
    public String processInput(String inputToProcess)    {
        inputToProcess="My Processed " + inputToProcess;
        return inputToProcess;
    }
}


8. Save the class file.Now on the left hand margin right click your database's root node and select Properties

9. Select Java Build Path. On the Source Tab Click the Add Folder button.

10.Navigate to WebContent\WEB-INF\myJavaMethods folder, select the same and click OK



11. Now click Windows->Open Perspective -> Other and select Domino Designer


12. Create a new XPage, put a text field in it and compute its default value with the following code,

var xpCall=new myJavaMethods.ProcessXPageCall();
xpCall.processInput('inputString'); 



You will see the following results on the screen.



Hope you enjoy using Java in XPages

2 comments:

  1. Hello,

    could you explain please how can I use this written code not only in XPages but in Java agents and Web services?

    ReplyDelete
  2. Well you can use Shared Java Libraries. Did you try it?

    ReplyDelete