Creating your first managed bean for XPages

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:

XPages Extension Library on OpenNTF updated to match Upgrade Pack 1

The XPages Extension Library project on OpenNTF was updated yesterday to version 853-20111215. This version matches the newly released Domino 8.5.3 Upgrade Pack 1.

If you want to use the OpenNTF version (and not Upgrade Pack 1) you can update your servers and clients using the update site method.

You can find the installation instructions for the OpenNTF version and for Upgrade Pack 1 on the Lotus Notes and Domino Application Development wiki.

Notes/Domino 8.5.3 Upgrade Pack 1: Great news for XPages

Great news for XPages today: Notes/Domino 8.5.3 Upgrade Pack 1 has just been released.

In short, Upgrade Pack 1 is an IBM supported version of the XPages Extension Library. This should help remove the fear of the Extension Library being open source that some customers have had, and thereby improve the adoption of XPages out there.

Upgrade Pack 1 is available on Passport Advantage as part number CI5HUEN.

XSnippets: code snippets for XPages

The beta version of XSnippets was recently announced by Niklas Heidloff on the OpenNTF blog. XSnippets is a code repository of useful XPages code snippets that OpenNTF contributors can contribute to. If you have useful code snippets then consider adding them to XSnippets.

I have added my first code snippet to XSnippets based on my blog entry on how to control the HTTP response status code in XPages:

Lotus Notes/Domino 8.5.3 and IBM XWork Server 8.5.3 are here

It’s October 4th and Lotus Notes and Domino 8.5.3 are now available for download on Passport Advantage. The brand new IBM XWork Server has also been announced.

I have been looking forward to this release in general, and in particular because of features specifically for XPages developers:

  • improved JavaScript editor
  • finally a java design element for XPages
  • Dojo 1.6.1
  • OneUI version 2.1 (which looks very similar to the prototype 3.0 version that the Lotus Notes and Domino Application Development wiki uses)
  • option to combine CSS and JS in a single file to improve site speed
  • improved HTML5 support such as support for adding HTML5 attributes
  • source control enablement
I look very much forward to an improved experience when working with XPages development in the Domino Designer client. Happy coding!

I am teaching two XPages courses in August

I will be teaching two 3-day courses in August, 2011 on basic XPages application development. The courses are arranged by Intravision.

The first 3-day course starts August 23 at IBM in Lyngby. Seats are still available if you are interested in signing up for the course. Please see the course details (in Danish) for more information on how to sign up.

The second 3-day course takes place the week after and is a closed course for a group of developers from the same company.

XPages: only show content for authorized users

Today I was asked: how do you make sure that anonymous users do not see content that only logged on (and thereby authorized) users must see?

I often use a simple solution of having a xp:panel for anonymous users and another xp:panel for authorized users (both on the same XPage). Only one of the two panels are rendered based on whether the user is logged or not. So the two xp:panels would look like this:

Panel 1: for anonymous users

<xp:panel>
<xp:this.rendered><![CDATA[#{javascript:@UserName() == "Anonymous"}]]></xp:this.rendered>
You must log on to see contents.
</xp:panel>

Panel 2: for authorized users

<xp:panel>
<xp:this.rendered><![CDATA[#{javascript:(@UserName() != "Anonymous"}]]></xp:this.rendered>
This is the secret content.
</xp:panel>

You can combine this with the XPages Dojo Login Custom Control available on OpenNTF so that the user can stay on the page when logging on instead of going to a seperate login page.

XPages Web Analytics custom control on OpenNTF

What a great weekend for open source contributions from me! 🙂 Yesterday I created the XPages SEO custom control – and today I have created a new XPages custom control for the OpenNTF development contest called XPages Web Analytics custom control: the custom control makes it easy to add web analytics to your XPages web site.

In the first release the custom control supports Google Analytics and Woopra Web Analytics.

In order to use the custom control you download it from OpenNTF, unzip the downloaded file and open the included Notes database. In the database you will find a custom control called “WebAnalytics” that you can include in your own XPages application. Once included you just drag the custom control to your XPage and add the custom properties for the specific web analytics providers that you would like to use.

XPages SEO custom control on OpenNTF

I have contributed to the OpenNTF development contest by creating an open source XPages Custom Control for Search Engine Optimization (SEO). The custom control makes it easy to add the following search engine friendly tags to your XPages web site:

  • page title
  • meta description
  • meta robots
  • meta keywords

With these SEO tags bundled in a custom control it also increases your chances of remembering to add these tags to your XPages web pages 🙂

Installation is easy:
Just download the SEO custom control, unzip the downloaded file and open the included Notes database. In the database you will find a custom control called “SEO” that you can include in your own XPages application. Once included you just drag the custom control to your XPage and add the custom properties for the specific XPage (either as static or computed values).

Controlling the HTTP response status code in XPages

This is a follow-up to my post on how to create custom 404 and error pages in XPages.

A very useful improvement to catching controlled errors such as “page not found” errors is to send the related HTTP status code in the response. So for “page not found” errors the HTTP response code 404 should be sent. This helps search engine crawlers understand that the page is not available whereby the search index can be updated accordingly.

The HTTP status code can be controlled with the facesContext response object by using setStatus. The following code example will generate a HTTP status code of 404 when added to the afterRenderResponse event of an error message XPage. The code example assumes that the source XPage sets the requestScope statusCode variable to “404” if the source XPage fails to find a document:

try {
	// The external context gives access to the servlet environment
	var exCon = facesContext.getExternalContext();

	// The servlet's response, check the J2EE documentation what you can do
	var response = exCon.getResponse();

	// Return HTTP status code 404 if necessary
	if (requestScope.statusCode = "404") {
		response.setStatus(404);
	}

	facesContext.responseComplete();
} catch(e) {
	_dump(e);
}