JSR-286 introduced resource serving and the same method can perform AJAX requests as well.
It's a long time but I am just describing here with code snippets all you can do with resource method.
This example works with Liferay 7.
There are three ways you can use resource method from your portlet.
We will be looking at these with code snippets
Method in your portlet class
It's a long time but I am just describing here with code snippets all you can do with resource method.
This example works with Liferay 7.
There are three ways you can use resource method from your portlet.
We will be looking at these with code snippets
- You can download a file
- You can fetch piece of data
- You can return a jsp/ftl...
1) Download File
Create a hyperlink and create resource url(Same step needs to be done for all calls)
<portlet:resourceURL var="downloadURL" /> <a href="<%=downloadURL%>">Download Users</a>
Method in your portlet class
public void serveResource(ResourceRequest resourceRequest, ResourceResponse resourceResponse) throws IOException, PortletException { resourceResponse.setContentType("text/csv"); resourceResponse.addProperty(HttpHeaders.CONTENT_DISPOSITION,
"attachment;filename=sample.csv"); OutputStream out = resourceResponse.getPortletOutputStream(); String names = "Vipin , Bardia"; InputStream in = new ByteArrayInputStream(names.getBytes()); IOUtils.copy(in, out); out.flush(); }
2) Fetch dynamic data
Javascript call
jQuery(".htmlButton").click(function() { jQuery.ajax({ url:$("#resource_url").val(), success: function(data) { jQuery('#result').html(data); } }); });
Method in your portlet class
public void serveResource(ResourceRequest resourceRequest, ResourceResponse resourceResponse) throws IOException, PortletException { resourceResponse.getWriter().write("I am sending my name : Vipin Bardia"); }
3) Return the jsp in AJAX call
Javascript call
Same javascript call as we used in example 2 to fetch dynamic data.
Method in your portlet class
public void serveResource(ResourceRequest resourceRequest, ResourceResponse resourceResponse) throws IOException, PortletException { include("/MYAJAX.jsp", resourceRequest, resourceResponse); }
You are just done, Try & Enjoy the function.............:)
Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a Front end developer learn from Javascript Training in Chennai . or learn thru JavaScript Online Training India. Nowadays JavaScript has tons of job opportunities on various vertical industry.
ReplyDeleteGreat Article IoT Projects for Students
ReplyDeleteDeep Learning Projects for Final Year
JavaScript Training in Chennai
JavaScript Training in Chennai
The Angular Training covers a wide range of topics including Components, Angular Directives, Angular Services, Pipes, security fundamentals, Routing, and Angular programmability. The new Angular TRaining will lay the foundation you need to specialise in Single Page Application developer. Angular Training
ในการเล่น BETFLIX เกมสล็อตแตกง่าย ทั้งยังมี Feature Buy ทำให้ผู้เล่น เข้าใกล้เงินโบนัสได้อย่างง่ายดาย เป็นหนึ่งในเกม BETFLIX เว็บตรงไม่ผ่านเอเย่นต์แตกง่าย
ReplyDelete