Creating your first managed bean for XPages
February 8th, 2012With 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:
- The Discussion and Teamroom templates in the Extension Library that both use beans
- Mastering XPages page 415 for a Java class example
- Mindoo: XPages series #4: Backing Bean Management with XPages by Karsten Lehmann
- Mindoo XPages blog series on managed beans (and more) by Karsten Lehmann
- xpagesblog.com: Writing a Managed Bean to Automate Server Side Functionality in XPages by Jeremy Hodge
- xpagesblog.com: Getting Deeper into the Managed Bean - Creating a Cached Master/Detail Recordset by Jeremy Hodge
- What the heck is a bean? by Tim Tripcony (with this great quote by Tim: "Why is it crucial to understand the nature of beans when developing XPages, even if you're not specifically writing Java code? Because darn near everything in an XPage is a bean.")
- Tim Tripcony on managed beans and themes (and much more): Taking themes to the next level
- Update: Jeremy Hodge has done a 3 part video series on getting started with managed beans for Notesin9. Its called "Intro to Java for XPages developers" and can be found at XPages.TV
Tags: Java, Lotus Domino Designer, XPages
February 8th, 2012 at 13:34
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
February 8th, 2012 at 13:45
Hi David, thanks!
I have updated the post to include links to the Jeremy Hodge show on XPages.TV.
February 8th, 2012 at 13:52
Nice, that's the kind of tutorials I like.
February 8th, 2012 at 14:48
Thanks Per - can't wait for the next installment 🙂
February 8th, 2012 at 20:51
Very clear and easy to read! Maybe you should copy this over to the Appdev wiki?
May 10th, 2012 at 16:48
[...] [...]
October 31st, 2012 at 12:28
[...] can start using Per Hendrik Lausten’s blog post as a starter to get more into managed beans. Jeremy Hodge also covers the topic [...]