Skip to main content

Using DWR inside a Portlet deployed into JBoss Portal

Posted by edgars on December 11, 2007 at 8:14 AM PST

JBoss Portal is in its 2.6.2 GA version, which counts with an easy administration console, however it also counts with a strong foundation in terms of technologies.

In other hand, you have DWR (Direct Web Remoting), which provide you many interesting features in its 2.0 version, such as Comet(see some Grizzly). In my point of view a great approach behind the scenes in DWR is hide the Java Script from Java developers like. Un/fortunately I can use the time I would spend studing it for focus in other technologies.

I've been using Netbeans for Portlets development, in fact a portlet is an Web Application with some additional configuration files (xml). Basically, my structure for this sample is:

--portletdwr
---src (Java Sources)
---web (Web sources)
-----WEB-INF
---------lib (libraries)
---------classes (classes)
---------jsp (my jsp)
------------dwr (my jsp)
---------------view.jsp (my jsp)
---------web.xml (standard web descriptor)
---------dwr.xml (dwr descriptor)
---------dwrobject.xml (portlet dwr descriptor)
---------portlet.xml (standard portlet descriptor)
---------jboss-app.xml (jboss portal descriptor)


Basically, in order to compile your examples you need the portlets API jars, where you can find in JBoss Portal if you want. In addition, you must have available the dwr.jar and other dependencies as well. You can see the entire project here on this zip file.

Creating a Portlet Controller

If you have any other MVC experience, for example Struts or WebWork, you will see that the javax.portlet.GenericPortlet works as a Controller, handling http requests and dispatching responses to clients. For our example with DWR you just need override the method doView(), responsible for forward the some response to be rendered by the client browser. See the following source code:

package org.jboss.portal.samples.dwr;

import java.io.IOException;
import javax.portlet.GenericPortlet;
import javax.portlet.PortletException;
import javax.portlet.PortletRequestDispatcher;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
public class DWRPortlet extends GenericPortlet {

@Override
public void doView(RenderRequest request, RenderResponse response) throws PortletException, IOException {
  response.setContentType("text/html");
  PortletRequestDispatcher prd = getPortletContext().getRequestDispatcher
                                 ("/WEB-INF/dwr/view.jsp");
   prd.include(request, response);

    }
}

The view.jsp

Using DWR you must create a reference with DWR gerenated scripts, the correct way to do that is use Java request objects in order to get dinamically the context path, so the src portion from Javascript references must be exactlly as you can in the following code:

<%@ taglib uri="http://java.sun.com/portlet" prefix="portlet" %>
 
<%
String contextPath = renderResponse.encodeURL(renderRequest.getContextPath());
%>
script type="text/javascript" src='<%=contextPath%>/dwr/interface/Destination.js
script type="text/javascript" src='<%=contextPath%>/dwr/engine.js   
script type="text/javascript" src='<%=contextPath%>/dwr/util.js

Everything else is exactlly the same way you can do using DWR in simple java web applications.

References

- DWR (http://getahead.org/dwr/getstarted)
- JBoss Portal (http://labs.jboss.com/jbossportal/)

Screeshots

JBoss Portal 2.6.2-GA - Mozilla Firefox-1.png

Related Topics >>