Sei sulla pagina 1di 27

Advanced Web Systems

Lab 2
First Sample Portlet

Adriano Venturini
Install liferay plugin SDK
http://sourceforge.net/projects/lportal/files/Liferay%20Port
al/6.1.1%20GA2/liferay-plugins-sdk-6.1.1-ce-ga2-
20120731132656558.zip/download
Create a directory awslab13\dev
Unzip in dev:
The zip file just downloaded
(you should have now among the others, the dev/liferay-
plugins-sdk-6.1.1/portlets dir)
There you will find these directories:
Portlets
Theme
hooks


Configuring Plugin SDK

Create a new file in awslab13/dev/liferay-plugins-
sdk:
build.<yourloginname>.properties
Add the line referring to the installation of your
liferay tomcat server:
app.server.dir=<yourbasepath>/awslab13/server/liferay-portal-
6.1.1-ce-ga2/tomcat-7.0.27


Create the first (empty) portlet
Open a shell and go to:
awslab13\dev\liferay-plugins-sdk\portlets
run settings.bat
Run :
create firstSamplePortlet firstSamplePortlet
Lets do some changes:
Portlet.xml:
<portlet-class>com.sample.jsp.portlet.JSPPortlet</portlet-
class>




Configure the Netbeans project
File, New project, java free form project
Browse to: C:\WorkingDirectory\awslab13\dev\liferay-plugins-
sdk-6.1.1\portlets\firstSamplePortlet-portlet
Specify as source dir (create it if not yet present):
C:\WorkingDirectory\awslab13\dev\liferay-plugins-sdk-
6.1.1\portlets\firstSamplePortlet-portlet\docroot\WEB-
INF\src
Specify as Java Sources classes:
C:\WorkingDirectory\awslab13\server\liferay-portal-
6.1.1-ce-ga2\tomcat-7.0.27\lib\ext\portlet.jar
C:\WorkingDirectory\awslab13\server\liferay-portal-
6.1.1-ce-ga2/tomcat-7.0.27\lib\ext\portal-service.jar
C:\WorkingDirectory\awslab13\dev\liferay-plugins-sdk-
6.1.1\lib\log-4j.jar



Main portlet file
\firstSamplePortlet-portlet\docroot\WEB-
INF\src\com\sample\jsp\portlet\JSPPortlet.java

Download it from the web site
Lets examine the project files
Switch to the files tabs (in netbeans)
Docroot\WEB-INF:
Web.xml
Portlet.xml
Liferay-portlet.xml
Docroot\WEB-INF\src
JSPPortlet class which extends GenericPortlet
It implements the methods:
doView()
doHelp()
doEdit()
processAction()

Run the deploy target
Open a shell
Cd awslab13\dev\liferay-plugins-sdk\portlets\first-portlet
Ant deploy
The sample portlet is compiled and deployed to the portal
Note: you could get an error the first time. If you get the error, start
netbeans as administrator
Differently from tomcat:
The portlet is put in a hot deploy directory managed by liferay
The default hot deploy directory is:
awslab13\server\liferay-portal-6.1.1-ce-ga2\deploy
The portal monitors that directory, when a new module is there (a WAR
file), it is deployed under the webapps of tomcat (as usual)
Look at the log: You will see your portlet being deployed. Check always for
errors!
When liferay deploy a module, injects in the web.xml file of the module
servlets and taglib that are needed to liferay to control the module
Deploy from netbeans
In the file:
awslab13\dev\liferay-plugins-sdk-
6.1.1\build.properties change uncomment
javac.compiler=modern and comment
javac.compiler=org.(like this:
javac.compiler=modern
#javac.compiler=org.eclipse.jdt.core.JDTComp
ilerAdapter )

Open in the project tab the build.xml file of
firstSamplePortlet-portlet and run deploy
Comparison of original and liferay
injected web.xml
After deploy, opens:
awslab\server\liferay-portal-6.1.1-ce-
ga2\tomcat-7.0.27\webapps\firstSamplePortlet-
portlet\WEB-INF\web.xml
The original web.xml was empy
It injects some filters and servlets: which are the
component handling the communication with the
portal server


Add the FirstSamplePortlet
Login as administrator
Add a page
Add your firstSamplePortlet
Connect with the debug to the portlet
Set breakpoint on the doView method
LECTURE 3

Add a support Portlet Mode:help
In portlet.xml:
<supports>
<mime-type>text/html</mime-type>
<portlet-mode>view</portlet-mode>
<portlet-mode>help</portlet-mode>
</supports>
Deploy
You will see an additional menu item on the portlet caption: help
Look at the url, you will find the parameter:
p_p_mode=help
It tells to the portal that you want the help mode
The help mode is not yet managed so you get an empty page
Add JSP to manage the help mode
In portlet.xml:
<init-param>
<name>help-template</name>
<value>/help.jsp</value>
</init-param>
Create help.jsp file in docroot
Deploy again the portlet

Do the same for the edit mode
Add the edit portlet-mode support to portlet.xml
<portlet-mode>edit</portlet-mode>
Add the new parameter
<init-param>
<name>edit-template</name>
<value>/edit.jsp</value>
</init-param>
Create the edit.jsp
Creation of links

Add a new link (render url)
In JSPPortlet.java, doView method:
PortletURL rp=renderResponse.createRenderURL();
rp.setParameter("viewPage", "secondView");
renderRequest.setAttribute("pageURL",
rp.toString());

Then in view.jsp:
<a
href="<%=((String)request.getAttribute("pageURL"))
%>"> show another Page</a>
Deploy and check the URL
Of course the link is not yet managed.
If you click there nothing happens yet!
You should add a specific code that manage that
click.
Where ?
Change the doView method to manage
the new parameter
public void doView(
RenderRequest renderRequest, RenderResponse renderResponse)
throws IOException, PortletException {
String viewPage = renderRequest.getParameter("viewPage");
if (viewPage != null && viewPage.equals("secondView")) {
include(/+viewPage + ".jsp", renderRequest, renderResponse);
} else {
PortletURL ru = renderResponse.createRenderURL();
ru.setParameter("viewPage", "secondView");
renderRequest.setAttribute("pageURL", ru.toString());
include(viewJSP, renderRequest, renderResponse);
}
}
Add the secondView.jsp


<%@ taglib uri="http://java.sun.com/portlet_2_0"
prefix="portlet" %>

<portlet:defineObjects />

<p>Second View</p>
Lets try an actionURL!
public void doView(
RenderRequest renderRequest, RenderResponse renderResponse)
throws IOException, PortletException {
String viewPage = renderRequest.getParameter("viewPage");
if (viewPage != null && viewPage.equals("secondView")) {
include("/"+viewPage + ".jsp", renderRequest, renderResponse);
} else {
PortletURL ru = renderResponse.createRenderURL();
ru.setParameter("viewPage", "secondView");
renderRequest.setAttribute("pageURL", ru.toString());
PortletURL au = renderResponse.createActionURL();
au.setParameter("action", "doSomething");
renderRequest.setAttribute("actionURL", au.toString());
include(viewJSP, renderRequest, renderResponse);
}
}
Add in view.jsp
<br/>
<a
href="<%=((String)request.getAttribute("actionU
RL"))%>"> This is an Action URL!</a>

Now manage the action
public void processAction(
ActionRequest actionRequest,
ActionResponse actionResponse)
throws IOException, PortletException {

actionResponse.setRenderParameter("viewPage",
"secondView");
}
Deploy and debug
Set a breakpoint in the doView
Set a breakpoint in processAction
Click on This is an actionURL
Check the sequence of operations when you click
on show another page or this is an actionURL
Exercise
Add a Render link which allows from the
SecondView to go back to the main view
Questions ?

Potrebbero piacerti anche