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>

2 comments:

  1. I'm doing something similar to this..

    but I cant get my content jsp compiled to html.. insead I get as is jsp in response data.

    Do you think, I'm missing something?

    Thanks in advance.

    ReplyDelete
  2. set content type before calling request dispatcher also validate jsp page directive.

    ReplyDelete