user872220
user872220

Reputation: 71

Making Ajax request in portlets for liferay 6

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..

Upvotes: 7

Views: 15651

Answers (4)

Nahuel Barrios
Nahuel Barrios

Reputation: 1910

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>

Upvotes: 3

Ravi Kumar Gupta
Ravi Kumar Gupta

Reputation: 1798

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

Upvotes: 0

Jignesh Shukla
Jignesh Shukla

Reputation: 101

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

Upvotes: 10

mvmn
mvmn

Reputation: 4047

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.

Upvotes: 0

Related Questions