Creating a portlet with the Portlet 2.0 Specification

This recipe will guide you through the process of creating a basic portlet that conforms to the Portlet 2.0 Specification. Once created, we will deploy the portlet into GateIn to see the result.

Getting ready

The following are required for this recipe:

  • Apache Maven
  • An IDE of your choice
  • GateIn-3.2.0.Final

How to do it...

To create a simple portlet based on the Portlet 2.0 Specification:

  1. Create a new Maven project within your IDE, specifying Group ID: gatein.cookbook, Artifact ID: chapter6, and Packaging: war.
  2. Inside the project's pom.xml, add the following dependency:
         <dependency>
            <groupId>javax.portlet</groupId>
            <artifactId>portlet-api</artifactId>
            <version>2.0</version>
            <scope>provided</scope>
         </dependency>
  3. Create a class named HelloWorldPortlet that extends javax.portlet.GenericPortlet within a package named gatein.cookbook.chapter6.
  4. Create a method named display within HelloWorldPortlet as follows:
        @RenderMode(name = "view")
        public void display(RenderRequest request, RenderResponse response)
        throws PortletException, IOException {
          getPortletContext().getRequestDispatcher("/helloWorld.jsp").include(request, response);
        }

    Note

    The @RenderMode annotation is used to inform the GenericPortlet which portlet-mode this method will render the content for. The display method will be used to render the view portlet mode, which is equivalent to overriding the doView method from GenericPortlet. Additional portlet modes include help and edit.

  5. Create a file named helloWorld.jsp in the src/main/webapp folder of the project.
  6. In helloWorld.jsp add a simple message, without any code, such as "Hello World from your new portlet!"
  7. Create portlet.xml in the src/main/webapp/WEB-INF folder of the project.
  8. Add the following to portlet.xml:
    <?xml version="1.0" encoding="UTF-8"?>
    <portlet-app 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"
        version="2.0">
      <portlet>
        <portlet-name>HelloWorld</portlet-name>
        <portlet-class>gatein.cookbook.chapter6.HelloWorldPortlet</portlet-class>
        <supports>
          <mime-type>text/html</mime-type>
          <portlet-mode>view</portlet-mode>
        </supports>
        <portlet-info>
          <title>Hello World portlet</title>
        </portlet-info>
      </portlet>
    </portlet-app>
  9. Create web.xml in the src/main/webapp/WEB-INF folder of the project.
  10. Add the following to web.xml:
    <?xml version="1.0"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_5.xsd"
            version="2.5">
    </web-app>
  11. Run the following in the root of the project directory to build the web archive:
    > mvn clean package
    
  12. Copy the generated web archive, chapter6-1.0.0.SNAPSHOT.war, from the target folder into the deployment folder where you unpacked the GateIn installation.
  13. Start the server and log in to the server as an administrator.
  14. Access the Application Registry, as seen in Chapter 2, Managing Portal Contents Using the GUI, and click on Import Applications to make our portlet available.
  15. Create a new page for the HelloWorld portlet as seen in Chapter 2, Managing Portal Contents Using the GUI.
  16. Navigate to the page you added the HelloWorld portlet to; you should now see a page similar to the following screenshot:
    How to do it...

How it works...

What we've done is create a very simple portlet that displays text, which is being run from within the portlet container. There are three main parts to a portlet that we covered earlier:

  • A portlet class for controlling integration with the portlet container. In our case this is HelloWorldPortlet, which is the central point for our portlet. It extends GenericPortlet so that we don't need to implement all the methods of the Portlet interface directly. For our portlet we only created the display() method to handle the rendering of the portlet, while leaving all the other integration with the portlet container to be completed by the GenericPortlet. Within the display() method we redirect to the Java Server Page we created, helloWorld.jsp, to generate the HTML content to be rendered by the portlet container.
  • A JSP that creates the HTML output we want rendered in the browser. Although helloWorld.jsp contained only text, we could add HTML or additional JSP content to generate the HTML output we needed.

    Note

    Note that markup generated for a portlet cannot contain <html>, <head>, <body> elements, or any markup that cannot be present within the <body> element, as it is the responsibility of the portlet container to generate these.

  • A portlet descriptor file, portlet.xml, describes to the portlet container which class controls the portlet and what features it makes available for use. We specified the portlet-class, what mime-type the portlet returns content in, what portlet-mode the portlet understands, and a title for the portlet. Specifying a portlet-mode of view informs the portlet container to only render our portlet content, and to not display help or editing options.

Note

All the various parameters that can be set within the portlet descriptor can be seen by checking the schema at http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd.

There's more...

In the portlet class we simply created a method annotated with @RenderMode(name = "view") to define how we would render the content for the portlet. There are several other ways in which the same outcome could have been achieved, which are described below.

Using a PrintWriter within the portlet class

Instead of delegating to a JSP to create the HTML content within display(), we could also have used the following code within display() to write HTML content directly onto the response stream:

PrintWriter out = response.getWriter();
out.println("Hello World from your new portlet!");

Overriding doView method from GenericPortlet

GenericPortlet provides default implementations for all the integration with the portlet container, saving development time, but it also provides convenient points to extend GenericPortlet functionality as needed.

Instead of creating display() in HelloWorldPortlet, we can replace display() with doView() from GenericPortlet and include a different implementation. Replacing display(), including the @RenderMode annotation, with the following will generate the same portlet content:

@Override
protected void doView(RenderRequest request, RenderResponse response) throws PortletException, IOException {
     getPortletContext().getRequestDispatcher("/helloWorld.jsp").include(request, response);
}

Implementing Portlet interface

Instead of extending from GenericPortlet, another option is to create a new class that implements the Portlet interface.

See also

  • The Using an action to pass form parameters recipe
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.139.83.199