Re: Linking Between Portlets
Khaled TLILI <[email protected]> Fri, 16 Nov 2007 12:39:25 +0100
| Newsgroups | gmane.comp.cms.jahia.devel |
|---|---|
| Message-ID | <[email protected]> |
Hi,
Here is an example of 2 portlet A and B. Portlet A displays links and
portlet B displays a result depending on clicked link.
1) First you have to specify the 2 portlets, A and B, in the
portlet.xml file.
Example of portlet.xml
-----------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<portlet-app xmlns="http://java.sun.com/xml/ns/portlet/portlet-app_1_0.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:portlet="http://java.sun.com/xml/ns/portlet"
xsi:schemaLocation="http://java.sun.com/xml/ns/portlet/portlet-app_1_0.xsd"
version="1.0">
<portlet>
<portlet-name>PortletA</portlet-name>
<portlet-class>jahia.portlet.PortletA</portlet-class>
<expiration-cache>0</expiration-cache>
<supports>
<mime-type>text/html</mime-type>
<portlet-mode>view</portlet-mode>
</supports>
<portlet-info>
<title>Portlet A</title>
</portlet-info>
</portlet>
<portlet>
<portlet-name>PortletB</portlet-name>
<portlet-class>jahia.portlet.PortletB</portlet-class>
<expiration-cache>0</expiration-cache>
<supports>
<mime-type>text/html</mime-type>
<portlet-mode>view</portlet-mode>
</supports>
<portlet-info>
<title>Portlet B</title>
</portlet-info>
</portlet>
</portlet-app>
2) Portlet A displays all links and deal with request like this
a) processAction(...){
- read value of parameter "action"
- store value of the parameter in the session with scope
APPLICATION_SCOPE in order to be accessible by all portlets and servlet
that belong to this application (In this case, PorletA and PortletB)
}
b) doView(...){
- display links with different values for parameter "action"
}
Example of code for PortletA
-----------------------------------------------
package jahia.portlet;
import javax.portlet.*;
import java.io.IOException;
import java.io.Writer;
public class PortletA extends GenericPortlet {
public static String ACTION = "action";
public void processAction(ActionRequest request, ActionResponse response) {
// get action
String action = request.getParameter(ACTION);
if (action != null) {
action = "1";
}
// important: set the parameter in Application scope in order.
request.getPortletSession().setAttribute(ACTION, "1",
PortletSession.APPLICATION_SCOPE);
}
public void doView(RenderRequest request, RenderResponse response)
throws IOException {
// first set content type
response.setContentType("text/html");
// create action urls
PortletURL invokeActionOne = response.createActionURL();
invokeActionOne.setParameter(ACTION, "1");
PortletURL invokeActionTwo = response.createActionURL();
invokeActionTwo.setParameter(ACTION, "2");
PortletURL invokeActionThree = response.createActionURL();
invokeActionThree.setParameter(ACTION, "3");
// generate link
Writer w = response.getWriter();
w.write("<a href='" + invokeActionOne.toString() + "'> action 1
</a>");
w.write("<br/>");
w.write("<a href='" + invokeActionTwo.toString() + "'> action 2
</a>");
w.write("<br/>");
w.write("<a href='" + invokeActionThree.toString() + "'> action
3 </a>");
w.write("<br/>");
}
}
3) PortletB read values of attribute "action" stored by portletA in the
session (APPLICATION_SCOPE) and executes the corresponding action or the
method.
Example of code for portlet B
----------------------------------------------
package jahia.portlet;
import javax.portlet.*;
import java.io.Writer;
import java.io.IOException;
public class PortletB extends GenericPortlet {
public static String ACTION = "action";
public void doView(RenderRequest request, RenderResponse response)
throws IOException {
// first set content type
response.setContentType("text/html");
// get Action
PortletSession session = request.getPortletSession();
// important: get attribute from application scope
String action = (String) session.getAttribute(ACTION,
PortletSession.APPLICATION_SCOPE);
if (action == null) {
response.getWriter().println("<b> there is no action </b>");
} else {
// depending on action value execute a specific method
if (action.equalsIgnoreCase("1")) {
response.getWriter().println("<b> Action 1 called </b>");
} else if (action.equalsIgnoreCase("2")) {
response.getWriter().println("<b> Action 2 called </b>");
} else if (action.equalsIgnoreCase("3")) {
response.getWriter().println("<b> Action 3 called </b>");
} else {
response.getWriter().println("<b> Action unknown </b>");
}
}
}
}
Order of method execution is the following:
1) processAction(.....) of portletA
2) render(....) (or doView()) of portletA and B. (see page 13 of portlet
documentation).
The main ideas are
1) to store shared data between porlets in the session with scope
"APPLICATION_SCOPE".
2) to portlets have to belongs to same application.
another solution is to user the lib. portlet_messaging:
http://www.doc.ic.ac.uk/~mo197/portlets/portlet_messaging/
I didn't test it but it looks interesting.
Regards,
KT.
[email protected] a écrit :
> Hi,
>
> We're developing a number of applications as Portlets within Jahia. Some of
> these applications provide different functionality for the same data (e.g.
> a Purchase order). We want to be able to link from one Portlet to another,
> providing parameters to load specific details in the linked-to portlet.
>
> e.g. one portlet shows a list of Purchase Orders and we want to link to the
> details of that Purchase Order in another Portlet.
>
> In some cases the Portlets may be on the same page, but more commonly we
> expect the Portlets to be on different pages. As we are developing locally
> and then deploying to a development server and then promoting to QA and
> Production, I'm not sure that the page ids will be consistent between the
> environments.
>
> Is there a syntax for URLs that can be used to link between portlets? Is it
> possible for the request to be an action request, and can we bypass the
> caching? Do you have any recommendation for how we can target portlets on
> different pages if the page ids may vary between environments?
>
> Thanks,
>
> Andrew May
> IT - Sr. Developer
> Abercrombie & Fitch
>
> _______________________________________________
> dev_list mailing list
> [email protected]
> http://lists.jahia.org/cgi-bin/mailman/listinfo/dev_list
>
>
_______________________________________________
dev_list mailing list
[email protected]
http://lists.jahia.org/cgi-bin/mailman/listinfo/dev_list