SAS 9.1.3 Integration Technologies » SAS Web Infrastructure Kit: Developer's Guide


Developing Custom Portlets
Development Steps
Creating a Deployment Descriptor
Creating Display Resources Files
Developing the Presentation JSP Page
Creating Action Classes
Implementing Portlet Help
Creating a PAR File
Use Cases
Simple Display Portlet
Localized Portlet
Portlet Template (Editable Portlet)
Remote Portlet
Tips and Best Practices
Using the Portlet API
Sample Portlets
Localized Display Portlet (Welcome)
Interactive Form Portlet (FormExample)
Portlet Template, or Editable Portlet (DisplayURL)
Web Application (HelloUserWikExample)
Remote Portlet (HelloUserRemote
Portlet
Sample: Interactive Form Portlet (FormExample)

Step 4: Create the Action Class

The FormExample portlet has its own action class, DisplayAction. The source code for DisplayAction follows. For more information, see Creating Action Classes.


/**Copyright (c) 2003 by SAS Institute Inc., Cary, NC 27513.
 * All Rights Reserved.
 */
package com.sas.portal.portlets.formexample;

import java.util.Enumeration;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;

import com.sas.portal.portlet.HTMLPortletAction;
import com.sas.portal.portlet.NavigationUtil;
import com.sas.portal.portlet.PortletContext;


/**Action for the Form Example Portlet. This prepares the URL
 * that will be assigned to the form's action within the portlet's 
 * JSP. It also populates a bean with the parameters from the JSP 
 * form.
 *
 * @author Todd.Folsom@sas.com
 * @version 1
 */
public final class DisplayAction extends HTMLPortletAction {

  /**
   * Prepare the URL for the form used in the portlet.
   * Returns the value of "FormExample.jsp".
   *
   * @param request  The HtppServletRequest associated with the 
   *                 method invocation
   * @param response HttpServletResponse associated with the 
   *                 method invocation
   * @param context  PortletContext mapped to the request path
   *
   * @return java.lang.String - representing a valid 
   *                 URL.
   */
  public String service(HttpServletRequest request,
       HttpServletResponse response, PortletContext context)
     throws Exception{

    super.service(request, response, context);

    //prepare the base URL for setting on the form in the JSP.
    // The "display" is the value used in portlet.xml for this 
    // action.	
    String baseURL = NavigationUtil.buildBaseURL(context, request,
        "display");
    context.setAttribute("formExample_baseURL", baseURL);

    //Make a new ExampleBean. Alternatively, this could be made 
    // once in the portlet initializer class, then you manage  
    // its properties in the action.
    ExampleBean bean = new ExampleBean();

    //The BeanUtils class will populate any bean with all the 
    // parameters from the form.
    HashMap map = new HashMap();
    Enumeration names = request.getParameterNames();
    while (names.hasMoreElements()) {
       String name = (String) names.nextElement();
       map.put(name, request.getParameterValues(name));
    }
    BeanUtils.populate(bean, map);

    //put the userInput into the portlet context so we can get it out
    // in the JSP.
    context.setAttribute("formExample_userInput", bean.getUserInput());

    return "FormExample.jsp";
  }
}