27
Advanced Web Systems Lab 2 First Sample Portlet Adriano Venturini

Advanced Web Systems Lab 2 First Sample Portletunibz.ectrldev.com/awslab13/2-SamplePortlet_lab.pdf · Configure the Netbeans project File, New project, java free form project Browse

  • Upload
    others

  • View
    6

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Advanced Web Systems Lab 2 First Sample Portletunibz.ectrldev.com/awslab13/2-SamplePortlet_lab.pdf · Configure the Netbeans project File, New project, java free form project Browse

Advanced Web Systems Lab 2

First Sample Portlet

Adriano Venturini

Page 2: Advanced Web Systems Lab 2 First Sample Portletunibz.ectrldev.com/awslab13/2-SamplePortlet_lab.pdf · Configure the Netbeans project File, New project, java free form project Browse

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: Advanced Web Systems Lab 2 First Sample Portletunibz.ectrldev.com/awslab13/2-SamplePortlet_lab.pdf · Configure the Netbeans project File, New project, java free form project Browse

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: Advanced Web Systems Lab 2 First Sample Portletunibz.ectrldev.com/awslab13/2-SamplePortlet_lab.pdf · Configure the Netbeans project File, New project, java free form project Browse

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: Advanced Web Systems Lab 2 First Sample Portletunibz.ectrldev.com/awslab13/2-SamplePortlet_lab.pdf · Configure the Netbeans project File, New project, java free form project Browse

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: Advanced Web Systems Lab 2 First Sample Portletunibz.ectrldev.com/awslab13/2-SamplePortlet_lab.pdf · Configure the Netbeans project File, New project, java free form project Browse

Main portlet file

\firstSamplePortlet-portlet\docroot\WEB-

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

Download it from the web site

Page 7: Advanced Web Systems Lab 2 First Sample Portletunibz.ectrldev.com/awslab13/2-SamplePortlet_lab.pdf · Configure the Netbeans project File, New project, java free form project Browse

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: Advanced Web Systems Lab 2 First Sample Portletunibz.ectrldev.com/awslab13/2-SamplePortlet_lab.pdf · Configure the Netbeans project File, New project, java free form project Browse

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: Advanced Web Systems Lab 2 First Sample Portletunibz.ectrldev.com/awslab13/2-SamplePortlet_lab.pdf · Configure the Netbeans project File, New project, java free form project Browse

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: Advanced Web Systems Lab 2 First Sample Portletunibz.ectrldev.com/awslab13/2-SamplePortlet_lab.pdf · Configure the Netbeans project File, New project, java free form project Browse

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: Advanced Web Systems Lab 2 First Sample Portletunibz.ectrldev.com/awslab13/2-SamplePortlet_lab.pdf · Configure the Netbeans project File, New project, java free form project Browse

Add the FirstSamplePortlet

Login as administrator

Add a page

Add your firstSamplePortlet

Page 12: Advanced Web Systems Lab 2 First Sample Portletunibz.ectrldev.com/awslab13/2-SamplePortlet_lab.pdf · Configure the Netbeans project File, New project, java free form project Browse

Connect with the debug to the portlet

Set breakpoint on the doView method

Page 13: Advanced Web Systems Lab 2 First Sample Portletunibz.ectrldev.com/awslab13/2-SamplePortlet_lab.pdf · Configure the Netbeans project File, New project, java free form project Browse

LECTURE 3

Page 14: Advanced Web Systems Lab 2 First Sample Portletunibz.ectrldev.com/awslab13/2-SamplePortlet_lab.pdf · Configure the Netbeans project File, New project, java free form project Browse

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: Advanced Web Systems Lab 2 First Sample Portletunibz.ectrldev.com/awslab13/2-SamplePortlet_lab.pdf · Configure the Netbeans project File, New project, java free form project Browse

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: Advanced Web Systems Lab 2 First Sample Portletunibz.ectrldev.com/awslab13/2-SamplePortlet_lab.pdf · Configure the Netbeans project File, New project, java free form project Browse

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: Advanced Web Systems Lab 2 First Sample Portletunibz.ectrldev.com/awslab13/2-SamplePortlet_lab.pdf · Configure the Netbeans project File, New project, java free form project Browse

Creation of links

Page 18: Advanced Web Systems Lab 2 First Sample Portletunibz.ectrldev.com/awslab13/2-SamplePortlet_lab.pdf · Configure the Netbeans project File, New project, java free form project Browse

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: Advanced Web Systems Lab 2 First Sample Portletunibz.ectrldev.com/awslab13/2-SamplePortlet_lab.pdf · Configure the Netbeans project File, New project, java free form project Browse

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: Advanced Web Systems Lab 2 First Sample Portletunibz.ectrldev.com/awslab13/2-SamplePortlet_lab.pdf · Configure the Netbeans project File, New project, java free form project Browse

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: Advanced Web Systems Lab 2 First Sample Portletunibz.ectrldev.com/awslab13/2-SamplePortlet_lab.pdf · Configure the Netbeans project File, New project, java free form project Browse

Add the secondView.jsp

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

prefix="portlet" %>

<portlet:defineObjects />

<p>Second View</p>

Page 22: Advanced Web Systems Lab 2 First Sample Portletunibz.ectrldev.com/awslab13/2-SamplePortlet_lab.pdf · Configure the Netbeans project File, New project, java free form project Browse

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: Advanced Web Systems Lab 2 First Sample Portletunibz.ectrldev.com/awslab13/2-SamplePortlet_lab.pdf · Configure the Netbeans project File, New project, java free form project Browse

Add in view.jsp

<br/>

<a

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

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

Page 24: Advanced Web Systems Lab 2 First Sample Portletunibz.ectrldev.com/awslab13/2-SamplePortlet_lab.pdf · Configure the Netbeans project File, New project, java free form project Browse

Now manage the action

public void processAction(

ActionRequest actionRequest,

ActionResponse actionResponse)

throws IOException, PortletException {

actionResponse.setRenderParameter("viewPage",

"secondView");

}

Page 25: Advanced Web Systems Lab 2 First Sample Portletunibz.ectrldev.com/awslab13/2-SamplePortlet_lab.pdf · Configure the Netbeans project File, New project, java free form project Browse

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: Advanced Web Systems Lab 2 First Sample Portletunibz.ectrldev.com/awslab13/2-SamplePortlet_lab.pdf · Configure the Netbeans project File, New project, java free form project Browse

Exercise

Add a Render link which allows from the

SecondView to go back to the main view

Page 27: Advanced Web Systems Lab 2 First Sample Portletunibz.ectrldev.com/awslab13/2-SamplePortlet_lab.pdf · Configure the Netbeans project File, New project, java free form project Browse

Questions ?