I built 2 sample portlets to demonstrate how you can pass complex object from one portlet to another. The
EventSourcePortlet
passes object of com.webspherenotes.portlet.events.Contact
type to TargetSourcePortlet
. You can download the sample code for this project from here
I had to follow these steps
- I started by creating Contact Java project, this project has Contact.java class like this
public class Contact implements Serializable{
private static final long serialVersionUID = -1637774642655976822L;
private String firstName;
private String lastName;
private String email;
public Contact(){
}
public Contact(String firstName, String lastName, String email) {
super();
this.firstName = firstName;
this.lastName = lastName;
this.email = email;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
The Contact class is simple POJO class and it implementsSerializable
interface. That is because thesetEvent()
method takes object ofSerializable
type as argument for event value - Compile the Contact java source into contact.jar and copy it into shared library of your portal. In case of WebSphere portal it will be
WebSphere/PortalServer/shared/app
- Create
EventSourcePortlet
project and in that create EventSourcePortlet.java like this
public class EventSourcePortlet extends javax.portlet.GenericPortlet {
public void init() throws PortletException {
super.init();
}
protected void doView(RenderRequest request, RenderResponse response)
throws PortletException, IOException {
System.out.println("Entering SourcePortlet.doView()");
response.setContentType("text/html");
if (request.getPortletSession().getAttribute("contact") != null) {
request.setAttribute("contact", request.getPortletSession()
.getAttribute("contact"));
}
getPortletContext().getRequestDispatcher("/source.jsp").include(
request, response);
System.out.println("Exiting SourcePortlet.doView()");
}
public void processAction(ActionRequest request, ActionResponse response)
throws PortletException, IOException {
System.out.println("Entering SourcePortlet.processAction()");
String fName = request.getParameter("fName");
String lName = request.getParameter("lName");
String email = request.getParameter("email");
Contact contact = new Contact(fName, lName, email);
response.setEvent("hello", contact);
request.getPortletSession().setAttribute("contact", contact);
System.out.println("Exiting SourcePortlet.processAction()");
}
}
ThedoView()
method of theEventSourcePortlet
is forwarding control to contact.jsp for rendering. The contact.jsp displays a simple form to user where user can inputfirstName, lastName and email.
TheprocessAction()
method ofEventSourcePortlet
gets control when user enters values on the form and clicks submit. In this method, I am reading values submitted by user and then creating aContact
object and then setting and event with name equal tohello
and value equal to theContact
object. - Create EventTargetPortlet and inside that create EventTargetPortlet.java like this
public class EventTargetPortlet extends javax.portlet.GenericPortlet {
protected void doView(RenderRequest request, RenderResponse response)
throws PortletException, IOException {
System.out.println("Entering TargetPortlet.doView()");
response.setContentType("text/html");
Contact contact = (Contact) request.getPortletSession().getAttribute(
"contact");
if (contact != null) {
response.getWriter().println(
"First Name " + contact.getFirstName() + "
Last Name "
+ contact.getLastName() + "
Email "
+ contact.getEmail());
} else {
response.getWriter().println("Contact not found in session ");
}
System.out.println("Exiting TargetPortlet.doView()");
}
public void processEvent(EventRequest request, EventResponse response)
throws PortletException, IOException {
System.out.println("Entering TargetPortlet.processEvent");
Event event = request.getEvent();
System.out.println("Event Name " + event.getName());
Contact contact = (Contact) event.getValue();
System.out.println("Contact First Name " + contact.getFirstName());
System.out.println("Contact Last Name " + contact.getLastName());
System.out.println("Contact Email " + contact.getEmail());
request.getPortletSession().setAttribute("contact", contact);
System.out.println("Exiting TargetPortlet.processEvent");
}
}
TheprocessEvent()
method of theEventTargetPortlet
get control whenever it receives event, in this method it is reading theContact
object that was sent as event payload and then setting it as attribute in PortletSession.
In thedoView()
method it reads the Contact object fromPortletSession
and prints those values - This is how the portlet.xml file for the EventSourcePortlet looks like
<?xml version="1.0" encoding="UTF-8"?>
<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"
id="com.webspherenotes.portlet.event.EventSourcePortlet.1a884b6b82">
<portlet>
<portlet-name>EventSourcePortlet</portlet-name>
<display-name xml:lang="en">EventSourcePortlet</display-name>
<display-name>EventSourcePortlet</display-name>
<portlet-class>com.webspherenotes.portlet.event.EventSourcePortlet</portlet-class>
<init-param>
<name>wps.markup</name>
<value>html</value>
</init-param>
<expiration-cache>0</expiration-cache>
<supports>
<mime-type>text/html</mime-type>
<portlet-mode>view</portlet-mode>
</supports>
<supported-locale>en</supported-locale>
<resource-bundle>com.webspherenotes.portlet.event.nl.EventSourcePortletResource</resource-bundle>
<portlet-info>
<title>EventSourcePortlet</title>
<short-title>EventSourcePortlet</short-title>
<keywords>EventSourcePortlet</keywords>
</portlet-info>
<supported-publishing-event>
<name>hello</name>
</supported-publishing-event>
</portlet>
<default-namespace>http://wpcertification.blogspot.com</default-namespace>
<event-definition>
<name>hello</name>
<value-type>com.webspherenotes.portlet.events.Contact</value-type>
</event-definition>
</portlet-app>
As you can see thevalue-type
of the event iscom.webspherenotes.portlet.events.Contact
to indicate that we want to send object ofContact
class as event payload
1 comment:
Thanks for info....
SEO Company in Bangalore
Post a Comment