AquaLogic User Interaction Development Guide

     Previous Next  Open TOC in new window   View as PDF - New Window  Get Adobe Reader - New Window
Content starts here

Creating a Custom ALI Portlet with the Java IDK Portlet API

This simplified Hello World portlet example allows a user to set the message that is displayed within a portlet.

Before writing any code, create a new IDK project as described in Setting Up a Custom Java IDK Project in Eclipse Stand-Alone (without WTP) or Setting Up a Custom Java IDK Project in Eclipse with WTP.

This example uses two pages: a portlet that displays the current setting value and a form for changing the value, and a page that sets the value in the ALI database and redirects to the portal page.

In the new IDK project, create a new JSP page for the portlet (portlet.jsp). The portlet code shown below instantiates the IDK and uses the IDK Portlet API IPortletRequest object to check for a Portlet setting called "PortletEntry." If the setting has an associated value, the portlet displays it. The portlet also displays a form that allows the user to enter a value. When the user clicks Submit, the portlet code sends the value from the form in a request to the setPrefs.jsp page, shown next.
Note: There is no need to include html, head and body tags; the portlet is displayed as part of the HTML table that makes up the portal page.
<%@ page language="java" import="com.plumtree.remote.portlet.*,java.util.Date" %>
You refreshed at <%= new Date().toString()%><br/>

<%
//get the idk
IPortletContext portletContext = PortletContextFactory.createPortletContext(request, response);
IPortletRequest portletRequest = portletContext.getRequest();
String settingKey = "PortletEntry";

String settingValue = portletRequest.getSettingValue(SettingType.Portlet, settingKey);

//if the entry has already been set, display it here
if (null != settingValue)
{
%>
  <br/><b> Preference value is <%=settingValue%></b>!<br/>
<%
}  

//form to enter the preference
%>
<P>Enter your preference:
<form METHOD="post" ACTION="setPrefs.jsp" name="form1">
<input type="text" name="<%=settingKey%>">
<br/>
<input type="submit" name="Submit" value="Submit">
</form>
Next, create the Set Preferences page (setPrefs.jsp). The code shown below gets the value for the PortletEntry Portlet setting from the request, then uses the IDK Portlet API IPortletResponse object to add the setting to the database and redirect to the portal. The redirect causes the portal page to refresh and display the updated setting in the portlet.
<%@ page language="java" import="com.plumtree.remote.portlet.*" %>

<%
//set the cache control so we don't get a cached page
response.setHeader("Cache-control", "max-age=0");

//get the idk
IPortletContext portletContext = PortletContextFactory.createPortletContext(request, response);  

//get IPortletResponse to set preferences and redirect back to the portal
IPortletResponse portletResponse = portletContext.getResponse();

//get the setting value from the servlet request
String settingKey = "PortletEntry";
String settingValue = request.getParameter(settingKey);

//set the setting value
portletResponse.setSettingValue(SettingType.Portlet, settingKey, settingValue);

//redirect back to the portal
portletResponse.returnToPortal();

%>

After you have completed the JSP pages, deploy your custom project as described in Deploying a Custom Java IDK Project in Eclipse Stand-Alone (without WTP) or Deploying a Custom Java IDK Project in Eclipse with WTP.


  Back to Top      Previous Next