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.
The following are required for this recipe:
To create a simple portlet based on the Portlet 2.0 Specification:
gatein.cookbook
, Artifact ID: chapter6
, and Packaging: war
.pom.xml
, add the following dependency:<dependency> <groupId>javax.portlet</groupId> <artifactId>portlet-api</artifactId> <version>2.0</version> <scope>provided</scope> </dependency>
HelloWorldPortlet
that extends javax.portlet.GenericPortlet
within a package named gatein.cookbook.chapter6
.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); }
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
.
helloWorld.jsp
in the src/main/webapp
folder of the project.helloWorld.jsp
add a simple message, without any code, such as "Hello World from your new portlet!"portlet.xml
in the src/main/webapp/WEB-INF
folder of the project.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>
web.xml
in the src/main/webapp/WEB-INF
folder of the project.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>
> mvn clean package
chapter6-1.0.0.SNAPSHOT.war
, from the target
folder into the deployment folder where you unpacked the GateIn installation.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:
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.helloWorld.jsp
contained only text, we could add HTML or additional JSP content to generate the HTML output we needed.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.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.
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.
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!");
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); }
3.139.83.199