xpages and java (dannotes 50th conference, november 2013)

19
XPages and Java Introduction to using Java and beans in XPages Per Henrik Lausten DanNotes, November 2013

Upload: per-henrik-lausten

Post on 14-May-2015

3.185 views

Category:

Technology


1 download

DESCRIPTION

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

TRANSCRIPT

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

XPages and JavaIntroduction to using Java and beans in XPages

Per Henrik LaustenDanNotes, November 2013

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

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

• Contact me: +45 6086 8400, [email protected], @perlaustenphl-consult.dk, per.lausten.dk/blog

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

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!

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

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)

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

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 Tripconyhttp://www.timtripcony.com/blog.nsf/d6plinks/TTRY-8GK6K7

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

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);

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

Calling methods: Bean

• SSJSvar 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)

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

XPages scope variables

• Application (NSF)• Session (user)• View (page)• Request (request)

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

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

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

Example: app scoped beanpackage dk.dannotes;public class Config implements Serializable {

private static final long serialVersionUID = 6469339826789980362L;private String propertyA;private Vector propertyB;

public Config() {init();

}

public void init() {setPropertyA("A");setPropertyB("B");

}

public void setPropertyA(String propertyA) {this.propertyA = propertyA;

}public String getPropertyA() {

return propertyA;}public void setPropertyB(Vector propertyB) {

this.propertyB = propertyB;}public Vector getPropertyB() {

return propertyB;}

}

<xp:text .. value="#{config.propertyA}" />

<faces-config><managed-bean>

<managed-bean-name>config</managed-bean-name> <managed-bean-

class>dk.dannotes.Config</managed-bean-class> <managed-bean-

scope>application</managed-bean-scope></managed-bean>

</faces-config>

<xp:text .. value="#{config.propertyB}" />

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

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

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

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 XPageshttp://per.lausten.dk/blog/2012/02/creating-your-first-managed-bean-for-xpages.html

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

Using variable resolverpublic 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");

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

Using variable resolver: getInstance()

private static final String BEAN_NAME = "config";

// access to the beanpublic static Config getInstance() {

return (Config) Utils.resolveVariable(BEAN_NAME);}

String propertyA = Config.getInstance().getPropertyA();

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

Error messages

• Writing error messages to your Display Errors control :FacesContext.getCurrentInstance().addMessage(

"messages1", new

FacesMessage(FacesMessage.SEVERITY_ERROR, msg, ""));

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

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 Javahttp://per.lausten.dk/blog/2013/05/how-to-recycle-notes-objects-in-xpages-and-java.html

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

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

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

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

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

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