Refreshing a single portlet in Liferay - caching

I have a problem with refreshing a single Liferay portlet. I wrote a bit of javascript code to refresh a single Liferay portlet every 120s:
function refreshPortlet() {
Liferay.Portlet.refresh("SOME_KEY")
};
setInterval("refreshPortlet()", 120000);
It works fine and the portlet refreshes, but the problem is that content of the portlet does not. I tried to clear the cache of the portlet with WebCachePoolUtil:
function refreshPortlet() {
WebCachePoolUtil.remove("SOME_KEY");
Liferay.Portlet.refresh("SOME_KEY")
};
setInterval("refreshPortlet()", 120000);
But now the function does not execute at all. Any hints?
Thanks in advance,
Stjepko

WebCachePoolUtil is a Java class, thus not available on the JS side. The reason the method is not executing is that it's running into the unavailability of that class.
If you want to execute browser-side code like you did, you'd need to identify where caching is done - e.g. on the browser or on the server. Observe your Developer Tool's network tab and inspect what's going across the wire.
Depending on the portlet that you're trying to refresh - specifically if it's a custom portlet - I'd recommend to implement Ajax explicitly and refresh either from a REST API or through the portlet's serveResource method.

Related

Liferay 6.2 ajax portlet content rendering

I've noticed that sometimes liferay loads the content of a portlet using ajax. For example, we've done some heavy duty web content templates and sometimes I can see a loading spinner while rendering the page.
I know about ajaxable and render-weight properties in liferay-portlet.xml, but... how does liferay know whether to render a portlet content using ajax or not? and second question, is it any way to disable this feature for asset publisher and web content display portlets without changing liferay's internal liferay-portlet.xml?
By default ajaxable is set to true.
In order to modify this you can try updating the render-weight and ajaxable attributes using the class PortletPreferences.
Read more here: https://www.liferay.com/community/forums/-/message_boards/message/11904281

AJAX POST does not always occur

I have tried to incorporate AJAX into an application that I built.
The basic function is:
-The user clickes a button, which triggers JS code that makes an AJAX call that does a POST to a Servlet(sending account data).
-The Servlet(which has an EJB injected into it), communicates with an EJB through it's local interface.
-The EJB(on init) initializes a DAO object, injects an EntityManager into it, and uses that DAO object to communicate with a database through JPA (Hibernate as the provider).
-The local interface methods of the EJB return Data Transfer Objects, which are parsed in the Servlets doPost() method, and the DTO's are used to build an HTML table(String) that the Servlet responds to the AJAX call with.
-On the client side, I use that HTML table(responseText) to update a div on the page.
I have 2 questions:
1) Is using a data-centric approach to the AJAX call (returning an HTML table instead of JSON Strings) a common choice in enterprise level applications that make use of AJAX?
2)I've noticed that sometimes the POST is not even called. It seems to be intermittent. I tried to add the Cache Control header, but that doesn't seem to work. This concerns me especially when I think about eventually deploying the application to production, that maybe AJAX is not the way to go, but when it works, the application works smoothly.
1) Using AJAX to submit data or update the web page is very common. Page at a time applications are the old way web applications used to be done where you would need to reload the entire page just to update a little bit of information - which would be inefficient and not to mention would create a bad user experience. These days, updating just "Part of a page" is very common and is mostly done using AJAX, and if not WebSockets.
Now you question regarding updating the page using the servers response which is HTML - to update the page, or just get a JSON String, and manipulate the DOM (ie adding tables etc). I have used a combination of these. For example if needed to add a row to a table, you could get the server to generate the HTML using a tempting engine (groovy or similar). In addition you you need a response code, so you can package the HTML and Response code in a JSON, and send it back to the client. Any combination of these works depending on your use case.
JsonObject json = new JsonObject();
json.addProperty("responseCode", responseCode);
json.addProperty("html", html);
2) You can write a simple script to send multiple requests to the AJAX url to see if it is the server that is not able to handle the amount of requests. If it works, then you can narrow down the problem to be client side. Make sure you are not using blocking techniques. You can also setup a callback function to see if there is any response if any. AJAX Post is similar to a normal POST request, just make sure you have some indicator for the user notifying them that the request is in progress/complete.

NodeJS MVC framework with partial ajax-loaded views

I'm looking for a NodeJS MVC framework that allows rendering partial views on client side using Ajax (without whole page refreshing).
As far as I know, you can declare partial views on server with almost any Framework (Express, Sails...), but this will lead to refresh the whole page even if there's only a small portion of your page that really changes. This problem doesn't exist with a SPA Framework (this one would just load the partial html file in a container via ajax).
I believe Microsoft ASP.NET MVC was able to handle this case, by comparing the previous version of the page with the new requested page, and just returning the portion of the page that really changed. This may be time and CPU-consuming but it just works.
Is any Node MVC Framework managing a similar thing today ? Or is it mandatory to use an SPA Framework when a reactive user interface is required (without any whole page refresh) ?
Thanks in advance !
sails.js! It supports partials as you requested. You don't need to refresh the page, if you send ajax-request or handle the stuff via websockets dynamically.

how can we develop ajax application with GWT UI-Binder

As per my project requirement i want to develop an Ajax application with GWT Ui-Binder.. for achieving page refreshing automatically while getting content from database..
Remember: withOut click Refresh button (or) any mouse motion.If u aware about this task...can you please give me with any related exmaple..
if you want to refresh automatic, so you may have any condition where you get event of getting data from database like rpc came on success method etc.. then simply call Window.Location.refresh(); there.

Using ajax inside the Web Content Display Portlet

I am using the liferay community edition 6.0. One of the pages will be using only the Web Content Display portlet to add content to it. I want to achieve the following inside that content i will load some html links that will be used to download some pdf. I want to add an ajax call to me domain before the actual redirect to the link has been done.
My question is the following. I've used the hook feature of liferay to add a java script ajax function in to the view.jsp. The click event is caught in the view.jsp but since there is no controller i don't know how to propagate this event to the domain.
So far i've done this function on my custom create portlet that are done using Spring MVC. But for this specific portlet i cant seem to find the controller or what ever handler the Web Content Display is using(if any).
Can someone please tell me if this is possible and point me in some direction if it is possible.
I am not sure what functionality you're trying to implement here, but I'll try to help. You can make an Ajax call to another jsp in your hook from your view.jsp.
jQuery.ajax({
type: "POST",
url: "/jsp/path/your.jsp",
data: "country="+country,
success: function() {
...
}
...
});

Resources