aspose file tools*
The moose likes JSF and the fly likes EL - Without FacesContext Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » JSF
Bookmark "EL - Without FacesContext" Watch "EL - Without FacesContext" New topic
Author

EL - Without FacesContext

Ashish Sarin
author
Ranch Hand

Joined: Nov 20, 2000
Posts: 432
    
    5
Hi,

The Expression Language that is currently used in JSF requires the FacesContext ( associated with the faces request ) for evaluation. This is obvious because FacesContext is used to resolve the managed bean name that is used in the expressions.

If I use AJAX ( which I am using ) then the HTML or HTML fragments that are returned from the server are pretty much static in nature. I was able to get around this problem by using a templating mechanism, which means that I started composing HTMLs as we do it in Struts tiles ( ie., HTMLs with placeholders and each placeholder can be another HTML file or just a dynamic data ).

There still is one problem: In JSF we have the 'rendered' attribute which can be used to show/hide a JSF component. I was trying to come up with something similar in the HTMLs that I have. So i can have something like this in my HTML:
<exp:if value="#{someManagedBean.accessBean.saveAllowed}">
<input type="button" name="Save" value="Save" onclik="doSomething;"/>
</exp:if>

If the expressions inside the 'value' attribute is simple then I can create a simple expression framework to parse the expression (for example, by using the BeanWrapper implementation of Spring framework) and on the basis of the result I will show/hide the 'Save' button - Functionality which will be similar to the 'rendered' attribute. BUT, if the expressions are complex then I will have to write a complete expression framework to handle all possible scenarios ( ie. what to do when its a property is a Map or its a List ).

If I can somehow use the expression language of the JSF outside the faces context then I don't need to re-invent the wheel and it will also bring consistency in my application. If I can customize the behavior of EL framework, I would like the EL framework to obtain the managed bean instance from session/request and not from the FacesContext. Is there any provision avaialble in EL to achieve this functionality ?

thanks
Ashish
[ July 29, 2007: Message edited by: Ashish Sarin ]

Author of
Getting started with Spring Framework
Portlets in Action
Spring Roo 1.1 Cookbook
Portlets blogSpring Roo blog
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16145
    
  21

Yes, and it's exactly the same mechanism that at least one JSF implementation uses:

http://commons.apache.org/el/

The key is that if you include this jar and its supporting facilities - I think you'll need beanutils at a minimum - in your app and construct objects using your own private symbol contexts, you can take advantage of having your own pre-debugged EL interpreter.

In fact, this is exactly what the JSF servlet does, except that it's using the JSF bean management framework as its dictionary.

I'm not sure if you really need that much power, since I haven't quite visualized what you're attempting, but if you do it's there. Just make sure you do everything with the understanding that the JSF servlet has already set up one EL processing context and be sure you obey whatever rules there are to prevent resource collisions.


Customer surveys are for companies who didn't pay proper attention to begin with.
Ashish Sarin
author
Ranch Hand

Joined: Nov 20, 2000
Posts: 432
    
    5
Hi Tim,

Thanks for the response. I had a look Apache Commons EL framework but couldn't find any documentation on it. I will look again.

I will just give you a background on what I am trying to do:

I have been able to achieve a few things effectively with the approach that I talked about. When using AJAX, sometimes its required that you create a complex web page corresponding to an AJAX request. Currently the frameworks based on AJAX only provide a way to invoke a server-side Java method and allow you to return Text/XML as the response. Thats it. But in any business application you need more than that. For example, the return HTML from the server represents a data table (alongwith a datascroller component) and the column header are supposed to allow you sorting. This is only possible if we make use of tiles/templating feature while composing these HTMLS. This i could achieve programmatically ( and I pretty sure I can externalize this templating feature into an XML file just the way we do it in Struts).

Even if I use tiling feature i still need some way to customize portions of the HTML or they will not be usable across different pages. One way is that I customize it programmatically, which is not good because the code now carrys the logic which could have been easily pushed to the HTML by using some kind of an expression language.

Working on a complex application made me realize that AJAX can't be used easily in generating complex web pages unless AJAX frameworks are extended to provide the same features as it is available to technologies like JSP. JSP has JSTL, expression language, etc. I think somewhere (at post-processing) if AJAX frameworks can do the same things as JSP engine does to JSP page I think it will be pretty easy to create a complex web page. I am not sure how many people will agree with my point of view but recently when I was using AJAX ( DWR framework specifically ), I realized that either I am using AJAX wrongly or the AJAX doesn't exactly meet the requirements for developing complex web pages on the fly.

In the beginning I thought it may be performance intensive but theoretically JSP engine does so many things to JSP pages so it can't be slower than JSP page processing if I apply the same JSP engine to process HTML pages that are output by AJAX frameworks.

I have recently done some work on this and I am not disappointed. The performance of the application is atleast better than what I thought.

There will be no resource collission when using expression framework to process expressions in an HTML because the base object in expressions will still be picked from JSF managed beans ( that have a session scope ).

Hope this clarifies to some extent the approach that I have taken.

-Ashish
[ July 30, 2007: Message edited by: Ashish Sarin ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: EL - Without FacesContext