Creating a portlet using JSPs

So here's a more complex example where we will use JSP pages to handle some interaction between the Portlet views.

This application will display the result of a simple Math addition, making use of PortletContext to navigate between the pages of the application.

As first step create a new Web application named for example formexample.

Next add a class to it named FormPortlet:

{codecitation class="brush: java; gutter: true;"}
package com.sample;

import java.io.IOException;
import java.io.PrintWriter;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.GenericPortlet;
import javax.portlet.PortletException;
import javax.portlet.ProcessAction;
import javax.portlet.RenderMode;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;

public class FormPortlet extends GenericPortlet {

    @RenderMode(name = "view")
    public void display(RenderRequest request, RenderResponse response) throws PortletException, IOException {

          if (request.getParameter("result") != null) {
              getPortletContext().getRequestDispatcher("/output.jsp").include(request, response);
          }
          else {
              getPortletContext().getRequestDispatcher("/form.jsp").include(request, response);
          }
    }

    @ProcessAction(name = "calculateAction")
    public void nameAction(ActionRequest request, ActionResponse response) throws PortletException {
        int number1=Integer.parseInt(request.getParameter("number1"));
        int number2=Integer.parseInt(request.getParameter("number2"));

        response.setRenderParameter("result", String.valueOf(number1+number2));
    }
}
{/codecitation}
As you can see, this Portlet uses the PortletContext to navigate between JSP pages.

 The concept of a PortletContext is similar to a ServletContext: it provides access to the environment in which the portlet is running. In the case of portlets, the environ-ment is the portlet application to which the portlet belongs, and the portlet container in which the portlet is running.

In our example, the PortletContext retrieves the PortletRequestDispatcher which defines the following methods to include content generated by a resource or forward request to a resource:


include: This method includes the content generated from the resource in the portlet response.
forward: This method forwards the request for generating content to another resource.

Let's add two JSP pages to our Project so it will be easier to understand the flow. This is form.jsp which contains a basic form, collecting two numbers as request parameter:
{codecitation class="brush: xml; gutter: true;"}
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>

<portlet:defineObjects/>

<div>
  <div class="portlet-section-header">Calculator Machine</div>
  <br/>
  <div class="portlet-section-body">
    <form action='<portlet:actionURL name="calculateAction"/>' method="post">
      <span class="portlet-form-label">Enter two numbers</span>
      <input class="portlet-form-input-field" type="text" name="number1"/>
      <input class="portlet-form-input-field" type="text" name="number2"/>

      <input class="portlet-form-button" type="Submit" value="Calculate Sum"/>
    </form>
  </div>
  <br/>
</div>
{/codecitation}
At first, notice the <defineObjects> tag is used to introduce scripting variables in the JSP page. Then you can use implicit Portlet objects such as the renderRequest object which is used to obtain the current portlet mode and window state information.
Next pay attention to tge actionURL tag which can be used in the form action attribute (or in in hyperlinks as well) to send an action request to a portlet when a user clicks on it. This will recall the FormPortlet's calculateAction.

Next jsp is output.jsp which will merely display the result which will be contained into the result request variable.
{codecitation class="brush: xml; gutter: true;"}
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>

<portlet:defineObjects/>

<div>
  <div class="portlet-section-header">Calculator Machine</div>
  <br/>
  <div class="portlet-section-body">Result: <%=renderRequest.getParameter("result") %></div>
  <br/>
  <a href="/<portlet:renderURL portletMode='view' />">Reset</a>
  <br/>
</div>

Nothing new here, just notice the Reset hyperlink below that is used to restart the application. The only missing piece is portlet.xml which follows here:

{codecitation class="brush: xml; gutter: true;"}
<?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>FormPortlet</portlet-name>
        <portlet-class>com.sample.FormPortlet</portlet-class>
        <supports>
            <mime-type>text/html</mime-type>
            <portlet-mode>view</portlet-mode>
        </supports>

        <portlet-info>
            <title>FormPortlet</title>
        </portlet-info>
    </portlet>
</portlet-app>
{/codecitation}
Now load the Portlet using in the Application Registry as shown in the earlier example, and add a new page containing this portlet.
Here's the expected output:

gatein example gatein tutorial gatein jboss

Once you click on the "Calculate Sum" button, the outout.jsp page will appear:

gatein example gatein tutorial gatein jboss

For the sake of brevity there are no validation controls on the numbers entered- we leave this as an exercise to the reader. Enjoy GateIn!

Download the formportlet example Eclipse Project.

0
0
0
s2smodern