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 - Nov 19, 2019 4:11 AM

we will be buying more christmas ornaments these christmas because we like to decorate more~ best business circle

Commenter Photo

smith - Nov 19, 2019 4:11 AM

i gave my girlfriend a diamond bracelt and she was quite pleased with it;; it business mind

Commenter Photo

smith - Nov 19, 2019 4:12 AM

Advantageously, the article is actually the greatest topic on curing acne naturally. I concur with your conclusions and will eagerly look forward to your coming updates. Saying thanks will not just be enough, for the phenomenal lucidity in your writing. I will immediately grab your rss feed to stay informed of any updates. https://thetechnologyvision.com

Commenter Photo

smith - Nov 19, 2019 4:12 AM

i get my art books from barnes and noble that come or amazon, they have some great discount coupons too. the technology vision

Commenter Photo

smith - Nov 19, 2019 4:12 AM

What your stating is absolutely accurate. I know that everyone have to say the identical factor, but I just feel that you put it in a way that absolutely everyone can realize. I also really like the photographs you set in right here. They fit so effectively with what youre hoping to say. Im sure youll achieve so numerous men and women with what youve acquired to say. get tech skill

Commenter Photo

smith - Nov 19, 2019 4:12 AM

Maintain up the excellent work mate. This website publish shows how well you recognize and know this subject. https://travelgudier.com/

Commenter Photo

smith - Nov 19, 2019 4:12 AM

Great post, thanks. I just shown interest in your feed! the traveler season

Commenter Photo

smith - Nov 19, 2019 4:13 AM

some stores have really bad customer service while others have topnotch custmer service,, the general world

Commenter Photo

smith - Nov 19, 2019 4:13 AM

I’m impressed, I have to admit. Genuinely rarely do you encounter a blog that’s both educative and entertaining, and without a doubt, you could have hit the nail to the head. Your concept is outstanding; ab muscles something inadequate people are speaking intelligently about. We’re happy i found this during my seek out something in regards to this. https://thegeneralworld.com/

Commenter Photo

smith - Nov 19, 2019 4:13 AM

there are many hobbies out there but there is no other hobby like fishing, fishing is every enjoyable. ideal real state

Commenter Photo

smith - Nov 19, 2019 4:13 AM

Your article constantly have many of really up to date info. Where do you come up with this? Just declaring you are very creative. Thanks again https://thegeneralinfo.com/

Commenter Photo

smith - Nov 19, 2019 4:13 AM

well, i do believe in astrology and horoscope is the first part of the magazine that i always check~ top online general

Commenter Photo

smith - Nov 19, 2019 4:13 AM

my daughter have been doing some scrapbooking stuffs over the years and she really likes it,. https://futurehealthlife.com/

Commenter Photo

smith - Nov 19, 2019 4:13 AM

Nice post. I learn one thing tougher on different blogs everyday. It should at all times be stimulating to learn content from different writers and observe slightly something from their store. I’d desire to use some with the content on my weblog whether you don’t mind. Natually I’ll give you a link in your web blog. Thanks for sharing. https://healthlifeservices.com/

Commenter Photo

smith - Nov 19, 2019 4:14 AM

There are numerous fascinating time limits in this posting however I don’t know if I see all of them center to heart. There is some validity however I’m going to take hold opinion until I check into it further. Good article , thanks so we want extra! Included in FeedBurner as properly casino games media

Commenter Photo

smith - Nov 19, 2019 4:14 AM

Thank you for finding the time to debate this, Personally i think strongly over it and love learning read more about this topic. Whenever possible, when you gain expertise, could you mind updating your blog with extra information? It is very great for me. http://firstlyrics.com

Commenter Photo

smith - Nov 19, 2019 4:14 AM

depending on fossil fuel is always a bad idea, we should always concentrate on renewable energy.. Web 2.0

Commenter Photo

smith - Nov 19, 2019 4:14 AM

Your blog is amazing dude. i love to visit it everyday. very nice layout and content .”    audition dateand place

Commenter Photo

smith - Nov 19, 2019 4:14 AM

I am impressed with this web site , rattling I am a big fan . apk millions

Commenter Photo

smith - Nov 19, 2019 4:14 AM

This can be an incredibly amazing powerful resource that you’re offering and you simply provide it away cost-free!! I comparable to discovering websites which view the particular worth of offering you an excellent learning resource for zero cost. We truly dearly loved examining this article. Be thankful! https://getandroidemulator.com/

Commenter Photo

smith - Nov 19, 2019 4:15 AM

Hey! This post couldn’t always be written much better! Examining this post reminds me of these area mate! He always stored talking about this. I’ll ahead this article to be able to your ex. Convinced he will have a very good go through. We appreciate you revealing! wood gram

Commenter Photo

smith - Nov 19, 2019 4:15 AM

I appreciate your wordpress template, wherever did you get a hold of it through? https://techvictoria.com/

New Comment