Foros de discusión

Issue with running portlet with ajax call after upgrading from 6.2 to 7

David Kirvan, modificado hace 6 años.

Issue with running portlet with ajax call after upgrading from 6.2 to 7

New Member Mensajes: 10 Fecha de incorporación: 4/06/15 Mensajes recientes
I have a portlet that executes a javascript function that then executes a ajax call to a jsp in order to create a selected Highcharts graph or pie chart. I have ran it through the upgrade from 6.2 to liferay 7. I am testing with Liferay v7.0 CE GA4. a javascript file executes an ajax call. The issue the portlet is having is the URL cannot be found when running under Liferay 7. The portlet has been running just fine under Liferay 6.2.

I am receiving a 404 url not found "/BroilerIndustryTrends-portlet/jsp/data.jsp".

$.ajax({
type : "POST",
url : "/BroilerIndustryTrends-portlet/jsp/data.jsp",
data : {
"height" : height,
"template" : template,
"renderTo" : id,
"className" : template
},
dataType : 'json',
success : function(options) {

var chart = new Highcharts.Chart(options);
.....................................................

when running under Liferay 7 I display the current url and I get:
http://localhost:8080/web/guest/home?p_p_id=metric_WAR_BroilerIndustryTrendsportlet_INSTANCE_Z0sOBzajF0Kg&p_p_lifecycle=0&p_p_state=normal&p_p_mode=view&p_p_col_id=column-1&p_p_col_count=2

Any ideas how I can set the correct url for the ajax call?
David Kirvan, modificado hace 6 años.

RE: Issue with running portlet with ajax call after upgrading from 6.2 to 7

New Member Mensajes: 10 Fecha de incorporación: 4/06/15 Mensajes recientes
Maybe I have made the question not clear enough. I am trying to find out how to obtain the URL for a JSP from a WAB. Does anyone have an idea?
thumbnail
Andrew Jardine, modificado hace 6 años.

RE: Issue with running portlet with ajax call after upgrading from 6.2 to 7

Liferay Legend Mensajes: 2416 Fecha de incorporación: 22/12/10 Mensajes recientes
Hi David,

I wonder if perhaps the issue is with the p_p_id. WIth the modules, the portlet ids in 7 no longer use the <portletname>_WAR_<servletcontext> format but rather the package/portlet class. So for example, where as you might have used --

piechart_WAR_chartsportlet


before, now you would use ...

com_xyz_chart_portlet_ChartPortlet


for the portlet id. Is your portlet serve resource method being invoked? I mean, can you set a breakpoint in the class and see it stop there?
David Kirvan, modificado hace 6 años.

RE: Issue with running portlet with ajax call after upgrading from 6.2 to 7

New Member Mensajes: 10 Fecha de incorporación: 4/06/15 Mensajes recientes
Andrew Jardine:
Hi David,
for the portlet id. Is your portlet serve resource method being invoked? I mean, can you set a breakpoint in the class and see it stop there?


Thank you for your response. The portlet was not using the serve resource method. It is looking like Liferay 7 no longer can process standard Ajax calls. I have to decide whether to rewrite the application to run in Liferay 7 or move it to a different server and run it in an iFrame on the portal. Neither of them are what I want to do but given the limitations of 7 I will have to make a choice.
thumbnail
Jack Bakker, modificado hace 6 años.

RE: Issue with running portlet with ajax call after upgrading from 6.2 to 7

Liferay Master Mensajes: 978 Fecha de incorporación: 3/01/10 Mensajes recientes
In v6.2 I construct such urls with taglibs like portlet:renderURL and pass in a portlet:param to reference the .jsp path ; perhaps using taglib is a safer moving forward - perhaps better than hardcoding the path to jsp.
thumbnail
Andrew Jardine, modificado hace 6 años.

RE: Issue with running portlet with ajax call after upgrading from 6.2 to 7

Liferay Legend Mensajes: 2416 Fecha de incorporación: 22/12/10 Mensajes recientes
BAH! I would say you should use the the AUI templating!

var TPL_TEMPLATE_NAME = new A.Template(
			'<div>',
			'</div>'
		);


-- using an AJAX call to retrieve the compiled results of a JSP?! Blasphemy JACK! emoticon
thumbnail
Jack Bakker, modificado hace 6 años.

RE: Issue with running portlet with ajax call after upgrading from 6.2 to 7

Liferay Master Mensajes: 978 Fecha de incorporación: 3/01/10 Mensajes recientes
Andrew Jardine:
BAH! I would say you should use the the AUI templating!

var TPL_TEMPLATE_NAME = new A.Template(
			'<div>',
			'</div>'
		);


-- using an AJAX call to retrieve the compiled results of a JSP?! Blasphemy JACK! emoticon

lol (I think)

how would that work for OP David who was referencing need to construct a url to a jsp ?
thumbnail
Andrew Jardine, modificado hace 6 años.

RE: Issue with running portlet with ajax call after upgrading from 6.2 to 7

Liferay Legend Mensajes: 2416 Fecha de incorporación: 22/12/10 Mensajes recientes
I suppose it all depends on your case, but in this case, based on the nothing I know about the JSP or what the tool is using, I would say this --

Rather than putting the markup in the JSP, and then retrieving it via resource url, put the markup in a TPL and then use the resource URL to get whatever data is required to populate the dynamic piece of the template. Once you have the data, you can pass the data to the render() function for the template and get the final markup. The final markup can then be injected into the existing page -- which is what I assume is happening with the JSP results that come back from the AJAX request. Personally I like this model better for a few reasons, the least of which being that I can always change the endpoint later if I need to and as long as the result returns the JSON that I am expecting to parse, my core application doesn't need to change.
thumbnail
Jack Bakker, modificado hace 6 años.

RE: Issue with running portlet with ajax call after upgrading from 6.2 to 7

Liferay Master Mensajes: 978 Fecha de incorporación: 3/01/10 Mensajes recientes
Yeah, to quote the mighty Olaf "it depends".

The simple dialog example at Meera's site shows along the lines that I was thinking in my earlier blasphemous emoticon comment.

http://www.liferaysavvy.com/2014/06/liferay-62-aui-dialog-liferay-popup.html

And to add to your (Andrew) approach, I've seen an interesting technique of delivering text back and parsing it as json only if it is indeed json where otherwise assume it is text (good for say an error message where, for example, the expected json couldn't be retreived)
thumbnail
Andrew Jardine, modificado hace 6 años.

RE: Issue with running portlet with ajax call after upgrading from 6.2 to 7

Liferay Legend Mensajes: 2416 Fecha de incorporación: 22/12/10 Mensajes recientes
The only time I would advocate in favour of referencing a JSP to populate the body would be for a case where it was not my plugin -- say a core plugin's JSP or something. But if I have control over the markup, then doing that kind of hybrid thing makes me feel like you're only half adopting one approach or another.

Myself, I try to use a standard response structure for my resource calls where in I have an outer structure and then a data attribute with the result of the response. For example --

{
   success: true,
   status: 200,
   message: "13 report records found"
   data: {
   }
}


Then I can use the success attribute to determine whether or not my call worked, the status code to determine handling logic, and the message (which I try to populate using a language key) to control the message I want to display to the user (if any). The data attribute then becomes my actual result which I either massage or pass as is (depending on the scenario) to the template.

Now, I am willing to admit a few things here emoticon. You certainly can't do as much with a TPL definition as you can in a JSP. You can loop to output a list, and you can do IF statements (there is, sadly, no concept of an else) and really, it's akin to velocity in a way where you basically have a map of key value pairs. But then purists will tell you that you shouldn't be processing logic in your template to begin with of course.

At the end of the day, there are several ways to skin this cat I guess. Using the <portlet:renderURL> as you referenced Jack would save the issue of the view path breaking on upgrade, but so would the TPL. I guess, my position is that if you are going to use AUI to create a rich UI, then I try to use as much of it as I can to create a clear separation between the two layers.

Mind you, I am also mental enough to enjoy building custom AUI modules -- so take any advice I give with a grain of salt!
David Kirvan, modificado hace 6 años.

RE: Issue with running portlet with ajax call after upgrading from 6.2 to 7

New Member Mensajes: 10 Fecha de incorporación: 4/06/15 Mensajes recientes
Thank your for your input. The app is a third party app that was not designed to fit within Liferay but folks here really wanted to run it as a portlet. I would have just ran it in an iFrame. I will consider all of your input on trying to determine the best approach to take in order to make it work in Liferay 7.
thumbnail
Andrew Jardine, modificado hace 6 años.

RE: Issue with running portlet with ajax call after upgrading from 6.2 to 7

Liferay Legend Mensajes: 2416 Fecha de incorporación: 22/12/10 Mensajes recientes
No problem -- just a few points of clarification though.

First, AJAX works in 7 the same way it did in 6. You still have a portlet class in which you can define a serveResource method, or now you can provide separate classes that implement the MVCResourceCommand interface to separate your logic out a little more cleanly. Nothing was removed from 7 with respect to ajax and I am using it in several plugins (using both models) without issue. If you choose to go with the MVCResourceCommand route, you just have to make sure you have both your portletId and the mapping correct otherwise the provider won' t be linked. The issue is almost certainly with the hard coded portletId -- using the wrong portletId in 6 would also cause the issue, but because the portletId is something that could change (even in 6, I could always rename my portlet) it is best to try to centralize the value in a constant or read it dynamically if you can.

The other thing you said was that the old WAR files are not supported in 7. This is not true either -- it's just that they go through a dynamic conversion process when the deployment happens to convert them from WAR files into modules that can be installed and started in the OSGI runtime. This was done by design (at least to my understanding) because Liferay recognized that the need for everyone to rewrite all of their plugins from scratch was not an optimal solution and would certainly affect the adoption rate. Again though, I suspect that the issue here is that the module that is generated has a portletId reference that follows the new standard, not the old one so the static reference in the code means that it can't find the portlet (in the container) to route the request to -- so the request dies.

If you have access to the source, I would start by trying to change the reference for the portlet url to use the <portlet:renderURL/> as Jack suggested. That might be your shortest path to success.
David Kirvan, modificado hace 6 años.

RE: Issue with running portlet with ajax call after upgrading from 6.2 to 7

New Member Mensajes: 10 Fecha de incorporación: 4/06/15 Mensajes recientes
Andrew Jardine:
No problem -- just a few points of clarification though.

First, AJAX works in 7 the same way it did in 6. You still have a portlet class in which you can define a serveResource method, or now you can provide separate classes that implement the MVCResourceCommand interface to separate your logic out a little more cleanly. Nothing was removed from 7 with respect to ajax and I am using it in several plugins (using both models) without issue. If you choose to go with the MVCResourceCommand route, you just have to make sure you have both your portletId and the mapping correct otherwise the provider won' t be linked. The issue is almost certainly with the hard coded portletId -- using the wrong portletId in 6 would also cause the issue, but because the portletId is something that could change (even in 6, I could always rename my portlet) it is best to try to centralize the value in a constant or read it dynamically if you can.

The other thing you said was that the old WAR files are not supported in 7. This is not true either -- it's just that they go through a dynamic conversion process when the deployment happens to convert them from WAR files into modules that can be installed and started in the OSGI runtime. This was done by design (at least to my understanding) because Liferay recognized that the need for everyone to rewrite all of their plugins from scratch was not an optimal solution and would certainly affect the adoption rate. Again though, I suspect that the issue here is that the module that is generated has a portletId reference that follows the new standard, not the old one so the static reference in the code means that it can't find the portlet (in the container) to route the request to -- so the request dies.

If you have access to the source, I would start by trying to change the reference for the portlet url to use the <portlet:renderURL/> as Jack suggested. That might be your shortest path to success.


Thank you for your reply. I am still working on trying to get this little portlet working on Liferay 7 with well over 100 hours of trying to get it reconfigured to run under Liferay 7.

My Ajax call was calling a JSP to obtain the JSON data. I simply could not get it to work. By that I mean it could not seem to find the other jsp that was in the Ajax URL. So Ajax does not work the same between versions of Liferay. Of course I will concede that it is not necessarily Ajax that is the issue, but the difficulty with OSGI in obtaining a URL in the same manner that a standard Java EE would. I changed my view.jsp to use a resourceURL in the Ajax call and post to a servResource method. This meant moving the processing to obtain the JSON from a JSP to a JAVA class. I had a dozen JSON files that I were being importing into my original JSP. That posed a challenge in the Java class as could not find the JSON files via using the file system. I was able to get by this issue by using bundle.getEntry and BufferReader.

Still trying to get my Highcharts graphs to display as it appears that it does not recognize the JSON data being passed back to it from the servResource method.

I concur with your second point. I have finally been able to get the WAR file deployed and Liferay 7 creates a WAB from the war file. I think I just had to make so many changes to satisfy the OSGI requirements that the original portlet war file would not get deployed.
David Kirvan, modificado hace 6 años.

RE: Issue with running portlet with ajax call after upgrading from 6.2 to 7

New Member Mensajes: 10 Fecha de incorporación: 4/06/15 Mensajes recientes
My AJAX call expects to have a JSON object returned from the serveResource method. It looks like the JSON being returned is being wrapped with other data. the object returned is throwing errors. It appears that the actual JSON data is being stored in options.details[1].response. Does this make sense to anyone? When I do use the options.details[1].response to try and obtain the JSON data I still get errors reading series data for the pie chart being created. Does anyone know if Liferay is wrapping additional data around JSON objects.
David Kirvan, modificado hace 6 años.

RE: Issue with running portlet with ajax call after upgrading from 6.2 to 7

New Member Mensajes: 10 Fecha de incorporación: 4/06/15 Mensajes recientes
Thanks for your reply. The portlet worked great under 6.2 but with Liferay 7 using OSGI and us no longer being able to have WAR files and being able to identify a URL to a JSP for AJAX calls the application no longer functions.