XPages and Java (DanNotes 50th conference, November 2013)


Published on

Introduction to using Java and XPages (POJO, beans, scope variables and more).

Published in: Technology, Education
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

XPages and Java (DanNotes 50th conference, November 2013)

  1. 1. XPages and Java Introduction to using Java and beans in XPages Per Henrik Lausten DanNotes, November 2013
  2. 2. About Per Henrik Lausten • Web developer with my own one-man company, PHL Consult • Lead developer on Sherlock Web • Chairman of NotesNet – an assocation of 25 independent consultants • Member of the board at OpenNTF – open source for IBM Notes/Domino and IBM Connections • Member of the board at DanNotes • 2013 IBM Champion for IBM Collaboration Solutions • Mentor for XPages developers in several companies • 8K-rank on Stack Overflow with >250 answers primarily on XPages • Experienced XPages web application developer
  3. 3. Why Java and XPages? • XPages is Java! • XPages is based on JSF (JavaServer Faces) – From Mastering XPages: XPages is based on JSF version 1.1, although note that some important fixes from JSF version 1.2 and 2.0 have been applied to the XPages foundation layer; therefore, in reality, the XPages base version is more like 1.1++ • Server-Side JavaScript is interpreted at runtime (so Java is faster) • Java gives you access to many open source libraries, a better code editor and more • Let’s get started!
  4. 4. POJO versus bean • Plain Old Java Object (POJO) – An ordinary Java object • Bean – An ordinary Java object that adheres to certain rules: • • • • • Serializable No-argument constructor Private properties Public getter and setter methods Configured to a specific scope • (Notice: everything does not have to be a bean)
  5. 5. Well known beans in XPages • Data sources • Controls – Core controls – Container controls – Etc. • "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." Source: What the heck is a bean? by Tim Tripcony http://www.timtripcony.com/blog.nsf/d6plinks/TTRY-8GK6K7
  6. 6. Calling methods: POJO • Simple example calling a POJO method from Server-side JavaScript (SSJS): var myPOJO = new dk.dannotes.PojoObject(); var output = myPOJO.method(input); • Example: calling method using SSJS in QuerySave event of document data source: var myOtherPojo= new dk.dannotes.OtherPojoObject(); myOtherPojo.process(document);
  7. 7. Calling methods: Bean • SSJS var output = myBean.method(input); • Expression Language (binding to a field) <xp:inputText value="#{myBean.value}" id="fieldA" /> • myBean is defined in faces-config.xml (more about that later)
  8. 8. XPages scope variables • • • • Application (NSF) Session (user) View (page) Request (request)
  9. 9. Examples of scoped beans • Application scoped bean: – general configuration • Session scoped bean: – user settings – shopping cart • View scoped bean: – data processing similar to a document data source (for fields, for repeats/lists, etc.) • Request scoped bean: – EmailBean – PDF handling
  10. 10. Example: app scoped bean package dk.dannotes; public class Config implements Serializable { private static final long serialVersionUID = 6469339826789980362L; private String propertyA; <faces-config> private Vector propertyB; public Config() { init(); } public void init() { setPropertyA("A"); setPropertyB("B"); } <managed-bean> <managed-bean-name>config</managed-beanname> <managed-beanclass>dk.dannotes.Config</managed-bean-class> <managed-beanscope>application</managed-bean-scope> </managed-bean> </faces-config> public void setPropertyA(String propertyA) { this.propertyA = propertyA; <xp:text } public String getPropertyA() { return propertyA; <xp:text } public void setPropertyB(Vector propertyB) { this.propertyB = propertyB; } public Vector getPropertyB() { return propertyB; } } .. value="#{config.propertyA}" /> .. value="#{config.propertyB}" />
  11. 11. More examples • Apache POI: Java API for Microsoft documents • PDF generation • Other binary output (see session later by John Foldager) • Calling backend web services • Using 3rd party services – Microsoft Exchange Web Services Java API
  12. 12. How? • Create your Java class • Register your Java class as a bean in faces-config.xml • Use your bean: #{helloWorld.someVariable} DEMO Source: Creating your first managed bean for XPages http://per.lausten.dk/blog/2012/02/creating-your-first-managed-bean-for-xpages.html
  13. 13. Using variable resolver public static Object resolveVariable(String variable) { return FacesContext.getCurrentInstance().getApplication().getVariableResolver() .resolveVariable(FacesContext.getCurrentInstance(), variable); } • Accessing current session: Utils.resolveVariable("session"); • Accessing current database: Database myDb = Utils.resolveVariable("database"); • Accessing currentDocument: DominoDocument myXspDoc = Utils.resolveVariable("currentDocument"); • Accessing other beans: Utils.resolveVariable("beanName");
  14. 14. Using variable resolver: getInstance() private static final String BEAN_NAME = "config"; // access to the bean public static Config getInstance() { return (Config) Utils.resolveVariable(BEAN_NAME); } String propertyA = Config.getInstance().getPropertyA();
  15. 15. Error messages • Writing error messages to your Display Errors control : FacesContext.getCurrentInstance().addMessage( "messages1", new FacesMessage(FacesMessage.SEVERITY_ERROR, msg, "") );
  16. 16. To recycle or not to recycle? • The classic IBM lotus.domino Java API – Using Notes objects in XPages and in Java require that you recycle those objects in order to avoid memory leaks and backend out of memory issues – All Lotus object instances • myDatabase.recycle(); • myView.recycle(); • myDoc.recycle(); – Don't forget columns • Vector colValues = myView.getColumnValues(); • session.recycle(colValues); – Don't forget NotesDateTime objects • DateTime myDate = session.createDateTime("Now"); • myDate.recycle(); – Don't forget! • The new OpenNTF Domino API (9.0+) – No recycling required at all! – See session on the OpenNTF Domino API by Paul Withers later today Source: How to recycle Notes objects in XPages and Java http://per.lausten.dk/blog/2013/05/how-to-recycle-notes-objects-in-xpages-and-java.html
  17. 17. Debugging • Poor Man’s Debugger – System.out.println(String msg); • Use the XPages Debug Toolbar from Java – DebugToolbar.get().info( String msg ); • Use the Domino server Java debugger • Also go to Mark Leusinks session later today
  18. 18. Recommendation • Use Java and go "all in" • Use Java for as much as possible (including your own document data sources) • It's a journey from using SSJS only to (almost) using Java only • Get more inspiration in the rest of today's sessions
  19. 19. Need help? • Contact the 'Gang of four' – Per Henrik Lausten: phl-consult.dk – Jakob Majkilde: majkilde.dk – John Dalsgaard: dalsgaard-data.dk – John Foldager: izone.dk