PortletMode

  • A portlet mode indicates the function a portlet is performing in the render method.
  • A portlet mode advises the portlet what task it should perform and what content it should generate. When invoking a portlet, the portlet container provides the current portlet mode to the portlet. Portlets can programmatically change their portlet mode when processing an action request.
  • The Portlet Specification defines three portlet modes, VIEW, EDIT, and HELP. The PortletMode class defines constants for these portlet modes.
  • The availability of the portlet modes, for a portlet, may be restricted to specific user roles by the portal. For example, anonymous users could be allowed to use the VIEW and HELP portlet modes but only authenticated users could use the EDIT portlet mode.
  • Portlets must support the VIEW portlet mode.
  • Portlets are not required to support the EDIT/HELP portlet mode
 Sample Code :

package com.jsrtutorial.fourthexample;
import java.io.*;
import javax.portlet.*;

public class FourthExample extends GenericPortlet {
    public static final String JSP_FOLDER    = "/_FourthExample/jsp/";    // JSP folder name
public static final String VIEW_JSP      = "FourthExampleView";         // JSP file name to be rendered on the view mode
    public static final String EDIT_JSP      = "FourthExampleEdit";         // JSP file name to be rendered on the edit mode
       public static final String HELP_JSP      = "FourthExampleHelp";         // JSP file name to be rendered on the help 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 doEdit(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, EDIT_JSP));
        rd.include(request,response);
    }
 
    protected void doHelp(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, HELP_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";
    }

}

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.jsrtutorial.fourthexample.FourthExample.2e07272233">
    <portlet>
        <portlet-name>FourthExample</portlet-name>
        <display-name xml:lang="en">FourthExample</display-name>
        <display-name>FourthExample</display-name>
        <portlet-class>com.jsrtutorial.fourthexample.FourthExample</portlet-class>
        <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>
            <portlet-mode>edit</portlet-mode>
            <portlet-mode>help</portlet-mode>

        </supports>
        <supported-locale>en</supported-locale>
        <resource-bundle>com.jsrtutorial.fourthexample.nl.FourthExampleResource</resource-bundle>
        <portlet-info>
            <title>FourthExample</title>
            <short-title>FourthExample</short-title>
            <keywords>FourthExample</keywords>
        </portlet-info>
    </portlet>
    <default-namespace>http://FourthExample/</default-namespace>
</portlet-app>

Note: To support edit/help mode,you need to declare these mode in the portlet.xml. By default,view mode is rendered when the portlet is called.

Click here to download the source code