0
votes

I am attempting to call some java code from an XPage and was attempting to do this via SSJS. Just trying to get even a basic hello world example working. Ideally the return from the java code could be stuffed into a variable.

Goal: (Xpage contents)

<?xml version="1.0" encoding="UTF-8"?>
<xp:view xmlns:xp="http://www.ibm.com/xsp/core">
    <xp:label value="#{javascript:helloWorld.anyoneThere}" id="label1"></xp:label>
</xp:view>

would print 'Yo!' when the page loads. Instead I get a Runtime Error that helloWorld is not found.

Created a package

package testBean;

public class helloWorld {
    public String anyoneThere(){
        return "Yo!";
    }
}

Then I modified the faces-config file

<?xml version="1.0" encoding="UTF-8"?>
<faces-config>
    <managed-bean>
        <managed-bean-name>helloWorld</managed-bean-name>
        <managed-bean-class>testBean</managed-bean-class>
        <managed-bean-scope>application</managed-bean-scope>
    </managed-bean>
</faces-config>

Not sure what I need to do to initialize / call the java function. Some examples I've seen are hooking into views or are on events but the intended code for what I'm doing would more likely be run in the beforePageLoad section.

2
Both answers have pointers to get your sample working. I'd want to suggest to also follow the standard Java naming standards and start you Class name with an uppercase letter, e.g. HelloWorld. Also, make your class serializable ("public class HelloWorld implements Serializable") which is a requirement for beans.Mark Leusink

2 Answers

6
votes

Your managed-bean-class has you include the class name as well.

<managed-bean-class>testBean.helloWorld</managed-bean-class>

Also your label has to have parenthesis

<xp:label value="#{javascript:helloWorld.anyoneThere();}" id="label1"></xp:label>
2
votes

I have written a small tutorial on the basics of creating and using a simple managed bean with XPages.

The tutorial also shows how to add getters and setters to your variables so that you can use EL to reference them. So in your example you will be able to do the following to reference the anyoneThere variable (assuming that you have set up a getter and setter for it):

<xp:label value="#{helloWorld.anyoneThere}" id="label1"></xp:label>