Expanding your use of EL (Part 1)

Jul 23, 2013 1:07 PM

Tags: el java
  1. Expanding your use of EL (Part 1)
  2. Expanding your use of EL (Part 2)

One of the main aspects to cleaning up the code of an XPage is the heavy use of Expression Language (EL), which is XPages/JSF's shorthand language for binding properties to values and methods. The most common kind you'll run across is the kind you get for "free" when working with document data sources:

<xp:inputText value="#{doc.FirstName}"/>

This is the essence of EL in a nutshell: the actual code behind the scenes can be pretty complex, but EL handles that for you. It goes far beyond this, though, in two important ways: accessing custom and non-Domino data sources and writing complex operations.

Accessing Custom Data Sources

In addition to accessing "built-in" data types like Domino documents and the scope variables, EL is generic enough to deal with several kinds of data access (technically, it ONLY does these, and the standard bindings are examples of it):

  • Lists (and probably arrays). EL can use bracketed-subscript notation to reference individual elements of a Java list/array, e.g. #{names[3]}. I've found this to be the rarest use, but it could be useful if repeating over two parallel lists.
  • Maps. This is how EL accesses the scope variables (requestScope, viewScope, etc.). It translates code like #{sessionScope.cart} to sessionScope.get("cart") and sessionScope.put("cart", ...).
  • DataObjects. This is basically a "Map-lite" interface from IBM that is meant for making generic key/value objects easily-accessible via EL - this is what Domino document objects implement. It translates code like #{doc.FirstName} to doc.getValue("FirstName") and doc.setValue("FirstName", ...).
  • Generic bean-style methods. If (and only if) your object doesn't implement any of the above interfaces, EL falls back to seeking out bean-style getters and setters. For example, if you have a class that implements no special interfaces, EL will attempt to match #{myObject.foo} to myObject.getFoo() (or myObject.isFoo()) and myObject.setFoo(...).

The first important thing to remember is that EL applies these rules to your own and third-party objects just as much as it does to IBM's stock objects. If your code is just #{doc.foo}, you can switch "doc" from a Domino document to a HashMap, a custom DataObject class, a bean with getFoo, or an entirely-different third-party data source that implements one of those and it will continue to work smoothly. If you instead wrote it as #{javascript:doc.getItemValueString('foo')}, your experience would be... a bit rougher.

The second important thing to remember is EL's equivalence between dot and bracket notations. What I mean by this is that these three are all equivalent (assuming "barHolder" is a variable on the page containing the string "bar"):

  • #{foo.bar}
  • #{foo['bar']} or #{foo["bar"]}
  • #{foo[barHolder]}

Note that those are equivalent for every supported object type, including generic beans (okay, but not Lists, because #{foo.1} is illegal). This equivalence allows you to use EL to reference keys that would not be legal in dot notation (e.g. #{someProperties['us.iksg.setting']}) and to use other EL expressions to determine the field name. Say you're keeping some user-specific info in the application scope; you could end up with a chain like this to access it via EL:

<xp:text value="#{applicationScope.userCache[context.user.name].ticketCount}"/>

The more you understand the different ways of accessing your objects via EL, the more you can remove SSJS from your XPage markup and make your apps cleaner, (probably) faster, and more portable.

In my next post, I will cover the second major topic: complex operations and comparisons in EL.

Commenter Photo

Paul Withers - Jul 23, 2013 1:23 PM

It's also worth reminding folks that you can combine lanagues, so for that last example, you could use:

value="Ticket count is #{applicationScope.userCache[context.user.name].ticketCount}"

Commenter Photo

Jesse Gallagher - Jul 23, 2013 1:30 PM

Excellent point - that's a great way to save a ton of code.

Commenter Photo

Nathan T. Freeman - Jul 23, 2013 4:20 PM

It's worth noting that when you chain EL like this...

#{applicationScope.userCache[context.user.name].ticketCount}

...if there's a problem inside the chain, like userCache returning null, the EL will frequently report the WRONG element of the expression having a problem. The simplest way I know of to debug EL is to switch back to SSJS, where you can either debug or include trace information to find out what's happening, then switch back to EL when you've corrected the problem.

 

Commenter Photo

smith - Jan 19, 2020 12:53 AM

s

Commenter Photo

smith - Jan 19, 2020 1:27 AM

This web site is often a walk-through for all of the knowledge you wanted concerning this and didn’t know who must. Glimpse here, and you’ll absolutely discover it. https://itsportshub.com/

Commenter Photo

smith - Jan 19, 2020 1:27 AM

The the next occasion Someone said a weblog, Hopefully so it doesnt disappoint me approximately this. What i’m saying is, I know it was my choice to read, but I actually thought youd have something interesting to express. All I hear is often a number of whining about something that you could fix if you werent too busy searching for attention. https://thesportshint.com/

Commenter Photo

smith - Jan 19, 2020 1:28 AM

An interesting discussion is worth comment. I do think that you should write read more about this topic, it will not be considered a taboo subject but usually everyone is too few to communicate in on such topics. To another. Cheers https://itscurrentfashion.com/

Commenter Photo

smith - Jan 19, 2020 1:28 AM

Generally I try and get my mix of Vitamin E from pills. While I’d really like to through a fantastic meal plan it can be rather hard to at times. https://entertainmentbee.com/

Commenter Photo

smith - Jan 19, 2020 1:28 AM

I take great pleasure in reading articles with quality content. This article is one such writing that I can appreciate. Keep up the good work. https://homeimprovementall.com/

Commenter Photo

smith - Jan 19, 2020 1:28 AM

Appreciate it for this grand post, I am glad I discovered this web site on yahoo. https://petrefine.com/

Commenter Photo

smith - Jan 19, 2020 1:28 AM

The attractiveness of those blogging engines and CMS platforms could be the lack of limitations and ease of manipulation that makes it possible for developers to put into action prosperous content and ’skin’ the website in such a way that with really tiny work one particular would never ever discover what it is generating the website tick all devoid of limiting material and effectiveness. https://thepetsabout.com/

Commenter Photo

smith - Jan 19, 2020 1:28 AM

very good good…this post deserves almost nothing …hahaha merely joking: S …nice write-up: P https://lifestyleabout.com/

Commenter Photo

smith - Jan 26, 2020 3:20 AM

You can increase your blog visitors by having a fan page on facebook.’.: koinfekcje boreliozy

Commenter Photo

smith - Jan 28, 2020 2:11 AM

An impressive share, I simply given this onto a colleague who had been doing a small analysis with this. And then he the truth is bought me breakfast because I discovered it for him.. smile. So well then, i’ll reword that: Thnx for any treat! But yeah Thnkx for spending plenty of time to talk about this, Personally i think strongly regarding it and enjoy reading on this topic. When possible, as you become expertise, do you mind updating your blog site with increased details? It is highly ideal for me. Big thumb up due to this text! Japji Khaira

New Comment