Wednesday, September 23, 2009

Google Web Toolkit + JSR 168 portlets

I want GWT. No, not JSF or other stuff like Struts (uugghh!) with JavaScript libraries, where you also have to care about sort of XML configs, JSP etc etc etc. Forget it. Use GWT.

But Enterprise wants portal with portlets (and me too). So how to make a "standalone" portlet in a nice .war file, while using GWT and such for this? I use GlassFish server and NetBeans IDE, so my sharing here towards these components only. Here is how I managed this to work:
  1. Get Portlet Container Driver from OpenPortal for a start and follow the instructions to let it get working on your GlassFish (simply run configurator jar and point directory and domain).
  2. Install Portal Pack in your NetBeans IDE.
Now, how to create a portlet with GWT:
  1. Create a new project, called "Java Web Application" in your NetBeans with GWT and PortalPack support enabled. Nothing else is needed for our example.
  2. Let's say, the name of our project is "GWTPortlet" and let's say it is in "net.maryniuk.portlet" package.
  3. NetBeans will generate for you a skeleton for your portlet. It is all what you actually need from non-GWT part.
  4. Create GWT client, that should be in "net.maryniuk.portlet.client". It will add some JavaScript and meta tags to "welcomeGWT.html" file, which you can find in "Web Pages" folder. Get these two tags (javascript and meta) and put them into "GWTPortlet_view.jsp" file, that you can find in "Web Pages/WEB-INF/jsp" folder.
  5. Change path in JavaScript tag the way it find generated JavaScript by GWT. For example, if your tag looks like: <script src="foo.js">, then change it to <script src="/GWTPortlet/foo.js">.
  6. Add a <div id="foo"></div> tag with an ID (in our example "foo") somewhere inside that JSP page.
  7. In your GWT entry point, change RootPanel.get()... to RootPanel.get("foo")... so then GWT can lookup an element with an ID "foo" and place itself there.
  8. Build your project. You're done — subdirectory "dist" contains your .war file with GWT portlet!
  9. Now just deploy it inside your Portlet Driver and see the result.

Here how it looks like for me:

Later on, after you sure your portlet works as expected, you can deploy it on Liferay or Sun Webspace:


Luca Agostino said...

Hi, I've tried this method, but it only works without gwt rpc it normal or I miss something?
Thank you if you want to reply..

BM said...

Yup, you're missing everything. :-) Google RPC calls works exactly as expected. Just make sure your JavaScript URL's are valid and your portlet is calling them.

In fact, what you see in a browser is just a regular JavaScript that calls server through Ajax.