Store user settings

 

+
Search Tips   |   Advanced Search

 


Overview

Portlet data is saved, retrieved, or deleted using the PortletData object. Portlets can store values in the PortletData object only when the portlet is in edit mode. If the portlet is on a group page, then information saved in PortletData is available to all users of the portlet. The portlet retrieves a reference to an instance of PortletData by calling the getData() method of the PortletRequest object. The store() method saves the information PortletData. Only data of the Java String type can be saved in the PortletData object.

PortletData is used by...

wp_root/dev/samples/HelloUser.war

...to edit and persist the greeting.

The sample builds on the HelloJSP sample by adding the doEdit() and the actionPerformed() methods, allowing users to edit the greeting and save their preferences.

The default greeting to display is obtained from the Web application deployment descriptor . The following example shows this descriptor with the initialization parameter defaultHelloString set to "Hello!".

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE web-app 
          PUBLIC 
          "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" 
          "http://java.sun.com/dtd/web-app_2_3.dtd">

<web-app id="WebApp">

 <display-name>HelloUser</display-name>

   <servlet id="com.ibm.wps.samples.HelloUser.9066feacf9c1001710b7b313e1a97f47">
      <servlet-name>HelloUser</servlet-name>

      <servlet-class>com.ibm.wps.samples.HelloUser</servlet-class>
          <init-param>
              <param-name>defaultHelloString</param-name>
              <param-value>Hello!</param-value>
          </init-param>               
   </servlet>

   <servlet-mapping id="unique_ID">
      <servlet-name>HelloUser</servlet-name>
      <url-pattern>/HelloUser/*</url-pattern>
   </servlet-mapping>

</web-app>

Configuration data that is set by the <init-param> tags are read-only and maintained for all users and every concrete portlet derived from the portlet. To allow different configurations for each concrete portlet, edit the <concrete-portlet> tag of portlet.xml.

The doView() method receives control prior to the standard display for this portlet. The PortletData object is accessed to obtain the string to display. If the user has not yet specified a string to display, the default string will be used. The string is stored in the PortletRequest object to make it available to the JSP that generates the view markup for this portlet (viewJSP).

The doEdit() method receives control prior to the display of the edit page for this portlet. A return URI is created and passed to the JSP for edit mode using the PortletRequest object. The save action is included in the return URI using the addAction() method. The portal passes control to the ActionListener upon processing the save action. The ActionListener can preserve the user entered "edit" information in the persistent storage. See Action events for more information about ActionListener and portlet actions.

Example: Saving and retrieving data from PortletConfig


package com.ibm.wps.samples;

import org.apache.jetspeed.portlet.*;
import org.apache.jetspeed.portlet.event.*;
import java.io.*;

public class HelloUser extends PortletAdapter implements ActionListener{

   private String defaultString;

   public void init(PortletConfig portletConfig) throws UnavailableException
   {
      super.init( portletConfig );
      // The default Hello String is obtained from the portlet configuration parameters
      defaultString = portletConfig.getInitParameter("defaultHelloString");
   } 

   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 this is the first time the user has accessed this portlet, then
      // no display string will be found for this user in persistent storage
      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);

      getPortletConfig().getContext().include("/jsp/View.jsp", request, response);
   }

   public void doEdit(PortletRequest request, PortletResponse response) 
                                     throws PortletException, IOException 
   {

      // Create the return URI for the cancel link of the edit page
      PortletURI cancelURI = response.createReturnURI();

      // Preserve the Cancel URI in the request to make it accessible by the edit JSP
      request.setAttribute("cancelURI", cancelURI.toString());

      // For the "Save" button the return URI must include the "Save" action
      // so the Action Listener for this portlet will be invoked
      PortletURI saveURI = response.createReturnURI();
      saveURI.addAction("save");
      // Preserve the Save URI in the request to make it accessible by the edit JSP
      request.setAttribute("saveURI", saveURI.toString());

      //Get the user's name to display from persistent storage
      String stringToDisplay = (String)request.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
      request.setAttribute("userName", stringToDisplay);

      getPortletConfig().getContext().include("/jsp/Edit.jsp", request, response);

   }

   public void actionPerformed(ActionEvent event) 
   {

      String action = event.getActionString();
      PortletLog log = 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( "<i><b>Couldn't write the user data to " );
              log.error( "persistence because an I/O Error occurred.</b></i>" );
            }
         }

      }
    
   } 

}

As mentioned previously, an ActionListener is implemented by HelloUser to process the save action. The user enters a name on the edit page and the actionPerformed() method of the ActionListener obtains the user-specified string from the PortletRequest object for storing in the user's persistent storage. The ActionListener is invoked prior to returning to the doView() method of the portlet, thus if the user failed to enter a name, the ActionListener can force the portlet to remain in edit mode, waiting for input from the user.

See also