本篇部分引用以下文章:
http://daoger.iteye.com/blog/359826
首先下载liferay-tomcat-bundle和liferay-plugin-SDK,将其解压直系统任意目录。
进入liferay-plugin-SDK目录,新建plugin项目:
ant -Dportlet.name=<project name> -Dportlet.display.name="<portlet title>" create
新建Eclipse Java项目LiferayPlugin,将liferay-plugin-SDK解压后的目录拷贝至里面。将Liferay需要的jar加入环境变量(主要包括tomcat/webapps/ROOT/WEB-INF/lib和tomcat/common/lib/ext两个文件夹里面的jar)
在LiferayPlugin目根目录下,新建资源文件build.{username}.properties,这里的username是你的计算机用户名称。然后在build.{username}.properties文件中添加app.server.dir这一个属性,它的值指向tomcat的目录。
build新建的portlet项目,此后运行Liferay,就可在plugin中看见发布的portlet。
以下引用ipc-baseball-portlet.rar 中的部分源码:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd"> <web-app> <listener> <listener-class>com.liferay.portal.kernel.spring.context.PortletContextLoaderListener</listener-class> </listener> <taglib> <taglib-uri>http://java.sun.com/jstl/core_rt</taglib-uri> <taglib-location>/WEB-INF/tld/c-rt.tld</taglib-location> </taglib> </web-app>
<?xml version="1.0"?> <portlet-app xmlns="http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd" version="2.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd"> <!-- <default-namespace>http://liferay.com/events</default-namespace> --> <portlet> <portlet-name>pitcher-portlet</portlet-name> <display-name>Pitcher Portlet</display-name> <portlet-class>com.liferay.ipc.PitcherPortlet</portlet-class> <init-param> <name>view-jsp</name> <value>/pitcher/view.jsp</value> </init-param> <expiration-cache>0</expiration-cache> <supports> <mime-type>text/html</mime-type> </supports> <portlet-info> <title>Pitcher Portlet</title> <short-title>Pitcher Portlet</short-title> <keywords>IPC Baseball Pitcher</keywords> </portlet-info> <security-role-ref> <role-name>administrator</role-name> </security-role-ref> <security-role-ref> <role-name>guest</role-name> </security-role-ref> <security-role-ref> <role-name>power-user</role-name> </security-role-ref> <security-role-ref> <role-name>user</role-name> </security-role-ref> <supported-publishing-event> <qname xmlns:x="http://liferay.com/events">x:ipc.pitch</qname> </supported-publishing-event> </portlet> <event-definition> <qname xmlns:x="http://liferay.com/events">x:ipc.pitch</qname> <value-type>java.lang.String</value-type> </event-definition> </portlet-app>
/**
* Copyright (c) 2000-2007 Liferay, Inc. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package com.liferay.ipc;
import java.io.IOException;
import java.util.Random;
import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.GenericPortlet;
import javax.portlet.PortletException;
import javax.portlet.PortletRequestDispatcher;
import javax.portlet.ProcessAction;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
import javax.xml.namespace.QName;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* This portlet demonstrates the new Event model in JSR-286
* that is used to achieve Inter-Portlet Communication. This portlet
* will "throw" an event out there for another portlet to catch.
*
* @author Rich Sezov
*
*/
public class PitcherPortlet extends GenericPortlet {
public void init() throws PortletException {
editJSP = getInitParameter("edit-jsp");
helpJSP = getInitParameter("help-jsp");
viewJSP = getInitParameter("view-jsp");
}
public void doDispatch(RenderRequest req, RenderResponse res) throws IOException, PortletException {
String jspPage = req.getParameter("jspPage");
if (jspPage != null) {
include(jspPage, req, res);
} else {
super.doDispatch(req, res);
}
}
public void doEdit(RenderRequest req, RenderResponse res) throws IOException, PortletException {
if (req.getPreferences() == null) {
super.doEdit(req, res);
} else {
include(editJSP, req, res);
}
}
public void doHelp(RenderRequest req, RenderResponse res) throws IOException, PortletException {
include(helpJSP, req, res);
}
public void doView(RenderRequest req, RenderResponse res) throws IOException, PortletException {
include(viewJSP, req, res);
}
protected void include(String path, RenderRequest req, RenderResponse res) throws IOException, PortletException {
PortletRequestDispatcher prd = getPortletContext().getRequestDispatcher(path);
if (prd == null) {
_log.error(path + " is not a valid include");
} else {
prd.include(req, res);
}
}
/**
* Since Portlet 2.0's GenericPortlet class lets you call methods
* from processAction via annotations, we don't even need to touch the
* processAction method to call our actions.
*
*/
@ProcessAction(name = "pitchBall")
public void pitchBall(ActionRequest request, ActionResponse response) {
String pitchType = null;
// Send an Event that the ball has been pitched.
Random random = new Random(System.currentTimeMillis());
int pitch = random.nextInt(3) + 1;
switch (pitch) {
case 1:
pitchType = "Fast Ball";
break;
case 2:
pitchType = "Curve Ball";
break;
case 3:
pitchType = "Slider";
break;
// should never print
default:
pitchType = "Screw Ball";
}
QName qName = new QName("http://liferay.com/events", "ipc.pitch");
response.setEvent(qName, pitchType);
}
protected String editJSP;
protected String helpJSP;
protected String viewJSP;
private static Log _log = LogFactory.getLog(PitcherPortlet.class);
}
<% /** * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ %> <%@ taglib uri="http://java.sun.com/portlet" prefix="portlet" %> <portlet:defineObjects /> <p>Click the link below to pitch the ball. </p> <a href="<portlet:actionURL name="pitchBall"></portlet:actionURL>">Pitch!</a>