Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 685
user872220 Avatar asked Aug 10 '11 06:08

user872220


People also ask

How do you send Ajax request every 5 seconds?

Creating a function that calls the AJAX request and using this function in setInterval() and set Interval for 5 sec. Now the function executes every 5 seconds and fetches new data from the server. It repeatedly executes the function even when the previous AJAX request is not successfully executed and return.

Which function is used to initiate Ajax request?

Approach 1: In this approach, we will use the XMLHttpRequest object to make Ajax call. The XMLHttpRequest() method which create XMLHttpRequest object which is used to make request with server. Syntax: var xhttp = new XMLHttpRequest();


2 Answers

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>
like image 36
Nahuel Barrios Avatar answered Sep 21 '22 18:09

Nahuel Barrios


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

like image 154
Jignesh Shukla Avatar answered Sep 20 '22 18:09

Jignesh Shukla