Showing posts with label Ajax. Show all posts
Showing posts with label Ajax. Show all posts

Friday, December 21, 2012

ResourceServing Portlet JSR286



In general to implement serveResource you need to follow these steps :
 
1) Implement serveResource method in Portlet
 
2) Define the content Page - This content page is JSP that the serveResource() method forwards. Always create a separate content page for the output of the serveResource() method. A resource JSP is not mandatory because the output can be written directly on the response. The output of the serveResource() method can be HTML, XML, JSON, static resource and so on.
i.e.
public void serveResource(ResourceRequest request, ResourceResponse response) throws PortletException, IOException {
                String targetJsp="sample_resource.jsp";
                //Invoke business methods here
                //.......
                PortletRequestDispatcher rd=getPortetContext().getRequestDispatcher(targetJsp);
                rd.include(request,response);
}
Note : Don't use renderRequest and renderResponse object in resource.jsp instead use resourceRequest and resourceResponse if required

3) Generate the ResourceServing URL : The resource serving URL targets the serveResource() method
<div id="divToReplace">
                Content will be fetched dynamically
</div>

<form id="myForm" method="post" action="<portlet:resourceURL/>">
                <input type="submit" value="Get Data"/>
</form>
<script>
                var form=dojo.byId("myForm");
                dojo.connect(form,"onsubmit",function(event) {
                                //stop the submit event since we want to control form submission
                                event.preventDefault();
                                event.stopPropagation();

                                var xhrArgs={
                                                form: dojo.byId("myForm"),
                                                handleAs : "text",
                                                load : function(data) {
                                                                //callback function invoked when the response comes back successfully
                                                                dojo.byId("divToReplace").innerHTML=data;
                                                }
                                };
                                //perform AJAX call
                                dojo.xhrPost(xhrArgs);
                });
</script>