Portlet Modes Tutorial
Resin 3.0

Features
Installation
Configuration
Web Applications
JSP
Servlets and Filters
Portlets
Databases
Admin (JMX)
Amber
Security
XML and XSLT
XTP
Resources
Performance
Protocols
Third-party
Troubleshooting/FAQ

Generic
Tutorials

Basic

Hello World
Render Parameters
Action
Modes
JSP
Action
Basic
JSP

Find this tutorial in: /opt/resin/webapps/resin-doc/portlet/tutorial/basic-modes
Try the Tutorial

Portlets use modes to subdivide their functionality, performing different tasks and creating different content depending on the current mode.

This tutorial adds an "edit" mode to the hello world example. The "view" mode shows the current identity and color, and the "edit" mode provides the user a form for entering the identity and color.

  1. Files in this tutorial
  2. Rendering
  3. A url link to a different mode
  4. Switching modes in processAction()
  5. portlets.xml
  6. Custom Modes

Files in this tutorial

WEB-INF/classes/example/HelloWorldPortlet.java Portlet
WEB-INF/portlets.xml portlet configuration
WEB-INF/web.xml web-app configuration

Rendering

The portlet implements the rendering for each mode with a doMODE(RenderRequest request, RenderResponse response) method. The render method prepares objects common to the views, stores them as request attributes, and then calls the appropriate do method.

render()
See it in: WEB-INF/classes/example/HelloWorldPortlet.java
  public void render(RenderRequest request, RenderResponse response)
    throws PortletException, IOException
  {
    PortletMode mode = request.getPortletMode();

    // prepare objects in common with all modes and store them
    // as request attributes

    prepareObjects(request,response);

    if (mode.equals(PortletMode.EDIT)) {
      doEdit(request,response);
    }
    else {
      doView(request,response);
    }
  }

The prepareObjects() method prepares objects in common to the views and stores them as request attributes. This avoids duplication of code, and also anticipates a time when the view might be rendered using JSP. The JSP can easily access the prepared objects because they are stored as request attributes.

In this simple example, the prepared objects are Strings. Real benefits are seen when more complex objects are prepared, for example a render parameter might indicate a primary key that is used to retrieve information from a database; objects containing that data are prepared for display by the view.

prepareObjects()
See it in: WEB-INF/classes/example/HelloWorldPortlet.java
  protected void prepareObjects(RenderRequest request, RenderResponse response)
  {
    String identity = request.getParameter("identity");
    if (identity == null)
      identity = "World";

    String color = request.getParameter("color");
    if (color == null)
      color = "silver";

    request.setAttribute("identity",identity);
    request.setAttribute("color",color);
  }

A url link to a different mode

Just like a render parameter, the mode is maintained from request to request. A url link can cause the mode of the portlet to switch.

In this tutorial, the doView presents a url that switches to the "edit" mode.

prepareObjects()
See it in: WEB-INF/classes/example/HelloWorldPortlet.java

  PortletURL editUrl = response.createRenderURL();
  editUrl.setPortletMode(PortletMode.EDIT);
    
  out.println("<a href='" + editUrl + "'>Edit</a>");

Switching modes in processAction()

The action handles the form submit. In this case, only the "edit" mode supports actions.

The action mode switches to the "view" mode using response.setPortletMode(). Once the form is submitted the mode is switched and the users sees the information that was just entered.

processAction()
See it in: WEB-INF/classes/example/HelloWorldPortlet.java

  public void processAction(ActionRequest request, ActionResponse response) 
    throws PortletException, IOException
  {
    PortletMode mode = request.getPortletMode();

    if (mode.equals(PortletMode.EDIT)) { 

      // get the values submitted with the form

      String identity = request.getParameter("identity");
      String color = request.getParameter("color");

      // set the values of the render parameters

      response.setRenderParameter("identity",identity); 
      response.setRenderParameter("color",color); 

      // switch to View mode

      response.setPortletMode(PortletMode.VIEW);

    }
  }

If a call to setPortletMode() is not made, then the mode is maintained for the call to render(). In this example, if the setPortletMode(PortletMode.VIEW) call was omitted, then then render() that followed the processAction() would still have a mode of "edit".

portlets.xml

The configuration of the portlet in WEB-INF/portlets.xml specifies the modes that the portlet supports. In this case, the portlet uses the "view" and the "edit" modes.

Specifying modes in portlets.xml
See it in: WEB-INF/portlets.xml
<portlet-app ...
    ...

    <supports>
      <mime-type>text/html</mime-type>
      <portlet-mode>view</portlet-mode>
      <portlet-mode>edit</portlet-mode>
    </supports>

    ...

Custom Modes

PortletMode.VIEW, PortletMode.EDIT, PortletMode.HELP are constants for the three standard modes specified by the portlet specification. A portlet can also have any number of custom modes.

A custom PortletMode java object is created with a String argument to the constructor:

new PortletMode(String)
public class MyPortlet implements Portlet {

  final private static PortletMode SECURITY = new PortletMode("security");

A custom portlet mode also requires a corresponding entry in the configuration of the portlet in WEB-INF/portlets.xml.

custom modes in portlets.xml
<portlet-app xmlns="http://java.sun.com/xml/ns/portlet/portlet-app_1_0.xsd"
             version="1.0">

  <portlet>
    <portlet-name>myPortlet</portlet-name>
    <portlet-class>example.MyPortlet</portlet-class>
    <resource-bundle>example.MyPortlet</resource-bundle>
    <supports>
      <mime-type>text/html</mime-type>
      <portlet-mode>view</portlet-mode>
      <portlet-mode>edit</portlet-mode>
      <portlet-mode>security</portlet-mode>
    </supports>

  </portlet>

  <custom-portlet-mode>
    <portlet-mode>security</portlet-mode>
  </custom-portlet-mode>

</portlet-app>

Try the Tutorial


Action
Basic
JSP
Copyright © 1998-2003 Caucho Technology, Inc. All rights reserved.
Resin® is a registered trademark, and HardCoretm and Quercustm are trademarks of Caucho Technology, Inc.