RE: Liferay 7 recieve data in ...Portal.java from view.jsp

Matej Dragicevic, modified 7 Years ago. New Member Posts: 15 Join Date: 3/29/18 Recent Posts

Greetings,

I have trouble sending my data from my view.jsp to my ...Portlet.java class with the liferay 6 way

 

what I am trying to do is the following

view.jsp:

<div class="bottom">
    <input class="button" type="button" name="testData" value="Test Data" onclick="testData();"/>
</div>
<script>
    var testJsp = '<portlet:resourceURL id="testJsp" />';

    var lastName = 'Dragicevic';
    var firstName = 'Matej';
    console.log(testJsp);
    console.log(lastName);
    console.log(firstName);


    testData = function() {
        jQuery.ajax({
            url: testJsp,
            cache: false,
            data: {
                lastName : lastName,
                firstName : firstName
            },
            type: "POST",
            dataType: "json",
            complete: function() {
            },
            success: function(dataJson) {
                alert("ajax success");
            },
            error: function(error) {
                alert("ajax error");
            }
        });
    }
</script>

TestPortlet.java:

@Override
public void serveResource(final ResourceRequest resourceRequest, final ResourceResponse resourceResponse)
      throws IOException, PortletException {

   final String resourceId = resourceRequest.getResourceID();

   if ("testJsp".equals(resourceId)) {
      testJsp(resourceRequest, resourceResponse);
   }

   super.serveResource(resourceRequest, resourceResponse);
}

private void testJsp(ResourceRequest resourceRequest, ResourceResponse resourceResponse) {
   String lastName = ParamUtil.getString(resourceRequest, "lastName", "xxx");
   String firstName = ParamUtil.getString(resourceRequest, "firstName", "yyy");
   LOG.warn("The nameString is this: " + lastName + "  " + firstName );
}

This was working in liferay 6 and I would like to know what the way to do this in liferay 7 is.

I am getting an ajax error but I am still able to get into the testJsp() java method and log the xxx and yyy nameStrings. I guess the format of the data is somehow wrong.

Is there another way to do this or am I doing the top version somehow wrong?

BR

Matej

thumbnail
Minhchau Dang, modified 7 Years ago. Liferay Master Posts: 598 Join Date: 10/22/07 Recent Posts
Matej Dragicevic:

This was working in liferay 6 and I would like to know what the way to do this in liferay 7 is.

Looks like none of your parameters are namespaced. Some of the early changes in Liferay 6.2 were LPS-35309, which made Liferay not read un-namespaced parameters, and LPS-37706, which at least allowed it to be configurable at the portlet level.

So, since 7.x inherits that behavior, unless you specifically added &lt;requires-namespaced-parameters&gt;false&lt;/requires-namespaced-parameters&gt; to your liferay-portlet.xml (if creating a legacy portlet), or add com.liferay.portlet.requires-namespaced-parameters=false as a component property (if creating a portlet with OSGi), your parameters won't get read.

Matej Dragicevic, modified 7 Years ago. New Member Posts: 15 Join Date: 3/29/18 Recent Posts

Thank you.