Writing the My Greeting Portlet
This tutorial is based on Liferay Official Guide (http://www.liferay.com/documentation/liferay-portal/6.0/development/-/ai/writing-the-my-greeting-portlet). Some changes in texts and codes have been made to adapt them to Eclipse and Liferay 6.1
- Create a new portlet “my-greeting-1” that is exactly similar to “my-greeting”
- Open liferay-portlet.xml and ensure the value of the element instanceable is false
<instanceable>false</instanceable> - Open view.jsp
Replace the content with the following texts. (to avoid from character translation error, copy these texts to notepad program first, then copy them from notepad to the code editor)
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<%@ page import="javax.portlet.PortletPreferences" %>
<portlet:defineObjects />
<%
PortletPreferences prefs = renderRequest.getPreferences();
String greeting = (String)prefs.getValue(
"greeting", "Hello! Welcome to our portal.");
%>
<p><%= greeting %></p>
<portlet:renderURL var="editGreetingURL">
<portlet:param name="jspPage" value="/edit.jsp" />
</portlet:renderURL>
<p><a href="<%= editGreetingURL %>">Edit greeting</a></p>
- Next, create edit.jsp in the same directory as view.jsp.
Right-hand click docroot, Select New/File.

Type new file name “edit.jsp”
- Open edit.jsp
Copy the following texts. (to avoid from character translation error, copy these texts to notepad program first, then copy them from notepad to the code editor). If there is character error message, save with option utf-8.
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<%@ taglib uri="http://alloy.liferay.com/tld/aui" prefix="aui" %>
<%@ page import="javax.portlet.PortletPreferences" %>
<portlet:defineObjects />
<%
PortletPreferences prefs = renderRequest.getPreferences();
String greeting = renderRequest.getParameter("greeting");
if (greeting != null) {
prefs.setValue("greeting", greeting);
prefs.store();
%>
<p>Greeting saved successfully!</p>
<%
}
%>
<%
greeting = (String)prefs.getValue(
"greeting", "Hello! Welcome to our portal.");
%>
<portlet:renderURL var="editGreetingURL">
<portlet:param name="jspPage" value="/edit.jsp" />
</portlet:renderURL>
<aui:form action="<%= editGreetingURL %>" method="post">
<aui:input label="greeting" name="greeting" type="text" value="<%= greeting %>" />
<aui:button type="submit" />
</aui:form>
<portlet:renderURL var="viewGreetingURL">
<portlet:param name="jspPage" value="/view.jsp" />
</portlet:renderURL>
<p><a href="<%= viewGreetingURL %>">Back</a></p>
- Deploy.
Click “Edit greeting”

- Edit.

Type “Holla” and Click Save.

- Changes saved successfully.
Click Back.

New greeting text.

- Tip: If your portlet deployed successfully, but you don't see any changes in your browser after refreshing the page, Tomcat may have failed to rebuild your JSPs. Simply delete the work folder in liferay-portal-[version]/tomcat-[tomcat-version] and refresh the page again to force them to be rebuilt.
OBSERVATION
There are a few important details to notice in this implementation.
- First, the links between pages are created using the <portlet:renderURL> tag, which is defined by the http://java.sun.com/portlet_2_0 tag library. These URLs have only one parameter named jspPage, which is used by MVCPortlet to determine which JSP to render for each request. You must always use taglibs to generate URLs to your portlet. This restriction exists because the portlet does not own the whole page, only a fragment of it, so the URL must always go to the portal who is responsible for rendering, not only your portlet but also any others that the user might put in the page. The portal will be able to interpret the taglib and create a URL with enough information to be able to render the whole page.
- Second, notice that the form in edit.jsp has the prefix aui, signifying that it is part of the Alloy UI tag library. Alloy greatly simplifies the code required to create nice looking and accessible forms, by providing tags that will render both the label and the field at once. You can also use regular HTML or any other taglibs to create forms based on your own preferences.
- Another JSP tag that you may have noticed is <portlet:defineObjects/>. The portlet specification defined this tag in order to be able to insert into the JSP a set of implicit variables that are useful for portlet developers such as renderRequest, portletConfig, portletPreferences, etc.
- One word of warning about the portlet we have just built. For the purpose of making this example as simple and easy to follow as possible, we have cheated a little bit. The portlet specification does not allow to set preferences from a JSP, because they are executed in what is known as the render state. There are good reasons for this restriction, that are explained in the next tutorial.