Tribune

Home » Liferay Portal » English » 3. Development

Vista Combinata Vista Piatta Vista ad Albero
Discussioni [ Precedente | Successivo ]
Abid Ali
IPC- throug event.
5 febbraio 2013 22.15
Risposta

Abid Ali

Punteggio: Junior Member

Messaggi: 90

Data di Iscrizione: 30 marzo 2012

Messaggi recenti

Hello All,
I am trying to do one example on IPC using event. I developed two portlets 1. Pitcher 2. Catcher.
But my controller not goes to Catcher Portlet. I have created this two portles in between IPC-Portlet Project.
Give me solution for dat.

  1
  2View.jsp of pitcher Portlet
  3------------------------------------------
  4<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
  5
  6<portlet:defineObjects />
  7
  8<p>Click the link below to pitch the ball.</p>
  9<a href="<portlet:actionURL name="pitchBall" />">Pitch!
 10</a>
 11
 12PitcherPortlet.java
 13------------------------------
 14package com.ipc.PitcherPortlet;
 15
 16import com.liferay.portal.kernel.log.Log;
 17import com.liferay.portal.kernel.log.LogFactoryUtil;
 18import java.io.IOException;
 19import java.util.Random;
 20
 21import javax.portlet.ActionRequest;
 22import javax.portlet.ActionResponse;
 23import javax.portlet.GenericPortlet;
 24import javax.portlet.PortletException;
 25import javax.portlet.PortletRequestDispatcher;
 26import javax.portlet.ProcessAction;
 27import javax.portlet.RenderRequest;
 28import javax.portlet.RenderResponse;
 29import javax.xml.namespace.QName;
 30
 31/**
 32 * Portlet implementation class PitcherPortlet
 33 */
 34public class PitcherPortlet extends GenericPortlet {
 35
 36    public void init() {
 37        viewJSP = getInitParameter("view-template");
 38    }
 39   
 40    public void doView(
 41            RenderRequest renderRequest, RenderResponse renderResponse)
 42        throws IOException, PortletException {
 43       
 44        include(viewJSP, renderRequest, renderResponse);
 45    }
 46
 47    protected void include(
 48            String path, RenderRequest renderRequest,
 49            RenderResponse renderResponse)
 50        throws IOException, PortletException {
 51
 52        PortletRequestDispatcher portletRequestDispatcher =
 53            getPortletContext().getRequestDispatcher(path);
 54
 55        if (portletRequestDispatcher == null) {
 56            _log.error(path + " is not a valid include");
 57        }
 58        else {
 59            portletRequestDispatcher.include(renderRequest, renderResponse);
 60        }
 61    }
 62
 63   
 64    @ProcessAction(name="pitchBall")
 65    public void pitchBall(ActionRequest request, ActionResponse response) {
 66        System.out.println("i m in process action");
 67    String pitchType = null;
 68    Random random = new Random(System.currentTimeMillis());
 69    int pitch = random.nextInt(3) + 1;
 70    switch (pitch) {
 71    case 1:
 72    pitchType = "Fast Ball";
 73    break;
 74    case 2:
 75    pitchType = "Curve Ball";
 76    break;
 77    case 3:
 78    pitchType = "Slider";
 79    break;
 80    default:
 81    pitchType = "Screw Ball";
 82    }
 83    QName qName = new QName("http://liferay.com/events", "ipc.pitch");
 84    response.setEvent(qName, pitchType);
 85    System.out.println("in pitcher");
 86    }
 87 
 88    protected String viewJSP;
 89
 90    private static Log _log = LogFactoryUtil.getLog(PitcherPortlet.class);
 91}
 92
 93catcherportlet.java
 94------------------------------
 95package com.ipc.CatcherPortlet;
 96
 97import com.liferay.portal.kernel.log.Log;
 98import com.liferay.portal.kernel.log.LogFactoryUtil;
 99import java.io.IOException;
100
101import javax.portlet.Event;
102import javax.portlet.EventRequest;
103import javax.portlet.EventResponse;
104import javax.portlet.GenericPortlet;
105import javax.portlet.PortletException;
106import javax.portlet.PortletRequestDispatcher;
107import javax.portlet.ProcessEvent;
108import javax.portlet.RenderRequest;
109import javax.portlet.RenderResponse;
110
111/**
112 * Portlet implementation class CatcherPortlet
113 */
114public class CatcherPortlet extends GenericPortlet {
115
116    public void init() {
117         
118        viewJSP = getInitParameter("view-template");
119    }
120   
121    public void doView(
122            RenderRequest renderRequest, RenderResponse renderResponse)
123        throws IOException, PortletException {
124       
125        include(viewJSP, renderRequest, renderResponse);
126    }
127
128    protected void include(
129            String path, RenderRequest renderRequest,
130            RenderResponse renderResponse)
131        throws IOException, PortletException {
132
133        PortletRequestDispatcher portletRequestDispatcher =
134            getPortletContext().getRequestDispatcher(path);
135
136        if (portletRequestDispatcher == null) {
137            _log.error(path + " is not a valid include");
138        }
139        else {
140            portletRequestDispatcher.include(renderRequest, renderResponse);
141        }
142    }
143    @ProcessEvent(qname="{http://liferay.com/events}ipc.pitch")
144    public void catchBall(EventRequest request, EventResponse response) {
145    System.out.println("in catcher");
146    Event event = request.getEvent();
147    String pitch = (String)event.getValue();
148    response.setRenderParameter("pitch", pitch);
149   
150    }
151
152
153    protected String viewJSP;
154
155    private static Log _log = LogFactoryUtil.getLog(CatcherPortlet.class);
156
157}
158
159catcherportlet .jsp(view.jsp)
160-----------------------------------------
161<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
162
163<portlet:defineObjects />
164
165 <b>Catcher Portlet</b>
166
167 <%
168String pitch = (String) renderRequest.getParameter("pitch");
169%>
170<p>And the pitch is....</p>
171<p>
172<%
173if (pitch != null) {
174%>
175<%= pitch %>!
176<%
177}
178else {
179%>
180... waiting for pitch.
181<%
182}
183%>
184</p>
185
186portlet.xml
187-------------------
188<?xml version="1.0"?>
189
190<portlet-app 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" version="2.0">
191   
192    <portlet>
193        <portlet-name>pitcher-portlet</portlet-name>
194        <display-name>Pitcher Portlet</display-name>
195        <portlet-class>
196            com.ipc.PitcherPortlet.PitcherPortlet
197        </portlet-class>
198        <init-param>
199            <name>view-template</name>
200            <value>/html/pitcher/view.jsp</value>
201        </init-param>
202        <expiration-cache>0</expiration-cache>
203        <supports>
204            <mime-type>text/html</mime-type>
205            <portlet-mode>view</portlet-mode>
206        </supports>
207        <portlet-info>
208            <title>Pitcher Portlet</title>
209            <short-title>Pitcher Portlet</short-title>
210            <keywords>IPC Baseball Pitcher</keywords>
211        </portlet-info>
212        <security-role-ref>
213            <role-name>administrator</role-name>
214        </security-role-ref>
215        <security-role-ref>
216            <role-name>guest</role-name>
217        </security-role-ref>
218        <security-role-ref>
219            <role-name>power-user</role-name>
220        </security-role-ref>
221        <security-role-ref>
222            <role-name>user</role-name>
223        </security-role-ref>
224        <supported-publishing-event>
225<qname xmlns:x="http://liferay.com/events">x:ipc.pitch</qname>
226</supported-publishing-event>
227       
228    </portlet>
229    <portlet>
230        <portlet-name>catcher-portlet</portlet-name>
231        <display-name>Catcher Portlet</display-name>
232        <portlet-class>
233            com.ipc.CatcherPortlet.CatcherPortlet
234        </portlet-class>
235        <init-param>
236            <name>view-template</name>
237            <value>docroot/html/catcher/view.jsp</value>
238        </init-param>
239        <expiration-cache>0</expiration-cache>
240        <supports>
241            <mime-type>text/html</mime-type>
242            <portlet-mode>view</portlet-mode>
243        </supports>
244        <portlet-info>
245            <title>Catcher Portlet</title>
246            <short-title>Catcher Portlet</short-title>
247            <keywords>IPC Baseball Catcher</keywords>
248        </portlet-info>
249        <security-role-ref>
250            <role-name>administrator</role-name>
251        </security-role-ref>
252        <security-role-ref>
253            <role-name>guest</role-name>
254        </security-role-ref>
255        <security-role-ref>
256            <role-name>power-user</role-name>
257        </security-role-ref>
258        <security-role-ref>
259            <role-name>user</role-name>
260        </security-role-ref>
261        <supported-processing-event>
262         <qname xmlns:x="http://liferay.com/events">x:ipc.pitch</qname>
263        </supported-processing-event>
264       
265    </portlet>
266    <event-definition>
267        <qname xmlns:x="http://liferay.com/events">x:ipc.pitch</qname>
268        <value-type>java.lang.String</value-type>
269    </event-definition>
270   
271   
272</portlet-app>