Creating your first managed bean for XPages

February 8th, 2012

With Java and managed beans you can make your XPages applications even more powerful. In this blog post I will go through the steps to create and use a managed bean in XPages.

I am using Lotus Notes and Domino Designer 8.5.3 and will therefore use the new Java design element for the Java code located in the Code section.

In short: use "New Java Class" to create a Java class. Give your new class a package name (e.g. com.company) and a class name (e.g. HelloWorld). Add a local variable and let Eclipse generate getter and setter for it by using Source - Generate Getters and Setters. Also, let Eclipse generate a public no-parameter constructor by using Source - Generate Constructor using Fields and deselect your newly created field and omit call to super().

You will then have the following basic skeleton for a Java class:

package com.company;

public class helloworld {

	public helloworld() {
	}

	private String someVariable;

	public String getSomeVariable() {
		return someVariable;
	}

	public void setSomeVariable(String someVariable) {
		someVariable = someVariable;
	}

}

In order to support the XPages server page persistence option "Keep pages on disk", the Java class needs to be serializable. The Java class therefore needs to implement Serializable. Therefore, add "implements Serializable" after your class name (and Eclipse will automatically add the required import). Then use the Eclipse Quick Fix to "Add generated serial version ID". You now have a basic skeleton for a Java class that can be used as a managed bean:

package com.company;

import java.io.Serializable;

public class helloworld implements Serializable {

	private static final long serialVersionUID = 6469339826789980362L;

	public helloworld() {
	}

	private String someVariable;

	public String getSomeVariable() {
		return someVariable;
	}

	public void setSomeVariable(String someVariable) {
		someVariable = someVariable;
	}

}

Now we need to tell XPages that we would like to use this Java class as a managed bean. First, Add the Package Explorer view to your Eclipse perspective using Window - Show Eclipse Views - Other and select Package Explorer. Now go to the Package Explorer and navigate to the WebContent/WEB-INF folder where you will find a faces-config.xml file. Open the faces-config.xml file and add the following managed bean section in the <faces-config> section:

<?xml version="1.0" encoding="UTF-8"?>
<faces-config>
	<managed-bean>
		<managed-bean-name>helloWorld</managed-bean-name>
		<managed-bean-class>com.company.helloworld</managed-bean-class>
		<managed-bean-scope>session</managed-bean-scope>
	</managed-bean>
</faces-config>

You now have a basic faces-config for your HelloWorld Java class. Scope for a bean can be the usual scopes (application, session, view, request).

With the Java class and the faces-config in place the managed bean can now be used in an XPage. The bean value (both the getter and setter) can be accessed from an XPage using #{helloWorld.someVariable}.

This very basic example of a bean does not do anything. The Java class would of course have to add values to the someVariable property before that value would be useful in an XPage. I will create a 2nd blog post that shows how to use a bean in a repeat control to display values from the managed bean.

For more information on XPages and managed beans have a look at:

Tags: , ,

7 Responses to “Creating your first managed bean for XPages”

  1. David Leedy Says:

    Grear post Per!!

    I've started playing around with this stuff myself just a little. It's kinda fun really.

    I just wanted to add to the further information. Jeremy Hodge did a 3 part video series on getting started with managed beans for Notesin9. Its called "Into to Java for XPages developers" and can easily be found at http://XPages.TV

  2. Per Henrik Lausten Says:

    Hi David, thanks!

    I have updated the post to include links to the Jeremy Hodge show on XPages.TV.

  3. Thomas Adrian Says:

    Nice, that's the kind of tutorials I like.

  4. Mark Roden Says:

    Thanks Per - can't wait for the next installment 🙂

  5. Simon O'Doherty Says:

    Very clear and easy to read! Maybe you should copy this over to the Appdev wiki?

  6. Beans | XPages.dk Says:

    [...] [...]

  7. Improving XPages Application Performance with Managed Beans (Russell Maher, The View Advanced Xpages 2012) « All about Lotus Domino Development (AaLDD) Says:

    [...] can start using Per Hendrik Lausten’s blog post as a starter to get more into managed beans. Jeremy Hodge also covers the topic [...]