Portlets, Tools, AJAX, and more...
GregZ Weblog
Archives
« November 2009
SunMonTueWedThuFriSat
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
     
       
Today
XML
Search

Links
 

Today's Page Hits: 7

All | Everything
« GoogleSearch Portlet | Main | Getting Portlets... »
20060117 Tuesday January 17, 2006
AJAXPortlet Revisited
Several months ago I wrote an article on using AJAX with portlets.  There were a couple of comments that came in that I wanted to try to address.

1. Multiple AJAX portlets on the same page will have their Javascript conflict.

The Javascript variables and functions should be namespaced to avoid conflicts between multiple AJAX portlets on the same page.  For instance,

var <portlet:namespace/>portletReq;

function <portlet:namespace/>asynchGet(updateURL){
...
Generates Javascript something like:
var AJAXPortlet_AJAXPortlet_AJAXPortletportletReq;

function AJAXPortlet_AJAXPortlet_AJAXPortletasynchGet(updateURL){
...

In this way, each portlet that might include similar Javascript functions (or variables) would have them namespaced appropriately to avoid conflicts.

2. An AJAX portlet will not work with WSRP.

The reference to the InvoiceServlet must be encoded in order to be resolved correctly with WSRP.  For instance,

<%=renderResponse.encodeURL("/AJAXPortlet/InvoiceServlet")%>
Returns something like:
http://host.com/portal/wsrpconsumer/resourceproxy?
wsrp_url=http%3A%2F%2Fhost.com%2FAJAXPortlet%2FInvoiceServlet&
wsrp_rewrite=false&
namespace=N_JSPTabContainer_WSRPAJAXPortlet&
portal_server_url=http%3A%2F%2Fhost.com%2Fportal&
cookie_key=5VR2N5IAAAAABAMTGHTQAAA_cookieHandle

However, the encoded reference to the InvoiceServlet needs the invoice parameter added. That will require some manipulation of the encoded URL string returned from renderResponse. In our case you will have to search the returned URL string for the wsrp_url paramater and insert "?invoice=" + select.value. All this would have to go in the selectInvoice Javascript function. This should resolve the problem where this AJAX portlet does not work with WSRP.

posted by gregz Jan 17 2006, 08:58:24 PM MST Permalink Comments [8]

Trackback URL: http://blogs.sun.com/gregz/entry/ajaxportlet_updates
Comments:

What happens when the portlet page is re-rendered? How do you keep track of the new AJAX-derived content through subsequent request-response cycles?

Posted by Craig Doremus on February 01, 2006 at 03:49 PM MST #

Hey Craig, one possible way would be to store the state of the asynchronous content. In our case since the asynchronous requests are to the InvoiceServlet, you could store the invoice number in HttpSession each time a request is made. Then when a new portlet request-response cycle happens, you could read the last viewed invoice from the HttpSession.

Posted by GregZ on February 03, 2006 at 03:07 PM MST #

Hi Greg, Thanks for the reply. Have you thought about a transparent way for keeping track of AJAX-derived content between requests? Perhaps you could use some sort of complex session Object that contains a HashMap of AJAX content keyed by the HTML element id and refreshed in the onLoad JS event. I am on the JSR-286 EG. Making AJAX transparent in a portlet environment is something we need to do for Portlet 2.0.

Posted by Craig Doremus on February 22, 2006 at 05:20 AM MST #

This is a creative suggestion. Yes, I think AJAX (asynchronous portlet requests) will be a important feature for JSR-286.

Posted by GregZ on February 23, 2006 at 09:51 AM MST #

Hi very good article thanx I have some AJAX portlets. As said in section 5.2.4, page 24, of JSR 168, each call to a URL of a Portlet in portal will generate one “processAction" for target Portlet and one “render” for other Portlets in the page, in AJAX scenario this will lead to lots of unnecessary call to render method of Portlets in the page, for each AJAX call. Do you know any good pattern other than Portlet caching to avoid portal calling render method of each Portlet in the page? Best regards Arash Rajaeeyan

Posted by Arash Rajaeeyan on August 13, 2006 at 05:46 AM MDT #

Hi Arash, You are correct. As the spec states, an actionURL will generate a render request after processAction to re-render the portlet (and the portal page). And in general, the same is the case for all PortletURLs (render and action). Because of this, PortletURLs tend not to be useful for AJAX requests. The content returned will be the _entire_ portal page, not the content for the specific portlet. As the article mentions, we are using the InvoiceServlet (that is bundled with the portlet web-app) to make AJAX calls to. For now this is the best alternative to using a PortletURL. A follow-up article to this one is in review right now and should be published soon that will expand on these issues. Thanks. GregZ

Posted by GregZ on August 16, 2006 at 05:14 PM MDT #

Hi Greg, can you post a new version of the AJAX portlet sample code with these modifications? Thanks. Paul

Posted by Paul Tran on October 19, 2006 at 02:06 PM MDT #

Hi Paul, Please see my latest article on this. http://blogs.sun.com/gregz/entry/best_practices_for_ajax_and The new sample code is linked from there and will soon be available at our Portlet Repository (https://portlet-repository.dev.java.net/). Thanks. GregZ

Posted by GregZ on October 19, 2006 at 04:28 PM MDT #

Post a Comment:

Name:
E-Mail:
URL:

Your Comment:

HTML Syntax: NOT allowed