Showing posts with label Lifeary serveResource. Show all posts
Showing posts with label Lifeary serveResource. Show all posts

Friday, 20 February 2015

How to use Liferay's serveResource method

Steps to use Liferay's serveResource method / Ajax call

Step 1: Create "resourceURL" in your JSP

Here is the snippet for the "View.jsp"

<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<portlet:resourceURL var="dynamiceResourceURL"></portlet:resourceURL>

<a href="dynamiceResourceURL">Dynamic Content</a>

Step 2: Create a "serveResource" method inside your MVCPortlet/Controller

Here is the snippet for <Your>Portlet

        @Override
public void serveResource(ResourceRequest resourceRequest,
ResourceResponse resourceResponse) throws IOException,
PortletException {
System.out.println("In serveResource method");
resourceResponse.setContentType("text/html");
PrintWriter writer = resourceResponse.getWriter();
writer.print("This is dynamic content.");
super.serveResource(resourceRequest, resourceResponse);
}

Step 3: Drop <your portlet> on portal page and click on the link "Dynamic Content". It will show result "This is dynamic content." which is returned by "serverResource" method of <your> controller.


Cheers!
Henal Saraiya
(Senior Consultant)
CIGNEX Datamatics