Chapter 3. Liferay Portlet Frameworks

Table of Contents

1. Writing a Simple JSPPortlet
2. StrutsPortlet Tutorial
2.1. Writting a Very Simple Struts Portlet
2.2. Adding an action
2.3. Conclusion

In the next sections, you will learn how to develop a JSR 168 portlet leveraging two frameworks offered by Liferay to make deployment easier. First you will also learn how to create a simple JSPPortlet before moving on to the more complicated StrutsPortlet.

Both of these frameworks are available through the extension environment, so the rest of this section will assume that you already have it installed in a directory called ext. If you need more information about the extension environment, please read the Liferay Portal 4 - Development in the Extension Environment guide.

Note that by using these portlet frameworks your portlets will only work in Liferay Portal but not in other JSR-168 compliant portlets. Use them also if you need to speed your development and do not plan to deploy your portlets in other portal in the near term.

1. Writing a Simple JSPPortlet

Although a JSPPortlet does little more than display content, there is still some work that needs to be done. Let’s start by creating a new directory called myjspportlet within ext\ext-web\docroot\html\portlet\ext Next, open portlet-ext.xml within ext\ext-web\docroot\WEB-INF\.

[Note]Note

If you are using Eclipse, you may need to associate .xml files to Eclipse if your .xml files are being opened in a separate editor. You can do this by selecting Window from the menu bar and then Preferences. Expand the Workbench navigation, and click on File Associations. From there you can add *.xml as a new File type and associate it to open in Eclipse.

Notice how the portlets are uniquely identified by their portlet-name (also referred within Liferay Portal as the portlet id). As such, you will want to create a new portlet that is an increment of the portlet name, such as EXT_2. Since we are creating a JSPPortlet, you will want the portlet-class to reference the full class name: com.liferay.portlet.JSPPortlet. For this tutorial, add the following to your portlet-ext.xml (you may find it easier to copy and paste EXT_1 and just make the necessary changes):

<portlet>
  <portlet-name>EXT_2</portlet-name>
  <display-name>My JSPPortlet</display-name>
  <portlet-class>com.liferay.portlet.JSPPortlet</portlet-class>
  <init-param>
    <name>view-jsp</name>
    <value>/portlet/ext/myjspportlet/view.jsp</value>
  </init-param>
  <expiration-cache>300</expiration-cache>
  <supports>
    <mime-type>text/html</mime-type>
  </supports>
  <portlet-info>
    <title>My JSP Portlet</title>
  </portlet-info>
  <security-role-ref>
    <role-name>Power User</role-name>
  </security-role-ref>
  <security-role-ref>
    <role-name>User</role-name>
  </security-role-ref>
</portlet>

Here is a basic summary of what each of the elements represents:

portlet-nameThe portlet-name element contains the canonical name of the portlet. Each portlet name is unique within the portlet application.
display-nameThe display-name type contains a short name that is intended to be displayed by tools. It is used by display-name elements. The display name need not be unique.
portlet-classThe portlet-class element contains the fully qualified class name of the portlet.
init-paramThe init-param element contains a name/value pair as an initialization param of the portlet.
expiration-cacheExpiration-cache defines expiration-based caching for this portlet. The parameter indicates the time in seconds after which the portlet output expires. -1 indicates that the output never expires.
supportsThe supports element contains the supported mime-type. Supports also indicates the portlet modes a portlet supports for a specific content type. All portlets must support the view mode.
portlet-infoPortlet-info defines portlet information.
security-role-refThe security-role-ref element contains the declaration of a security role reference in the code of the web application. Specifically in Liferay, the role-name references which role’s can access the portlet. (A Power User can personalize the portal, whereas a User cannot.)

Now that you have configured your portlet-ext.xml, the next step is to create the jsp pages. Within your /myjspportlet directory, add a file called init.jsp. Within this file, add the following two lines of code:

<%@ include file="/html/common/init.jsp" %>
<portlet:defineObjects />

These two lines import all the common class files and also set common variables used by each portlet. If you need to import portlet specific classes or initialize portlet specific variables, be sure to add them to their directory specific init.jsp, as opposed to the common/init.jsp.

These two lines import all the common class files and also set common variables used by each portlet. If you need to import portlet specific classes or initialize portlet specific variables, be sure to add them to their directory specific init.jsp, as opposed to the common/init.jsp.

Now, add a view.jsp. This jsp file will hold the content of your JSPPortlet. Write “Hello [your name here]‿ within the jsp. So the question is then, how does the portal know how to load these particular files? If you look back at the portlet element that was added within portlet-ext.xml, you will notice that you initialized a view-jsp parameter as having the value /ext/myjspportlet/view.jsp. By specifying this init-param, you are giving the portlet a default jsp to load.

Finally, in order to be able to add your portlet to the portal, you need to define the name within Language-ext.properties by adding the following line:

javax.portlet.title.EXT_2=My JSP Portlet

Since you have setup the Extension Environment, you need to deploy the changes you have made to your application server by running ant deploy from the ext directory.

[Note]Note

Eclipse users may use the Ant view to run ant commands. In this case just double click the deploy [default].

Start Tomcat again as soon as the deployment finishes. Browse to the Home tab of the portal, and in the Add Portlet to Wide Column dropdown add “My JSP Portlet‿ to your portal.