Create a sample portlet with Eclipse

advertisement
Hands-On Lab
Lab Manual
A quick Portlet tutorial
Page i
Contents
A quick Portlet tutorial ......................................................................................................................................... i
LAB 01: A QUICK PORTLET TUTORIAL ................................................................................................................. 3
Lab Objectives ........................................................................................................................................................ 3
Exercise 1 – Downloading and installing eclipse and plugins ................................................................................ 3
1.
Setup the eclipse and GlassFish ............................................................................................................... 3
2.
Configure the server for eclipse ................................................................................................................ 3
Exercise 2 – Create web application BodyCheck .................................................................................................. 5
1.
Create a Dynamic Web Application .......................................................................................................... 5
2.
Modify context path into the path of portlet container ............................................................................... 6
3.
Create portlet BodyCheck ......................................................................................................................... 7
4.
Input information for BodyCheck portlet .................................................................................................... 8
5.
The generated source codes are as below: .............................................................................................. 8
6.
Run the web application ............................................................................................................................ 8
7.
Open the portlet in the browser ................................................................................................................. 9
LAB 02: DEPLOY THE PORTLET INTO THE UPORTAL ........................................................................................ 9
Lab Objectives ........................................................................................................................................................ 9
Exercise 1 – Pack the portlet ........................................................................................................................... 10
1.
Export the .war file ................................................................................................................................... 10
2.
Deploy the BodyCheckPortlet.war into the uPortal ................................................................................. 10
Exercise 2 – Publish the portlet ....................................................................................................................... 10
1.
Login the uPortal with account admin/admin (). ...................................................................................... 10
2.
Select Portlet Manager ............................................................................................................................ 11
3.
In the Channel Manager, select Portlet. .................................................................................................. 11
4.
Input information for the project: ............................................................................................................. 12
5.
Input the “Portlet Descriptor” ................................................................................................................ 12
6.
Select categories ..................................................................................................................................... 12
7.
Select the groups and persons should have access to this portlet ......................................................... 13
8.
Click on Done link.................................................................................................................................... 13
9.
Try to use the portlet ............................................................................................................................... 14
LAB 03: APPLY THE JSTL FOR THE PORTLET .................................................................................................. 17
Lab Objectives ...................................................................................................................................................... 17
Exercise 1 – Modify the JSP for compatibility with portlet version 1.0............................................................. 17
1.
Modify taglib portlet for compatibility with portlet version 1.0. ................................................................. 17
Exercise 2 – Add library jstl.............................................................................................................................. 17
1.
Download and copy “jstl-1.1.2.jar”, “standard-1.1.2.jar” into BodyCheckPortlet\WebContent\WEBINF\lib. .............................................................................................................................................................. 17
2.
Update BodyCheckPortlet_view.jsp ........................................................................................................ 17
3.
Update BodyCheckPortlet.java ............................................................................................................... 18
APPENDIX ............................................................................................................................................................... 19
1.
Setup the Sun GlassFish Enterprise Server 2.1 ..................................................................................... 19
2.
Install Porlet Container for GlassFish...................................................................................................... 20
3.
Change the layout of home page of uPortal ........................................................................................... 20
4.
Setup the plugins for Eclipse ................................................................................................................... 20
Page ii
Lab 01: A quick portlet tutorial
Estimated time to complete this lab: 45 minutes
Lab Objectives
The objective of this lab is to introduce you how to create a simple portlet.

Downloading and installing eclipse and plugins.

Writing a simple portlet BodyCheck.
References:
- \\rai-server\Supports\Tools\jPackages\eclipse-jee-galileo-SR1-win32-rocky.zip
Exercise 1 – Downloading and installing eclipse and plugins
1. Setup the eclipse and GlassFish
This lab uses these softwares:
-
Eclipse IDE for Java EE Developers and plugins Eclipse Portal Pack 2.0.1.
GlassFish v2.1 Java EE 5 ()
Refer more details in Appendix.
2. Configure the server for eclipse
Open the Preferences dialog by going to menu Window > Preferences. Select node Server\Runtime
Environment.
Page 3
Click on “Add…” button.
In the “New Server Runtime Environment”, select node “Sun Microsystems\OpenPortal Porlet
Container 2.x”
Click on “Browse...” button to select GlassFish Home.
Click on Finish button.
The OpenPortal Porlet Container
Page 4
Exercise 2 – Create web application BodyCheck
1. Create a Dynamic Web Application
Going to menu File > New > Dynamic Web Project. Input the Project name. Click on Next
twice.
Select “Porlet 1.0 support” in Configuration
Page 5
2. Modify context path into the path of portlet container
Change the Context root to “portletdriver/dt”. This helps the eclipse open the home page of
the open portlet container when start the project.
Click on Finish button. The structure of the project is:
Page 6
3. Create portlet BodyCheck
Going menu New > Other…, select node Porlet\Java Porlet.
Click on Next button.
Page 7
4. Input information for BodyCheck portlet
5. The generated source codes are as below:
6. Run the web application
Right-click on the project, select Run As > Run on Server.
Select server “OpenPortal Portlet Container”.
Page 8
Then click on Finish button.
7. Open the portlet in the browser
Open the browser with the URL: http://localhost:8080/portletdriver/dt
Here is the content
BodyCheckPortlet
Lab 02: Deploy the portlet into the uPortal
Estimated time to complete this lab: 30 minutes
Lab Objectives
The objective of this lab is to introduce you how to deploy the portlet into uPortal

Pack the portlet into the .war file.
Page 9

Deploy the portlet into the uPortal manually.

Deploy the portlet into the uPortal by ant command.
Exercise 1 – Pack the portlet
1. Export the .war file
Right-click on the project, select Export > WAR file.
2. Deploy the BodyCheckPortlet.war into the uPortal
Using ant command
From the folder $uPortal_HOME, execute command
ant deployPortletApp -DportletApp=<Path of .war file>
In there, <Path of .war file> is the path of BodyCheckPortlet.war.
Ex:
ant deployPortletApp -DportletApp="D:/Projects/BodyCheckPortlet/dist/BodyCheckPortlet.war"
Note: Please read “README” file to know how modify “build.properties” of uPortal.
Exercise 2 – Publish the portlet
1. Login the uPortal with account admin/admin (1).
1
http://www.uportal.org
Page 10
2. Select Portlet Manager
3. In the Channel Manager, select Portlet.
Page 11
Click on Next button
4. Input information for the project:
Channel Title: BodyCheckPortlet
Dynamic Title: checked
Channel Name: BodyCheckPortlet
Channel Functional Name
Channel Description: BodyCheckPortlet
Channel Timeout: 5000
Channel Secure: no check
Click on Next button
5. Input the “Portlet Descriptor”
Framework Portlet: false
Portlet Web Application Path: /BodyCheckPortlet
Portlet Name: BodyCheckPortlet
Click on Next button four times.
6. Select categories
Check on the Development. Then click on “Select Marked”.
Page 12
Click on Next button.
7. Select the groups and persons should have access to this portlet
Check on “Close Group”. Then click on “Select Marked”.
Click on Next button. Then click on the Finished button.
8. Click on Done link.
Page 13
9. Try to use the portlet
Click on tab “Testing”. Then click on Add tab.
Click on Add Content
Page 14
Select Development > BodyCheck.
Click on Add to my page.
The portlet is displayed as below:
Page 15
Refer Appendix.3 to change layout of home page of uPortal.
Page 16
Lab 03: Apply the JSTL for the portlet
Estimated time to complete this lab: 30 minutes
Lab Objectives
The objective of this lab is to introduce you how to use upgrade the BodyCheckPortlet.
Exercise 1 – Modify the JSP for compatibility with portlet version 1.0
1. Modify taglib portlet for compatibility with portlet version 1.0.
For all JSP files, replace line
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet"%>
By
<%@ taglib uri="http://java.sun.com/portlet" prefix="portlet"%>
The content of file BodyCheckPortlet\WebContent\WEB-INF\jsp\BodyCheckPortlet_view.jsp after
updated:
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%-- Uncomment below lines to add portlet taglibs to jsp --%>
<%@ page import="javax.portlet.*"%>
<%@ taglib uri="http://java.sun.com/portlet" prefix="portlet"%>
<portlet:defineObjects />
<b>
Body Checking
</b>
Now, you should to re-deploy the portlet to view the changes.
Exercise 2 – Add library jstl
1. Download and copy “jstl-1.1.2.jar”, “standard-1.1.2.jar” into
BodyCheckPortlet\WebContent\WEB-INF\lib.
2. Update BodyCheckPortlet_view.jsp
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%-- Add portlet taglibs to jsp --%>
<%@ page import="javax.portlet.*"%>
<%@ taglib uri="http://java.sun.com/portlet" prefix="portlet"%>
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jstl/fmt" prefix="fmt"%>
<portlet:defineObjects />
<b>
Body Checking
<br/>
<form action='<portlet:actionURL/>' method="POST">
Height(m)(*):<input name="height" value="${height}"/>
Page 17
<br/>
Weight(kg)(*):<input name="weight" value="${weight}"/>
<br/>
<input type="submit" name="cmd" value="Check"/>
<br/>
<b>${status}</b>
</form>
3. Update BodyCheckPortlet.java
public class BodyCheckPortlet extends GenericPortlet {
final static Logger LOG = Logger.getLogger("BodyCheckPortlet");
private String status = "";
public void doView(RenderRequest request, RenderResponse response)
throws PortletException, IOException {
LOG.info("doView.START");
response.setContentType("text/html");
request.setAttribute("status", status);
PortletRequestDispatcher dispatcher =
getPortletContext().getRequestDispatcher(
"/WEB-INF/jsp/BodyCheckPortlet_view.jsp");
dispatcher.include(request, response);
LOG.info("doView.END");
}
public void doEdit(RenderRequest request, RenderResponse response)
throws PortletException, IOException {
response.setContentType("text/html");
PortletRequestDispatcher dispatcher =
getPortletContext().getRequestDispatcher(
"/WEB-INF/jsp/BodyCheckPortlet_edit.jsp");
dispatcher.include(request, response);
}
public void doHelp(RenderRequest request, RenderResponse response)
throws PortletException, IOException {
response.setContentType("text/html");
PortletRequestDispatcher dispatcher = getPortletContext().
getRequestDispatcher("/WEB-INF/jsp/BodyCheckPortlet_help.jsp");
dispatcher.include(request, response);
}
public void processAction(ActionRequest request, ActionResponse
response) throws PortletException, IOException {
LOG.info("processAction.START");
String height = request.getParameter("height");
String weight = request.getParameter("weight");
LOG.info("height=" + height + ";weight=" + weight);
status = "Default value: Normal";
LOG.info("processAction.END");
}
}
Page 18
Appendix
1. Setup the Sun GlassFish Enterprise Server 2.1
Download java-tools-bundle-update7-windows.exe (2) and run it. Select “Customize…” to install the
Sun GlassFish Enterprise Server v2.1.
Check on two items “Sun GlassFish Enterprise Server v2.1” and “Portlet Container 2.1” only.
Follow the screen guideline in next steps.
2
Download the lastest version at
http://java.sun.com/javaee/downloads/previous/index.jsp?userOsIndex=6&userOsId=windows&userOsName=
Windows. In the Intranet, it is already downloaded at
\\rai-server\Supports\Tools\Java\ApplicationServers\java-tools-bundle-update7-windows.exe
Page 19
Right-click
on the
space of
source
code
To avoid conflicting with Tomcat in case they run parallelly, you can change ports:
HTTP Port: 7070
HTTPS Port: 7171
Admin Port: 4747
2. Install Porlet Container for GlassFish
Copy files “setup.bat” and portlet-container-configurator.jar from “\\raiserver\Supports\Tools\Java\Portlet\” into your local PC. Run setup.bat. Input the home GlassFish
Server into “Select Install Directory”. Then click on OK.
3. Change the layout of home page of uPortal
Login the uPortal by account guest-lo/guest-lo to change layout. Then restart the uPortal to apply
changes.
4. Setup the plugins for Eclipse
In the intranet, the eclipse with installed plugins is at \\rai-server\Supports\Tools\jPackages\eclipsejee-galileo-SR1-win32-rocky.zip.
Page 20
Download