RE: Liferay 7.1 Ajax calls and parameters

Kevin Neibarger, modified 6 Years ago. Regular Member Posts: 105 Join Date: 2/2/18 Recent Posts
So, I'm seeing some odd namespace appending to form names/ids in my portlet when using an osgi module mvc-portlet in Liferay 7.1. I'm converting alot of 6.2 code to 7.1 and there is a weird "INSTANCE_" text string being appended before the variable name and I can't get it using ParamUtil in my serveResource method. 

Currently, in my serveResource method in the MVC Portlet I have a line of code

String dbConnection = ParamUtil.getString(resourceRequest, "dbSelection");

So, I'm trying to get a drop-down element with the variable "dbSelection", which is passed in via an Ajax call in jQuery
var url ='&lt;%=updatePopulationForUserURL%&gt;'+'&amp;<portlet:namespace />dbSelection=' + dbConnection;

When I print out the above url via an alert, I see something like this:
_practiceprofile_INSTANCE_7n0ap8UBjT2i_dbSelection

So, when I print out that variable value from the serveResource method I of course get null.. Why is this? And is there a new api like ParamUtil that will get that variable?




 
thumbnail
Olaf Kock, modified 6 Years ago. Liferay Legend Posts: 6441 Join Date: 9/23/08 Recent Posts
Kevin Neibarger

Currently, in my serveResource method in the MVC Portlet I have a line of code

String dbConnection = ParamUtil.getString(resourceRequest, "dbSelection");

So, I'm trying to get a drop-down element with the variable "dbSelection", which is passed in via an Ajax call in jQuery
var url ='&lt;%=updatePopulationForUserURL%&gt;'+'&amp;<portlet:namespace />dbSelection=' + dbConnection;

When I print out the above url via an alert, I see something like this:
_practiceprofile_INSTANCE_7n0ap8UBjT2i_dbSelection
In the portal (or portlet-)world, you'll always run across the namespace. Through HTTP, all parameters need to be namespaced with the portal ID. You obviously have an instantiable portlet, e.g. you can have it on the page multiple times - thus the random gibberish in there: This is to make the portlet-ID unique. In JS, on the browser side, you naturally see the fully decorated parameter. On the portal side, in Java, your portlet will receive the parameters directed to it - without the decoration. Thus, your PortalUtil.getParameter(rq, "dbSelection") is what I'd expect you need to use.

Does it not work? Or did you not try it? This should be unchanged forever (with the notable differences that early versions of Liferay used a 4-letter random string, while newer ones use the longer string)
Kevin Neibarger, modified 6 Years ago. Regular Member Posts: 105 Join Date: 2/2/18 Recent Posts
Thanks Olaf, I actually figured it out. I was actually trying to read in an attribute instead of a parameter and got my issue fixed. I was just wondering about the <portlet:namespace/> because it looks like the string that is generated from that has changed between Liferay 6.2 and 7.1 and it confused me a bit.