Search code examples
liferayportlet

Multiple Ajax calls liferay portlets


I am having liferay portlet and I need to heavily depend upon the AJAX calls. So I need to make multiple calls to serveResource method. One way to do the same is that I can pass a parameter with the URL and then differentiate the request according to that parameter.

But in my case I have to call serveResource so many times due to which the method will be difficult to maintain. Is there any framework to do so? Using which the code becomes maintainable.


Solution

  • Use Spring MVC framework and call different method based on your business logic/user action in controller,

    Try below code in jsp

    <portlet:resourceURL var="loadContents" id="loadContents"></portlet:resourceURL>
    <portlet:resourceURL var="loadCategories" id="loadCategories"></portlet:resourceURL>
    

    ajax call in jsp

    AUI().ready(
            function(A) {            
                A.use('aui-io-request', 
                        function(aui) {
                        A.io.request("<%=loadContents%>", {
                            autoLoad : false,
                            cache : false,
                            dataType : 'json',
                            data:{},
                            method:'POST',
                            on : {
                                success : function(event, id, xhr) {
                                    var response = this.get('responseData');
                                     // add logic here after response
                                }
                            }
                        }).start();
                    });
            });
    

    in controller/ java class

        @ResourceMapping("loadCategories")
        public void loadCategories(final ResourceRequest resourceRequest, final ResourceResponse resourceResponse)
        {
             // your business logic goes here
        }
    
        @ResourceMapping("loadContents")
        public void loadContents(final ResourceRequest resourceRequest, final ResourceResponse resourceResponse)
        {
             // your business logic goes here
        }
    

    hope above code snippets will help you and you get what you were looking for!!!