Introduction to the Java Portlet Specification
Putting It All Together
Tying Up Loose Ends
The GenericPortlet class provides a default implementation of the Portlet interface. By extending GenericPortlet, portlet developers can take advantage of built-in interpretation of Portlet Modes and default implementations of the lifecycle methods. The Hello World example below leverages the GenericPortlet implementation.
Portlet Tag Libraries
The portlet specification defines tag libraries that are used to develop JSPs that render portlet fragments. This library contains tags that can be used to define portlet objects within a page, generate portlet urls, and generate unique portlet namespaces. The example below utilizes these tag libraries to generate the edit page of the portlet (see example code download).
Hello World!, Portlet Style
public void doView(RenderRequest req, RenderResponse res) throws IOException { PrintWriter out = res.getWriter(); out.println(getGreeting(req) + " World!"); }
When the portal's controls are used to switch the portlet mode to edit, the portlet will render the edit screen that allows the user to change their greeting. doEdit is invoked by GenericPortlet's implementation of render. In this method, you utilize a request dispatcher to forward the request to a JSP that will generate this content fragment.
public void doEdit(RenderRequest req, RenderResponse res) throws PortletException, IOException { PortletRequestDispatcher dispatcher = getPortletContext().getRequestDispatcher("/edit.jsp"); dispatcher.include(req, res); }
The JSP page utilizes several portlet tags to generate the action URL that will be used to process this page.
<%@ taglib uri="http://java.sun.com/portlet" prefix="portlet"%> <portlet:defineObjects/> <portlet:actionURL var="url" portletMode="view"/> <FORM method="POST" action="<%= url %>"> <TABLE> <TR><TD>Greeting</TD> <TD><INPUT type="text" name="greeting" value="<%= renderRequest.getParameter("greeting") %>"/> </TD> <TD colspan="2"><INPUT type="submit" value="Save"/></TD> </TR> </TABLE> </FORM>
Finally, upon submission, the container calls the portlet's processAction method. This method sets a render parameter that can be utilized while rendering the portlet.
public void processAction(ActionRequest req, ActionResponse res) throws PortletException { String greeting = getGreeting(req); res.setRenderParameter("greeting", greeting); }
The full source of the example portlet above is available for download.
Conclusion
The basics of portlet development are very similar to those of servlet development. Web application developers who are aware of the basics of portlet development, including the generation of fragments and the separation of action and render requests, should find portlet development fairly familiar. In my next article, I will address the deployment of portlets within Apache Pluto (the reference implementation of the Java Portlet Specification) and show how using Pluto as a lightweight development environment can increase productivity.
References
- The Java Portlet Specification
- The Java Portlet API
About the Author
David DeWolf has been developing Web applications, portals, and portlet applications for six years. He is a member of the Apache Portals Project Management Committee and an active committer to Apache Pluto, the reference implementation of the Portlet Specification. David currently works at Digital Focus, which provides software development, agile coaching, and IT consulting services to Fortune 1000 and medium-sized businesses. Contact David at author@daviddewolf.com.
Page 2 of 2