27
Advanced Web Systems Lab 2 First Sample Portlet Adriano Venturini

Sample Portlet Lab

Embed Size (px)

DESCRIPTION

Portlet

Citation preview

Page 1: Sample Portlet Lab

Advanced Web Systems Lab 2

First Sample Portlet

Adriano Venturini

Page 2: Sample Portlet Lab

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

Page 3: Sample Portlet Lab

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

Page 4: Sample Portlet Lab

Create the first (empty) portlet

Open a shell and go to:

awslab13\dev\liferay-plugins-sdk\portlets

run settings.bat

Run :

create firstSamplePortlet firstSamplePortlet

Let’s do some changes:

Portlet.xml:

<portlet-class>com.sample.jsp.portlet.JSPPortlet</portlet-

class>

Page 5: Sample Portlet Lab

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

Page 6: Sample Portlet Lab

Main portlet file

\firstSamplePortlet-portlet\docroot\WEB-

INF\src\com\sample\jsp\portlet\JSPPortlet.java

Download it from the web site

Page 7: Sample Portlet Lab

Let’s 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()

Page 8: Sample Portlet Lab

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

Page 9: Sample Portlet Lab

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

Page 10: Sample Portlet Lab

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

Page 11: Sample Portlet Lab

Add the FirstSamplePortlet

Login as administrator

Add a page

Add your firstSamplePortlet

Page 12: Sample Portlet Lab

Connect with the debug to the portlet

Set breakpoint on the doView method

Page 13: Sample Portlet Lab

LECTURE 3

Page 14: Sample Portlet Lab

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

Page 15: Sample Portlet Lab

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

Page 16: Sample Portlet Lab

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

Page 17: Sample Portlet Lab

Creation of links

Page 18: Sample Portlet Lab

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>

Page 19: Sample Portlet Lab

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 ?

Page 20: Sample Portlet Lab

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);

}

}

Page 21: Sample Portlet Lab

Add the secondView.jsp

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

prefix="portlet" %>

<portlet:defineObjects />

<p>Second View</p>

Page 22: Sample Portlet Lab

Let’s 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);

}

}

Page 23: Sample Portlet Lab

Add in view.jsp

<br/>

<a

href="<%=((String)request.getAttribute("actionU

RL"))%>"> This is an Action URL!</a>

Page 24: Sample Portlet Lab

Now manage the action

public void processAction(

ActionRequest actionRequest,

ActionResponse actionResponse)

throws IOException, PortletException {

actionResponse.setRenderParameter("viewPage",

"secondView");

}

Page 25: Sample Portlet Lab

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

Page 26: Sample Portlet Lab

Exercise

Add a Render link which allows from the

SecondView to go back to the main view

Page 27: Sample Portlet Lab

Questions ?