5.2.8. Public render parameters

In this example you write two portlets: one sets a value of a public parameter, and the other consumes the value.

  1. Create a new Maven project as follows:

  2. Edit pom.xml:

    
    <project>
      <modelVersion>4.0.0</modelVersion>
      <groupId>com.acme.samples</groupId>
      <artifactId>hello-portlet</artifactId>
      <version>1.0</version>
      <packaging>war</packaging>
      <build>
        <finalName>prp-portlet</finalName>
      </build>

      <dependencies>
        <dependency>
          <groupId>javax.portlet</groupId>
          <artifactId>portlet-api</artifactId>
          <version>2.0</version>
          <scope>provided</scope>
        </dependency>
      </dependencies>
    </project>
  3. Edit web.xml:

    
    <web-app>
      <display-name>prp-portlet</display-name>
    </web-app>
  4. Edit portlet.xml:

    
    <portlet-app version="2.0" xmlns="http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd"
      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">
      <portlet>
        <portlet-name>Sharing-PRP-Portlet</portlet-name>
        <portlet-class>com.acme.samples.SharingPRPPortlet</portlet-class>
        <supports>
          <mime-type>text/html</mime-type>
        </supports>
        <portlet-info>
          <title>Sharing-PRP-Portlet</title>
        </portlet-info>
        <supported-public-render-parameter>current_time</supported-public-render-parameter>
      </portlet>
      <portlet>
        <portlet-name>Consuming-PRP-Portlet</portlet-name>
        <portlet-class>com.acme.samples.ConsumingPRPPortlet</portlet-class>
        <supports>
          <mime-type>text/html</mime-type>
        </supports>
        <portlet-info>
          <title>Consuming-PRP-Portlet</title>
        </portlet-info>
        <supported-public-render-parameter>current_time</supported-public-render-parameter>
      </portlet>
      <public-render-parameter>
        <identifier>current_time</identifier>
        <name>current_time</name>
      </public-render-parameter>
    </portlet-app>
    • In case you pack the two portlets separately, the two portlet.xml files must repeat the same public-render-parameter and supported-public-render-parameter elements. In other words, there is no difference between the sharing portlet and the consuming one's configuration.

  5. Edit SharingPRPPortlet.java:

    package com.acme.samples;
    
    
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.util.Date;
    import javax.portlet.GenericPortlet;
    import javax.portlet.RenderRequest;
    import javax.portlet.RenderResponse;
    import javax.portlet.PortletException;
    import javax.portlet.ActionRequest;
    import javax.portlet.ActionResponse;
    import javax.portlet.PortletURL;
    public class SharingPRPPortlet extends GenericPortlet {
      @Override
      public void processAction(ActionRequest request, ActionResponse response) throws IOException, PortletException {
        response.setRenderParameter("current_time", new Date(System.currentTimeMillis()).toString());
      }
      @Override
      public void doView(RenderRequest request, RenderResponse response) throws IOException, PortletException {
        PortletURL actionURL = response.createActionURL();
        PrintWriter w = response.getWriter();
        w.write("<p>Click <a href=\"" + actionURL.toString() + "\">here</a> to execute processAction()</p>");
        w.write("<span>" + request.getParameter("current_time") + "</span>");
        w.close();
      }
    }
  6. Edit ConsumingPRPPortlet.java:

    package com.acme.samples;
    
    
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.util.Map;
    import javax.portlet.GenericPortlet;
    import javax.portlet.RenderRequest;
    import javax.portlet.RenderResponse;
    import javax.portlet.PortletException;
    import javax.portlet.ActionRequest;
    import javax.portlet.ActionResponse;
    public class ConsumingPRPPortlet extends GenericPortlet {
      @Override
      public void doView(RenderRequest request, RenderResponse response) throws IOException, PortletException {
        Map<String, String[]> paramNames = request.getPublicParameterMap();
        PrintWriter w = response.getWriter();
        for (String name : paramNames.keySet()) {
          String value = request.getParameter(name);
          w.write("<p>" + "*<b>" + name + "</b>: " + value + "</p>");
        }
        w.close();
      }
    }
    • In SharingPRPPortlet.java, the current_time parameter is set by the processAction() method, so the doView() method provides a link to trigger processAction().

    • While both the portlets prints current_time, the ConsumingPRPPortlet portlet gets and prints all the public parameters that it supports.

Add the two portlets to a page and test them:

Copyright ©. All rights reserved. eXo Platform SAS
blog comments powered byDisqus