这个示例展示了一个个性化的 HelloWorld Portlet。它通过名称识别用户,并且允许用户进入编辑模式以设置一个新的名称。在 action方法(在 IBM Portlet API 中称为 actionPerformed ,而在 JSR 168中称为 processAction )中,Portlet持久化存储用户输入的新用户名。
清单 1. IBM Portlet API 示例 Portlet
首先看一看通过 IBM Portlet API 实现的 Portlet。
public class HelloWorld extends AbstractPortlet implements ActionListener
{
public void doView (PortletRequest request, PortletResponse response)
throws PortletException, IOException
{
//Get the user's name to display from persistent storage
PortletData portletData = request.getData();
String stringToDisplay=(String)portletData.getAttribute("userName");
if (stringToDisplay == null) {
stringToDisplay = defaultString; // set default string
}
// Add the display string to the portlet request to make it
// accessible by the view JSP
request.setAttribute("userName", stringToDisplay);
// Get a context for the current session for invoking the JSP
PortletContext context = getPortletConfig().getContext();
context.include(viewJSP, request, response);
}
public void doEdit(PortletRequest portletRequest,
PortletResponse portletResponse )
throws PortletException, IOException
{
// Create the cancel return URI for the edit page
PortletURI cancelURI = portletResponse.createReturnURI();
// Preserve the Cancel URI in the request to make it
// accessible by the edit JSP
portletRequest.setAttribute("cancelURI", cancelURI.toString());
// Create the save URI for the edit page
PortletURI saveURI = portletResponse.createReturnURI();
// For the "Save" button the return URI must include the "Save" action
// so the Action Listener for this portlet will be invoked
SimpleActionHelper.
addSimplePortletAction(getPortletConfig().getContext(), saveURI, "save");
// Preserve the Save URI in the request to make it accessible by
// the edit JSP
portletRequest.setAttribute("saveURI", saveURI.toString());
//Get the user's name to display from persistent storage
String stringToDisplay = (String)
portletRequest.getData().getAttribute("userName");
if (stringToDisplay == null) {
stringToDisplay = defaultString; // none found, set default string
}
// Add the display string to the request to make it accessible by the
// edit JSP as an inital value of the input field on the edit form
portletRequest.setAttribute("userName", stringToDisplay);
// Get a context for the current session for invoking the JSP
PortletContext context = getPortletConfig().getContext();
context.include(editJSP, portletRequest, portletResponse);
}
public void actionPerformed(ActionEvent event) {
String action =
SimpleActionHelper.getActionString(getPortletConfig().getContext(),
event);
HelloWorld helloPortlet = (HelloWorld)event.getPortlet();
PortletLog log = helloPortlet.getPortletLog();
// If this is a save action, then see if the user specified a name
if ( action!=null ) {
if ( action.equals("save") ) {
PortletRequest request = event.getRequest();
PortletData portData = request.getData();
String userName = request.getParameter("userName");
try {
// Save the name specified by the user
if ( userName != null ) {
portData.setAttribute("userName", userName);
portData.store();
}
} catch ( AccessDeniedException ade ) {
} catch ( IOException ioe ) {
log.error( " Couldn't write the user date to
persistence because an I/O Error occurred. " );
}
}
}
}
}
public class HelloWorld extends GenericPortlet
{
public void doView ( RenderRequest request, RenderResponse response)
throws PortletException, IOException
{
//Get the user's name to display from persistent storage
Portlet Preferences portletData = request.get Preferences(); String stringToDisplay = (String) portletData.get Value("userName", defaultString);
// Add the display string to the portlet request to make it
// accessible by the view JSP
request.setAttribute("userName", stringToDisplay);
// Get a context for the current session for invoking the JSP PortletRequestDispatcher rd = getPortletContext().getRequestDispatcher(viewJSP);
rd.include(request, response);
}
public void doEdit( RenderRequest portletRequest, RenderResponse portletResponse )
throws PortletException, IOException
{
// Create the cancel URI for the edit page
PortletUR L cancelURI = portletResponse.create ActionUR L();
// Preserve the Cancel URI in the request to make it
// accessible by the edit JSP
portletRequest.setAttribute("cancelURI", cancelURI.toString());
// Create the save URI for the edit page
PortletUR L saveURI = portletResponse.create ActionUR L();
// For the "Save" button the return URI must include the "Save" action
// so the Action Listener for this portlet will be invoked saveURI.setParameter("action", "save");
// Preserve the Save URI in the request to make it accessible by
// the edit JSP
portletRequest.setAttribute("saveURI", saveURI.toString());
//Get the user's name to display from persistent storage
String stringToDisplay = portletRequest.get Preferences().get Value("userName", defaultString);
// Add the display string to the request to make it accessible by the edit JSP
// as an inital value of the input field on the edit form
portletRequest.setAttribute("userName", stringToDisplay);
// Get a context for the current session for invoking the JSP PortletRequestDispatcher rd = getPortletContext().getRequestDispatcher(editJSP);
rd.include(portletRequest, portletResponse);
}
public void processAction(ActionRequest request, ActionResponse response)
throws PortletException, IOException
{
String action = request.getParameter("action");
// If this is a save action, then see if the user specified a name
if ( action!=null ) {
if ( action.equals("save") ) {
Portlet Preferences portData = request.get Preferences();
String userName = request.getParameter("userName");
// Save the name specified by the user
if ( userName != null ) {
portData.setAttribute("userName", userName);
portData.store();
}
}
IBM Portlet API 版本的 Portlet 的主要区别在于:
请求分配器使用 JSR 168 和 IBM Portlet API之间的请求分配器使用略有不同。JSR 168使用相同的机制以从上下文中取得一个请求分配器作为 servlet API 的参数。JSR168使用相同的机制从带有路径的上下文中获取请求分配器,以作为与Servlet API 一样的参数。