Making Ajax request in portlets for liferay 6
Asked Answered
O

4

7

I want to make an ajax call inside my jsp file which calls processAction method of a portlet, based on the success message from processAction method i need to make another call to serveResource method of portlet,please provide some examples..

Osteotome answered 10/8, 2011 at 6:55 Comment(1)
You can find one example on minimal reproducible example[1] [1]: https://mcmap.net/q/1479290/-ajax-in-liferay-portletJetton
I
10

In portlets, processAction() methods are automatically followed by render method and hence ajax response will get embedded with HTML fragment generated by render method. So writing ajax in portlets is a bit tricky.

Have a look at this blog of mine.

http://ajax-and-portlets.blogspot.com/2011/09/ajax-best-practice-in-portlets.html

It gives an insight view of what's the best practice to implement ajax in portlets (for both JSR-168 and JSR-286 portlets).

In case you want sample portlets, you can contact me through the contact details from the blog. I'll be happy to help you.

Thanks Jignesh

Introspect answered 29/9, 2011 at 13:52 Comment(0)
A
3

This question worked for me.

Basically, the Controller

@Controller
@RequestMapping("VIEW") // VIEW mapping (as opposed to EDIT)
public class MyPortlet {
    @RenderMapping
    public String handleRenderRequest(RenderRequest request, RenderResponse response) {
        return "defaultRender";
    }

    @ResourceMapping("myURL")
    public void handleMyResource(ResourceRequest request, ResourceResponse response) {
        OutputStream outStream;
        try {
            outStream = response.getPortletOutputStream();
            ObjectMapper mapper = new ObjectMapper();

            mapper.writeValue(outStream, "Hello world!");
        } catch (IOException ex) {
            // TODO : Do something with errors.
        }
    }
}

And the JSP:

<portlet:resourceURL id="myURL" var="myURL"/>

<script type="text/javascript">
    var urlink = "<%= myURL %>";
    $.ajax({
        url: urlink,
        cache: false,
        type: "POST",
        success: function(jsondata) {
            console.log(jsondata);
        }
    });
</script>
Ammoniacal answered 8/5, 2013 at 19:38 Comment(0)
D
0

based on the success message from processAction method That's not the right way to do it. On calling portlet action URL in response you get usual render response, so you'll get page with all the portlets. Instead you should use Portlet 2.0 resource serving feature, and return your response as a resource.

Doe answered 9/9, 2011 at 15:16 Comment(0)
A
0

You can check out my portlet which has examples for both serveResource and processAction methods calling. Ajax Jquery Portlet

Absolve answered 11/5, 2012 at 11:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.