Liferay Portal in Action 开发

来源:互联网 发布:淘宝如何激活蚂蚁花呗 编辑:程序博客网 时间:2024/05/17 12:24
liferay in action:
1. download the liferay zip from offcial site 6.0 with tomcat bundle:
  unzip the liferay6.0 to bundle folder like blow:
 D:\Development\liferay\development\6.0\bundles
 目录结构:
 --bundles
            |
            --data
            |
            --deploy
            |
            --tomcat-6.0.29
            |
            --licence
            |
            --portal-ext.properties(create by yourself for mysql database)         
    
2.配置mysql database
To point your Liferay bundle to your database, create a file called portal-ext.properties in your Liferay Home folder. 
  portal-ext.properties 内容为:
#
# MySQL
#
jdbc.default.driverClassName=com.mysql.jdbc.Driver
jdbc.default.url=jdbc:mysql://localhost/lportal?useUnicode=true&characterEncoding=UTF-8&useFastDateParsing=false
jdbc.default.username=root
jdbc.default.password=

3.sql command:
create database lportal character set utf8;
  mysql> create database lportal character set utf8;
  Query OK, 1 row affected (0.05 sec)

4.在启动tomcat之前要删除一个应用,以便是一个新的干净的应用。
Before you connect Liferay to your database, however, let's remove the sample web site so that you can start with a fresh, clean installation like the one. This is extremely easy to do: all you have to do is undeploy them.
删除目录 D:\Development\liferay\development\6.0\bundles\tomcat-6.0.29\webapps的文件夹sevencogs-hook

5.run the tomcat:
D:\Development\liferay\development\6.0\bundles\tomcat-6.0.2
9\bin>startup.bat
      shutdown.bat
自动创建数据库中的表等信息。并且tomcat启动起来。
登陆:http://localhost:8080

6.查看数据库,可以看到数据表已经建立好,
mysql> mysql -uroot -p
mysql> use lportal
Database changed
mysql> show tables;
Empty set (0.00 sec)

mysql> show tables;
+--------------------------------+
| Tables_in_lportal              |
+--------------------------------+
| account_                       |
| address                        |
| announcementsdelivery          |
| announcementsentry             |
| announcementsflag              |
| assetcategory                  |
| assetcategoryproperty          |
| assetentries_assetcategories   |
| assetentries_assettags         |
...

查看user_表:
mysql> desc user_;
+-----------------------+---------------+------+-----+---------+-------+
| Field                 | Type          | Null | Key | Default | Extra |
+-----------------------+---------------+------+-----+---------+-------+
| uuid_                 | varchar(75)   | YES  | MUL | NULL    |       |
| userId                | bigint(20)    | NO   | PRI |         |       |
| companyId             | bigint(20)    | YES  | MUL | NULL    |       |
| createDate            | datetime      | YES  |     | NULL    |       |
| modifiedDate          | datetime      | YES  |     | NULL    |       |
| defaultUser           | tinyint(4)    | YES  |     | NULL    |       |
| contactId             | bigint(20)    | YES  | UNI | NULL    |       |
| password_             | varchar(75)   | YES  |     | NULL    |       |
| passwordEncrypted     | tinyint(4)    | YES  |     | NULL    |       |
| passwordReset         | tinyint(4)    | YES  |     | NULL    |       |
| passwordModifiedDate  | datetime      | YES  |     | NULL    |       |
| digest                | varchar(255)  | YES  |     | NULL    |       |
| reminderQueryQuestion | varchar(75)   | YES  |     | NULL    |       |
| reminderQueryAnswer   | varchar(75)   | YES  |     | NULL    |       |
| graceLoginCount       | int(11)       | YES  |     | NULL    |       |
| screenName            | varchar(75)   | YES  |     | NULL    |       |
| emailAddress          | varchar(75)   | YES  | MUL | NULL    |       |
| facebookId            | bigint(20)    | YES  |     | NULL    |       |
| openId                | varchar(1024) | YES  |     | NULL    |       |
| portraitId            | bigint(20)    | YES  | MUL | NULL    |       |
| languageId            | varchar(75)   | YES  |     | NULL    |       |
| timeZoneId            | varchar(75)   | YES  |     | NULL    |       |
| greeting              | varchar(255)  | YES  |     | NULL    |       |
| comments              | longtext      | YES  |     | NULL    |       |
| firstName             | varchar(75)   | YES  |     | NULL    |       |
| middleName            | varchar(75)   | YES  |     | NULL    |       |
| lastName              | varchar(75)   | YES  |     | NULL    |       |
| jobTitle              | varchar(100)  | YES  |     | NULL    |       |
| loginDate             | datetime      | YES  |     | NULL    |       |
| loginIP               | varchar(75)   | YES  |     | NULL    |       |
| lastLoginDate         | datetime      | YES  |     | NULL    |       |
| lastLoginIP           | varchar(75)   | YES  |     | NULL    |       |
| lastFailedLoginDate   | datetime      | YES  |     | NULL    |       |
| failedLoginAttempts   | int(11)       | YES  |     | NULL    |       |
| lockout               | tinyint(4)    | YES  |     | NULL    |       |
| lockoutDate           | datetime      | YES  |     | NULL    |       |
| agreedToTermsOfUse    | tinyint(4)    | YES  |     | NULL    |       |
| active_               | tinyint(4)    | YES  |     | NULL    |       |
+-----------------------+---------------+------+-----+---------+-------+
38 rows in set (0.03 sec)

 

7.注册账号tianxin_star@163.com password:GW5c9Hmu

mysql> select firstName,lastName from user_;
+-----------+----------+
| firstName | lastName |
+-----------+----------+
|           |          |
| Test      | Test     |
| jack      | tian     |
+-----------+----------+

3 rows in set (0.00 sec)


8. plugins SDK install:

download the plugins SDK from www.liferay.com version: liferay-plugins-sdk-6.0.6-20110225

create folder at:D:\Development\liferay\development\6.0\bundles

name:plugins

upzip the sdk to plugins.

D:\Development\liferay\development\6.0\bundles

 目录结构:

--6.0

    |

    --bundles

    |

    --plugins

        |

        --build.u124460.properties

//u124460 is computer login name

add build.u124460.properties file in plugins folder.

add this content:app.server.dir=D:/Development/liferay/development/6.0/bundles/tomcat-6.0.29

reset the app.server.dir by customer.

 

9.create plugins.

   D:\Development\liferay\development\6.0\plugins>cd portlets

进入~/plugin/portlets folder create.bat hello-world "Hello World"

D:\Development\liferay\development\6.0\plugins\portlets>create.bat hello-world "
Hello World"
Buildfile: D:\Development\liferay\development\6.0\plugins\portlets\build.xml

create:
    [unzip] Expanding: D:\Development\liferay\development\6.0\plugins\portlets\p
ortlet.zip into D:\Development\liferay\development\6.0\plugins\portlets\hello-wo
rld-portlet
    [mkdir] Created dir: D:\Development\liferay\development\6.0\plugins\portlets
\hello-world-portlet\docroot\WEB-INF\tld
     [copy] Copying 6 files to D:\Development\liferay\development\6.0\plugins\po
rtlets\hello-world-portlet\docroot\WEB-INF\tld

BUILD SUCCESSFUL

Total time: 2 seconds

10.Deploying plugins

 go to the hello-world-porlet folder, run the command : ant deploy

D:\Development\liferay\development\6.0\plugins\portlets\hello-world-portlet>ant deploy
Buildfile: D:\Development\liferay\development\6.0\plugins\portlets\hello-world-p
ortlet\build.xml

if the tomcat server is running, there will log in tomcat console, When you see the “available for use” message, your plugin is deployed and can be used immediately. This applies for all plugin types, except for the Ext plugin.

 

使用IDE(Eclipse)工具开发

1.配置tomcat, new->server-> 配置tomcat server(路径,jre等等) Liferay Tomcat v6.0 Server

配置好后,在server tab中,找到Liferay Tomcat v6.0 Server, 双击打开,配置tomcat信息

 a.修改server locations

    1)use tomcat installtion

    2)deploy path change to D:\Development\liferay\development\6.0\bundles\tomcat-6.0.29\webapps

 b.open launch configuration, add args:

    -Dfile.encoding=UTF8 -Duser.timezone=GMT -Xmx1024m -XX:MaxPermSize=256m -Dexternal-properties=portal-developer.properties

 c.publishing

    select 'Never publish automatically'

 d.timeout both set to 3000+

2.Save the configuration. Now we can run the tomcat in eclispe.

3.import the customer portlet in plugins sdk to eclipse.

  new dynamic web project ->

    a. project name: jack-portlet

    b. change the default location to :D:\Development\liferay\development\6.0\plugins\portlets\jack-portlet-portlet

    c. click next, configure and edit the class folder:

        Eclipse will then guess at where your source code folder is and get the guess wrong. In the dialog box, select the source folder and click the Edit button. In the dialog box that appears, type docroot/WEB-INF/src and click OK. At the bottom of the dialog box, change the output folder to docroot/WEB-INF/classes. Click Next.

    d.Eclipse by default uses WebContent. So under Content Directory, type docroot. If the check box for Generate web.xml is checked, uncheck it.

    e.Next, select Add External Jars and then browse to your Liferay-Tomcat bundle folder. Browse to webapps/ROOT/WEB-INF/lib and then select the following .jars:
    commons-logging.jar
    util-java.jar
    util-taglib.jar
    util-bridges.jar

    f.run the project on tomcat sever which cofigure earlier.before every time run tomcat, you should change to ant view, do a ant deploy for customer portlet, then run the server, you can see the latest code in server.

 

let's make a portlet!!!

1. 在jack-portlet中, 用eclipse在docroot/WEB-INF/src目录下创建package:com.tien.liferay.action.portlet

   创建类:HelloWorldPortlet.action

2. ~/docroot/WEB-INF/portlet.xml, open the portlet.xml

   modify the portlet-class tag to :<portlet-class>com.tien.liferay.action.portlet.HelloWorldPortlet</portlet-class>

   modify the supports tag to:

        <supports>
            <mime-type>text/html</mime-type>
            <portlet-mode>view</portlet-mode>
            <portlet-mode>edit</portlet-mode>
        </supports>

3.添加edit init param

    <init-param>
        <name>edit-jsp</name>
        <value>/edit.jsp</value>
    </init-param>

4. class code:

package com.tien.liferay.action.portlet;
import java.io.IOException;
import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.GenericPortlet;
import javax.portlet.PortletException;
import javax.portlet.PortletMode;
import javax.portlet.PortletPreferences;
import javax.portlet.PortletRequestDispatcher;
import javax.portlet.PortletURL;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class HelloWorldPortlet extends GenericPortlet {

    private static Log _log = LogFactory.getLog(HelloWorldPortlet.class);

    protected String editJSP;
    protected String viewJSP;

    @Override
    public void init() throws PortletException {
        editJSP = getInitParameter("edit-jsp");
        viewJSP = getInitParameter("view-jsp");
    }

    @Override
    protected void doView(RenderRequest request, RenderResponse response)
            throws PortletException, IOException {
        PortletPreferences prefers = request.getPreferences();
        String userName = prefers.getValue("name", "no");
        if (userName.equalsIgnoreCase("no")) {
            userName = "";
        }
        request.setAttribute("userName", userName);
        include(viewJSP, request, response);
    }

    @Override
    public void doEdit(RenderRequest request,
            RenderResponse response)
            throws IOException, PortletException {
            response.setContentType("text/html");
            PortletURL addName = response.createActionURL();
            addName.setParameter("addName", "addName");
            request.setAttribute("addNameUrl", addName.toString());
        include(editJSP, request, response);

    }

    @Override
    public void processAction(ActionRequest request, ActionResponse response)
            throws PortletException, IOException {
        String addName = request.getParameter("addName");
        if (addName != null) {
            PortletPreferences prefs = request.getPreferences();
            prefs.setValue("name", request.getParameter("username"));
            prefs.store();
            response.setPortletMode(PortletMode.VIEW);
        }
    }
    protected void include(String path, RenderRequest request,
            RenderResponse response) throws IOException, PortletException {
        PortletRequestDispatcher portletRequestDispatcher = getPortletContext()
                .getRequestDispatcher(path);
        if (portletRequestDispatcher == null) {
            _log.error(path + " is not a valid include");
        } else {
            portletRequestDispatcher.include(request, response);
        }
    }
}

5.view.jsp

<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<jsp:useBean id="userName" class="java.lang.String" scope="request"/> 
<portlet:defineObjects /> 
<p>This is the Hello You portlet.</p>
<p>Hello, <%=userName %>!</p>

6.add edit.jsp and doedit() method in HelloWorldPortlet.java

<%@ taglib uri="http://java.sun.com/portlet" prefix="portlet"%>
<jsp:useBean class="java.lang.String" id="addNameUrl" scope="request" />
<portlet:defineObjects />
<form id="<portlet:namespace />helloForm" action="<%=addNameUrl%>"
    method="post">
    <table>
        <tr>
            <td>Name:</td>
            <td><input type="text" name="username"></td>
        </tr>
    </table>
    <input type="submit" id="nameButton" title="Add Name" value="Add Name">

</form>

7. Test

The default message of “Hello!” is being displayed in the portlet. This is the way it's supposed to function: we haven't set our Portlet Preference yet, so the portlet does not know our name. To get to Edit Mode, click the button in the title bar of the portlet that has a wrench on it, and then click the Preferences link (Liferay Portal displays a portlet's edit mode as a Preferences menu item). You will be brought to the portlet's edit mode, and your edit.jsp will be displayed.
Type your name and click the Add Name button. Your Portlet Preference will be stored, and because we changed the mode of the portlet back to View in our processAction method, the portlet will redisplay itself in view mode. Because our Portlet Preference is now set, the portlet will display the name we entered.

Congratulations! You have just written your first portlet!

8.Changing the portlet's category and name

Notice that when you added the portlet, you had to select it from the Sample category in the Add > More window. You also had to add the “Hello World” portlet, but now our portlet is called “Hello You.” The portlet is in a suboptimal category because the generated portlet project defaults to this category, and the portlet has the wrong name because we created “Hello World” in the last chapter. If you would like to create your own category for your portlet, this is easy to do. At the same time, we also want to rename the portlet so that it is no longer called Hello World, since it is now the Hello You portlet. We can do these things by editing two XML files.

change the portlet name and category path,

 change the portlet name in portlet.xml

        Find the two lines in the file that look like this:
            <portlet-name>hello-world</portlet-name>
            <display-name>Hello World</display-name>
        Change them so that they read like this:
            <portlet-name>hello-you</portlet-name>
            <display-name>Hello You</display-name>
        Next, find the the <portlet-info> section of the file:
            <portlet-info>
                <title>Hello World</title>
                <short-title>Hello World</short-title>
                <keywords>Hello World</keywords>
            </portlet-info>
        Change it so that it reads like this:
            <portlet-info>
                <title>Hello You</title>
                <short-title>Hello You</short-title>
                <keywords>Hello You</keywords>

            </portlet-info>

    Next, open liferay-portlet.xml, which is in the same folder. Find the following line, which is in the <portlet> tag:
            <portlet-name>hello-world</portlet-name>
    Change it so it reads:
            <portlet-name>hello-you</portlet-name>

    Finally, in the same WEB-INF folder, you will find a file called liferay-display.xml. This file controls the category under which your portlet appears in the Add > More window.

<?xml version="1.0"?>
    <!DOCTYPE display PUBLIC "-//Liferay//DTD Display 6.0.0//EN" "http://www.liferay.com/dtd/liferay-display_6_0_0.dtd">
    <display>
        <category name="category.sample">
            <portlet id="hello-you" />
        </category>
    </display>
Change the category name to something else, like My Portlets. The code would then read:
    <display>
        <category name="My Portlets">
            <portlet id="hello-you" />
        </category>
    </display>
Now go ahead and deploy your portlet again. Refresh the page. You'll see that an interesting thing has happened to your portlet, beofore deploy, you should remove previous add portlet in the page.


创建两个portlet项目,使用事件驱动,是两个portlet可以交互。

1.创建项目 create ipc-baseball "IPC Baseball",但是为了不要那么多项目,吧两个项目并成一个project。

We will have two portlets—a PitcherPortlet class and a CatcherPortlet class in one project. So we need to modify the project so that it is set up the way we want it.

edit the portlet.xml:

<?xml version="1.0"?>
<portlet-app version="2.0"
    xmlns="http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd"
    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">
    <!-- first portlet -->
    <portlet>
        <portlet-name>pitcher-portlet</portlet-name>
        <display-name>Pitcher Portlet</display-name>
        <portlet-class>com.tien.liferay.action.portlet.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>
    <!-- second portlet -->
    <portlet>
        <portlet-name>catcher-portlet</portlet-name>
        <display-name>Catcher Portlet</display-name>
        <portlet-class>
            com.tien.liferay.action.portlet.CatcherPortlet
        </portlet-class>
        <init-param>
            <name>view-jsp</name>
            <value>/catcher/view.jsp</value>
        </init-param>
        <expiration-cache>0</expiration-cache>
        <supports>
            <mime-type>text/html</mime-type>
        </supports>
        <portlet-info>
            <title>Catcher Portlet</title>
            <short-title>Catcher Portlet</short-title>
            <keywords>IPC Baseball Catcher</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-processing-event>
            <qname xmlns:x="http://liferay.com/events">x:ipc.pitch</qname>
        </supported-processing-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>

2.Configuring the event definition

 1)The Event definition is placed at the bottom of the file, outside of the two portlets. Our event will be a simple one: the PitcherPortlet will randomly generate a pitch (a number from 1 to 3), which will get translated into a String denoting what kind of pitch it is. This String object will be sent as the payload of our Pitch event. The PitcherPortlet will be configured to send the event, and the CatcherPortlet will be configured to receive the event.
First, we define the event itself, and then we define which portlet sends the event and which portlet receives it. Below the last portlet in the portlet.xml file, add the code in portlet.xml:

    a.Adding the event definition:

            <event-definition>
                <qname xmlns:x="http://liferay.com/events">x:ipc.pitch</qname>
                <value-type>java.lang.String</value-type>
            </event-definition>

        A QName is a qualified name. Using a QName allows events to be name-spaced properly so that no two event names are identical. The definition above declares an event called ipc.pitch within the name-space http://liferay.com/events. We've also declared that our payload is a String object.

    b.Identifying the Event Sender:

    The portlet standard defines a way of telling the portlet container (in this case, Liferay Portal) which portlet is responsible for sending an event. This is our PitcherPortlet. add below code in the PitcherPortlet definition in portlet.xml, below the last <security-role-ref> tag.

    <supported-publishing-event>
        <qname xmlns:x="http://liferay.com/events">x:ipc.pitch</qname>
    </supported-publishing-event>
This tells Liferay that the PitcherPortlet supports “publishing” the event we defined earlier.

    c.Identifying the Event Receiver:

    The portlet standard allows for one or many portlets to receive events. This might allow you to later create a BatterPortlet that also listens for the ipc.pitch event and determines whether the ball is hit. For now, we have only one event receiver: the CatcherPortlet. Add the code below the last <security-role-ref> tag in the CatcherPortlet definition.

    <supported-processing-event>
        <qname xmlns:x="http://liferay.com/events">x:ipc.pitch</qname>
    </supported-processing-event>
You have now defined everything you need to define in the portlet.xml file. Save and close the file; it's time to begin implementing our event.

3.Structuring the Pitcher Portlet and source code:

package com.tien.liferay.action.portlet;
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;
public class PitcherPortlet extends GenericPortlet {
    private static Log _log = LogFactory.getLog(PitcherPortlet.class);
    protected String viewJSP;
    public void init() throws PortletException {
        viewJSP = getInitParameter("view-jsp");
    }
    public void doView(RenderRequest req, RenderResponse res)
            throws IOException, PortletException {
        _log.info("---PitcherPortlet doView Method");
        include(viewJSP, req, res);
    }
    @ProcessAction(name = "pitchBall")
    public void pitchBall(ActionRequest request, ActionResponse response) {
        _log.info("---PitcherPortlet @ProcessAction[pitchBall] Method");
        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 void include(String path, RenderRequest req, RenderResponse res)
            throws IOException, PortletException {
        _log.info("---PitcherPortlet include Method");
        PortletRequestDispatcher prd = getPortletContext()
                .getRequestDispatcher(path);
        if (prd == null) {
            _log.error(path + " is not a valid include");
        } else {
            prd.include(req, res);
        }
    }
}

4.The PitcherPortlet's view.jsp lets the user pitch a ball, code below:

<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<portlet:defineObjects />
<p>Click the link below to pitch the ball. </p>
<a href="<portlet:actionURL name="pitchBall"></portlet:actionURL>">Pitch!</a>

As you can see, we did something a little different this time to create the ActionURL. We created the URL directly in the JSP using portlet tags, while in our Portlet 1.0 example we created the URL in the portlet class and sent it to the JSP as a bean in the request. Either way is fine; most developers find using the tags to be easier. I just wanted to be able to show you both ways of doing it so that you can decide which way works best for you. The Action URL we created has a name of pitchBall, which corresponds to our method name and annotation. When a user clicks on this link, our action method will be called.
Our pitcher is now implemented. We now need somebody to catch the ball.

 

5.Structuring the Catcher Portlet

package com.tien.liferay.action.portlet;
import java.io.IOException;
import javax.portlet.Event;
import javax.portlet.EventRequest;
import javax.portlet.EventResponse;
import javax.portlet.GenericPortlet;
import javax.portlet.PortletException;
import javax.portlet.PortletRequestDispatcher;
import javax.portlet.ProcessEvent;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class CatcherPortlet extends GenericPortlet {
    private static Log _log = LogFactory.getLog(PitcherPortlet.class);
    protected String viewJSP;
    public void init() throws PortletException {
        viewJSP = getInitParameter("view-jsp");
    }
    public void doView(RenderRequest req, RenderResponse res)
            throws IOException, PortletException {
        _log.info("---CatcherPortlet doView Method");
        include(viewJSP, req, res);
    }
    @ProcessEvent(qname = "{http://liferay.com/events}ipc.pitch")
    public void catchBall(EventRequest request, EventResponse response) {
        _log.info("---CatcherPortlet @ProcessEvent[catchBall] Method");
        Event event = request.getEvent();
        String pitch = (String) event.getValue();
        response.setRenderParameter("pitch", pitch);
    }
    protected void include(String path, RenderRequest req, RenderResponse res)
            throws IOException, PortletException {
        _log.info("---CatcherPortlet include Method");
        PortletRequestDispatcher prd = getPortletContext()
                .getRequestDispatcher(path);
        if (prd == null) {
            _log.error(path + " is not a valid include");
        } else {
            prd.include(req, res);
        }
    }
}

 

docroot/catcher/view.jsp:

<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<portlet:defineObjects />
<%
String pitch = request.getParameter("pitch");
//String pitch = (String)renderRequest.getParameter("pitch");
%>
<p>And the pitch is....</p>
<p>
<% if (pitch!=null) { %>
<%=pitch %>!
<% } else { %>
... waiting for pitch.
<% } %>
</p>

 

6.configuration the liferay-portlet.xml

<?xml version="1.0"?>
<!DOCTYPE liferay-portlet-app PUBLIC "-//Liferay//DTD Portlet Application 6.0.0//EN" "http://www.liferay.com/dtd/liferay-portlet-app_6_0_0.dtd">
<liferay-portlet-app>
    <portlet>
        <portlet-name>pitcher-portlet</portlet-name>
        <icon>/icon.png</icon>
        <instanceable>true</instanceable>
        <header-portlet-css>/css/main.css</header-portlet-css>
        <footer-portlet-javascript>/js/main.js</footer-portlet-javascript>
        <css-class-wrapper>ipc-baseball-portlet</css-class-wrapper>
    </portlet>
    <portlet>
        <portlet-name>catcher-portlet</portlet-name>
        <icon>/icon.png</icon>
        <instanceable>true</instanceable>
        <header-portlet-css>/css/test.css</header-portlet-css>
        <header-portlet-javascript>/js/test.js</header-portlet-javascript>
    </portlet>
    <role-mapper>
        <role-name>administrator</role-name>
        <role-link>Administrator</role-link>
    </role-mapper>
    <role-mapper>
        <role-name>guest</role-name>
        <role-link>Guest</role-link>
    </role-mapper>
    <role-mapper>
        <role-name>power-user</role-name>
        <role-link>Power User</role-link>
    </role-mapper>
    <role-mapper>
        <role-name>user</role-name>
        <role-link>User</role-link>
    </role-mapper>
</liferay-portlet-app>

// <portlet-name>pitcher-portlet</portlet-name> name should be the same as portlet.xml config. 


7.IPC Baseball's liferay-display.xml configuration file

<?xml version="1.0"?>

<!DOCTYPE display PUBLIC "-//Liferay//DTD Display 6.0.0//EN" "http://www.liferay.com/dtd/liferay-display_6_0_0.dtd">
    <display>
        <category name="IPC">
            <portlet id="pitcher-portlet" />
            <portlet id="catcher-portlet" />
        </category>
    </display>

           

8.IPC Baseball's liferay-plugin-package.properties configuration file 

name=IPC Baseball
module-group-id=liferay
module-incremental-version=1
tags=
short-description=
change-log=
page-url=http://www.liferay.com
author=Liferay, Inc.
licenses=LGPL

portal.dependency.jars=\
jstl.jar,\
jstl-impl.ja

9.log info:

03:06:36,682 INFO  [PitcherPortlet:35] ---PitcherPortlet @ProcessAction[pitchBall] Method
03:06:36,682 INFO  [PitcherPortlet:33] ---CatcherPortlet @ProcessEvent[catchBall] Method
03:06:36,964 INFO  [PitcherPortlet:27] ---CatcherPortlet doView Method
03:06:36,979 INFO  [PitcherPortlet:40] ---CatcherPortlet include Method
03:06:37,214 INFO  [PitcherPortlet:29] ---PitcherPortlet doView Method
03:06:37,229 INFO  [PitcherPortlet:60] ---PitcherPortlet include Method           


Great!!!



原创粉丝点击