Friday 16 March 2012

Working with portlet container in websphere application server 7.0-Part4

PortletPreferences are set by portlets to store customized information. By default, the PortletServingServlet servlet stores the portlet preferences for each portlet window in a cookie. 

The attributes of the cookie are defined as follows:
 
Path
context/portlet-name/portletwindow

Name:
The name of the cookie has the fixed value of PortletPreferenceCookie.
 
Value
The value of the cookie contains a list of preferences by mapping to the following structure:
*['/' pref-name *['=' pref-value]]
 
All preferences start with '/' followed by the name of the preference. If the preference has one or more values, the values follow the name separated by the '=' character. A null value is represented by the string '#*!0_NULL_0!*#'. 

In this example, the cookie value look like, 

Name: PortletPreferenceCookie

Path: /myportletpref/MyPortletPref/default

Value: /namepair=wpsadmin=wpsadmin

URL of the portlet is :

https://localhost:10002/myportletpref/MyPortletPref
 
Sample Code:

MyPortletPrefPortlet.java

package com.ibm.myportletpref;

import java.io.*;
import java.util.*;
import javax.portlet.*;

/**
 * A sample portlet based on GenericPortlet
 */
public class MyPortletPrefPortlet extends GenericPortlet {

    public static final String JSP_FOLDER    = "/_MyPortletPref/jsp/";    // JSP folder name

    public static final String VIEW_JSP      = "MyPortletPrefPortletView";         // JSP file name to be rendered on the view mode
   
    public void init() throws PortletException{
        super.init();
    }
    public void doView(RenderRequest request, RenderResponse response) throws PortletException, IOException {
        // Set the MIME type for the render response
        response.setContentType(request.getResponseContentType());
        // Invoke the JSP to render
        PortletRequestDispatcher rd = getPortletContext().getRequestDispatcher(getJspFilePath(request, VIEW_JSP));
        rd.include(request,response);
    }
   
    public void processAction(ActionRequest request, ActionResponse response) throws PortletException, java.io.IOException {
        PortletPreferences prefs=request.getPreferences();
        String[] namepair={request.getParameter("myname"),request.getParameter("password")};
        prefs.setValues("namepair", namepair);
        prefs.store();
   
    }
   
    private static String getJspFilePath(RenderRequest request, String jspFile) {
        String markup = request.getProperty("wps.markup");
        if( markup == null )
            markup = getMarkup(request.getResponseContentType());
        return JSP_FOLDER + markup + "/" + jspFile + "." + getJspExtension(markup);
    }

    private static String getMarkup(String contentType) {
        if( "text/vnd.wap.wml".equals(contentType) )
            return "wml";
        else
            return "html";
    }
   
    private static String getJspExtension(String markupName) {
        return "jsp";
    }

}

MyPortletPrefPortletView .jsp

<%@page session="false" contentType="text/html" pageEncoding="ISO-8859-1" import="java.util.*,javax.portlet.*,com.ibm.myportletpref.*" %>
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet"%>               
<portlet:defineObjects/>
Enter your Name<BR>
<form action='<portlet:actionURL/>'>
<input type="text" name="myname"/>
<input type="password" name="password"/>
<input type="submit" name="submit" value="submit"/>
</form>

ScreenShot

Click here to download the source code

No comments:

Post a Comment