Handling Resource request in Spring MVC Portlet

Starting from Version 3.0.1, Spring Framework support some of the new features introduced in the Portlet Specification 2.0 (JSR 286). One of the new features in the Portlet Specification 2.0, is ability to make resource request.

The Spring Portlet MVC Framework 3.0.* has concept of ResourceAwareController interface that you can implement in your Controller class to indicate that your Controller can be used for handling resource request. You can download the sample code from here

I built this simple ServeResource portlet to demonstrate how to handle resource request in Spring Portlet MVC Framework. The sample portlet supports only VIEW mode and it has only one ViewController class for handling all the requests


package com.webspherenotes.mvc.spring.action;

import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
import javax.portlet.ResourceRequest;
import javax.portlet.ResourceResponse;

import org.springframework.web.portlet.ModelAndView;
import org.springframework.web.portlet.mvc.AbstractController;
import org.springframework.web.portlet.mvc.ResourceAwareController;

public class ViewController extends AbstractController implements ResourceAwareController{
protected ModelAndView handleRenderRequestl(RenderRequest request,
RenderResponse response) throws Exception {
System.out.println("Entering ViewController.handleRenderRequest()");
ModelAndView modelAndView = new ModelAndView("hello");
System.out.println("Exiting ViewController.handleRenderRequest()");
return modelAndView;
}
public ModelAndView handleResourceRequest(ResourceRequest arg0,
ResourceResponse arg1) throws Exception {
System.out.println("Entering ViewController.handleResourceRequest()");
ModelAndView modelAndView = new ModelAndView("helloresource");
System.out.println("Exiting ViewController.handleResourceRequest()");
return modelAndView;
}
}


The ViewController implements ResourceAwareController interface and as a result it has implement
handleResourceRequest() method which gets called whenever a resource call is made to the portlet.

  1. handleRenderRequest: This method will get called to handle render request for the portlet in the VIEW mode. In my sample portlet i am working control to hello.jsp for generating markup

  2. handleResourceRequest: This method will get called to handle resource request for the portlet. Inside this method you will get access to ResourceRequest and ResourceResponse object and after processing the request, you can return object of ModelAndView type which will be used for forwarding control to JSP. In my sample portlet i am forwarding control to helloresource.jsp for generating markup for resource request



This is how my hello.jsp, looks like. This jsp has one "Resource Call" button and when you click on that button, it makes use of JavaScript to make a HTTP Get call to portlet:resourceURL, and once the response is returned it is displaying that response as alert on the page


<%@page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1" session="false"%>
<%@taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet"%>
<portlet:defineObjects />
<h1>Hello from doView</h1>
<script type="text/javascript">
function createXMLHttpRequestObject() {
var xmlHttp;
try {
xmlHttp = new XMLHttpRequest();
} catch (e) {
try {
xmlHttp = new ActiveXObject("Microsoft.XMLHttp");
} catch (e) {
}
}
if (!xmlHttp)
alert("Error creating the XMLHttpRequest object.");
else
return xmlHttp;
}
var xmlHttp = createXMLHttpRequestObject();
function makeResourceCall(){
if (xmlHttp){
try{
xmlHttp.open("GET", "<portlet:resourceURL/>", true);
xmlHttp.onreadystatechange = handleRequestStateChange;
xmlHttp.send(null);
}catch (e){
alert("Can't connect to server:\n" + e.toString());
}
}
}
function handleRequestStateChange(){
if (xmlHttp.readyState == 4){
if (xmlHttp.status == 200){
alert(xmlHttp.responseText);

}
}
}
</script>
<input type="button" onclick="JavaScript:makeResourceCall()" value="Resource Call" />


The helloresource.jsp is pretty simple, it is just returning following text

This is response of ServeResourcePortlet.serveResource() call

2 comments:

David said...

Thanks for the article.

Very useful.

Abhi said...

Thanks for info....
SEO Company in Bangalore