Sample Code for Portlet Config


In this example, I have  declared a initialization parameter called “admin”. In the portlet ,I am reading all  initialization parameter using Portlet Config Object 

Portlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<portlet-app xmlns="http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd" version="2.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd" id="com.jsr286tutorial.sixthexample.SixthExamplePortlet.c304d92233">
      <portlet>
            <portlet-name>PortletConfig</portlet-name>
            <display-name xml:lang="en">PortletConfig</display-name>
            <display-name>PortletConfig</display-name>
            <portlet-class>com.jsr286tutorial.portletconfig.PortletConfig</portlet-class>
            <init-param>
                  <name>admin</name>
                  <value>admin</value>
            </init-param>
            <init-param>
                  <name>wps.markup</name>
                  <value>html</value>
            </init-param>
            <expiration-cache>0</expiration-cache>
            <supports>
                  <mime-type>text/html</mime-type>
                  <portlet-mode>view</portlet-mode>
            </supports>
            <resource-bundle>com.jsr286tutorial.portletconfig.nl.portletconfigResource</resource-bundle>
            <portlet-info>
                  <title>PortletConfig</title>
                  <short-title>PortletConfig</short-title>
                  <keywords>PortletConfig</keywords>
            </portlet-info>
      </portlet>
      <default-namespace>http://PortletConfig/</default-namespace>
</portlet-app>

In Portlet

package com.jsr286tutorial.portletconfig;
import java.io.*;
import java.util.Enumeration;
import javax.portlet.*;
/**
 * This sample class is example of  PortletConfig
 */
public class PortletConfig extends GenericPortlet {
                public static final String JSP_FOLDER    = "/_SixthExample/jsp/";   
                public static final String VIEW_JSP      = "SixthExamplePortletView";        
                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());
                                Enumeration names=getPortletConfig().getInitParameterNames();
                                while(names.hasMoreElements())
                                {
                                                String parametername=(String)names.nextElement();
                                                String parametervalue=getPortletConfig().getInitParameter(parametername);
                                                System.out.println(parametername+ "="+parametervalue);
                                }
                                // Invoke the JSP to render
                                PortletRequestDispatcher rd = getPortletContext().getRequestDispatcher(getJspFilePath(request, VIEW_JSP));
                                rd.include(request,response);
                }
               

                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";
                }

}

Result:

Click here to download the sample code