I am newbie in liferay portal. I have developed one portlet in liferay for demo. I used inter portlet communication in this example. What i am doing is:- I have one search portlet in which i am having one textfield for search. When i click on search button it fetches the data from the database and display that data using search-contained in another portlet. I used ProcessEvent and ActionEvent annotation for this project.
Now what i want is when i click on the search button then the page should not be refresh(i.e i wish to use the concept of AJAX) and data should be displayed on the other portlet.
Code Snippet
Portlet A - view.jsp
<%@include file="/html/init.jsp"%>
<portlet:defineObjects />
<!--
<portlet:actionURL var="actionURL" name="pitchBall"></portlet:actionURL>
//-->
**Change to Resource URL**
<portlet:resourceURL var="resourceURL">
</portlet:resourceURL>
<aui:form method="POST" action="<%= resourceURL%>" name=" <portlet:namespace>fm1</portlet:namespace>">
<aui:input name="search" id="search" />
<aui:button type="submit" name="Search" value="Search" />
</aui:form>
Portlet A - SearchPortlet Class
package com.test;
/**
* Portlet implementation class SearchPortlet
*/
public class SearchPortlet extends GenericPortlet {
@Override
public void render(RenderRequest request, RenderResponse response)
throws PortletException, IOException {
// TODO Auto-generated method stub
super.render(request, response);
}
@ProcessAction(name="pitchBall")
public void pitchBall(ActionRequest request, ActionResponse response) throws SystemException {
String name = ParamUtil.getString(request, "search");
QName qName = new QName("http://liferay.com/events", "ipc.pitch");
response.setEvent(qName, name);
}
public void init() {
editJSP = getInitParameter("edit-jsp");
helpJSP = getInitParameter("help-jsp");
viewJSP = getInitParameter("view-jsp");
}
public void doEdit(
RenderRequest renderRequest, RenderResponse renderResponse)
throws IOException, PortletException {
include(editJSP, renderRequest, renderResponse);
}
public void doHelp(
RenderRequest renderRequest, RenderResponse renderResponse)
throws IOException, PortletException {
include(helpJSP, renderRequest, renderResponse);
}
@Override
public void doView(
RenderRequest renderRequest, RenderResponse renderResponse)
throws IOException, PortletException {
//super.doView(renderRequest, renderResponse);
System.out.println("In doView code");
renderResponse.setContentType(renderRequest.getResponseContentType());
// file to display...
String url = "/html/searchportlet/view.jsp";
// read the above file and output it...
getPortletContext().getRequestDispatcher(url).include(renderRequest, renderResponse);
//include(viewJSP, renderRequest, renderResponse);
}
@Override
public void serveResource(ResourceRequest request, ResourceResponse response)
throws PortletException, IOException {
// TODO Auto-generated method stub
//super.serveResource(request, response);
System.out.println("In serveResource code");
response.setContentType("text/html");
String name = request.getParameter("search");
// this seems to be the page that was calling...?
String resourceID = request.getResourceID();
System.out.println("resourceId was : " + resourceID);
System.out.println("message was : " + name);
PrintWriter writer = response.getWriter();
writer.print(name);
}
protected void include(
String path, RenderRequest renderRequest,
RenderResponse renderResponse)
throws IOException, PortletException {
PortletRequestDispatcher portletRequestDispatcher =
getPortletContext().getRequestDispatcher(path);
if (portletRequestDispatcher == null) {
_log.error(path + " is not a valid include");
}
else {
portletRequestDispatcher.include(renderRequest, renderResponse);
}
}
protected String editJSP;
protected String helpJSP;
protected String viewJSP;
private static Log _log = LogFactoryUtil.getLog(SearchPortlet.class);
}
Portlet B - view.jsp
<%@include file="/html/init.jsp"%>
<portlet:defineObjects />
<%
String name = (String)renderRequest.getParameter("name");
%>
<liferay-ui:search-container
emptyResultsMessage="author-empty-results-message">
<liferay-ui:search-container-results
results="<%= KeyurAuthorLocalServiceUtil.getStudentByName(name) %>" />
<liferay-ui:search-container-row className="com.test.model.KeyurAuthor">
<liferay-ui:search-container-column-text name="authorId"
property="authorId" />
<liferay-ui:search-container-column-text name="authorName"
property="authorName" />
<liferay-ui:search-container-column-text name="authorEmail"
property="authorEmail" />
</liferay-ui:search-container-row>
<liferay-ui:search-iterator></liferay-ui:search-iterator>
</liferay-ui:search-container>
Portlet B - SearchResultPortlet Class
/**
* Portlet implementation class SearchResultPortlet
*/
public class SearchResultPortlet extends GenericPortlet {
public void init() {
editJSP = getInitParameter("edit-jsp");
helpJSP = getInitParameter("help-jsp");
viewJSP = getInitParameter("view-jsp");
}
@ProcessEvent(qname="{http://liferay.com/events}ipc.pitch")
public void catchBall(EventRequest request, EventResponse response) {
Event event = request.getEvent();
String name = (String)event.getValue();
response.setRenderParameter("name", name);
}
public void doEdit(
RenderRequest renderRequest, RenderResponse renderResponse)
throws IOException, PortletException {
include(editJSP, renderRequest, renderResponse);
}
public void doHelp(
RenderRequest renderRequest, RenderResponse renderResponse)
throws IOException, PortletException {
include(helpJSP, renderRequest, renderResponse);
}
public void doView(
RenderRequest renderRequest, RenderResponse renderResponse)
throws IOException, PortletException {
include(viewJSP, renderRequest, renderResponse);
}
protected void include(
String path, RenderRequest renderRequest,
RenderResponse renderResponse)
throws IOException, PortletException {
PortletRequestDispatcher portletRequestDispatcher =
getPortletContext().getRequestDispatcher(path);
if (portletRequestDispatcher == null) {
_log.error(path + " is not a valid include");
}
else {
portletRequestDispatcher.include(renderRequest, renderResponse);
}
}
protected String editJSP;
protected String helpJSP;
protected String viewJSP;
private static Log _log = LogFactoryUtil.getLog(SearchResultPortlet.class);
}