bartex9
bartex9

Reputation: 391

Calling portlet in Liferay 7

I have a simple portlet which returns a JSON object. Here's the code of the portlet:

package example.portlet;

@Component(
    immediate = true,
    property = {
        "com.liferay.portlet.display-category=category.sample",
        "com.liferay.portlet.instanceable=true",
        "javax.portlet.display-name=Example Portlet",
        "javax.portlet.init-param.template-path=/",
        "javax.portlet.init-param.view-template=/view.jsp",
        "javax.portlet.resource-bundle=content.Language",
        "javax.portlet.security-role-ref=power-user,user"
    },
    service = Portlet.class
)
public class ExamplePortlet extends MVCPortlet {

    @Override
    public void serveResource(ResourceRequest request, ResourceResponse response) throws IOException, PortletException {
        ...
    } 
}

I would like to call this portlet using A.io.request from existing JavaScript which is outside of portlet module. How can I do that? What url should I provide?

Upvotes: 0

Views: 195

Answers (2)

Milen Dyankov
Milen Dyankov

Reputation: 3052

As Olaf pointed out, you don't get an URL to a portlet. You can get URL to a portlet instance that is placed on some page in some site in some server instance.

If you simply want something that serves JSON (regardless of the page/site/instance), you don't have to write a portlet for it. You can just write restful service that Liferay will happily serve for you. An example can be found here.

Upvotes: 0

Olaf Kock
Olaf Kock

Reputation: 48057

A portlet has a URL depending on the page its on and potentially then on the instance, as it could be added to the page multiple times. Thus one cannot really know "the" URL. If this is rightfully portlet code, you should encapsulate its call or at least the URL resolution in the portlet. Otherwise it might need to be in a servlet or even both (e.g. in a library called by the servlet and a portlet).

Upvotes: 1

Related Questions