Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

respond to http request with json object in portlet

I am a beginner in liferay portlet development and I am developing a portlet that receives a http get request, processes some information and than it has to return a json object. My problem is that my portlet sends a whole html page instead of just the json object. This is my code:

HttpServletResponse servletResponse = PortalUtil.getHttpServletResponse((renderResponse));

            servletResponse.setHeader("Content-type", "application/json");
            servletResponse.setCharacterEncoding("application/json");
            PrintWriter out = servletResponse.getWriter();
            out.write(EntityUtils.toString(responseEntity));


            out.flush();
            out.close();

I execute this in the doView() method I know that this is not the best practice, but I am not concerned with that at the moment. Can someone explain to me how to return just the json object I read something about serveResponse, but I couldn't figure out how to invoke it.

like image 903
layser Avatar asked Jul 11 '13 11:07

layser


1 Answers

Well, one thing to notice, that the doView() is mostly responsible for rendering of your portlet. Your requirement can better achieved by
1 - processAction(Portlet Action) or
2 - serveResource(Portlet AJAX service).

To my view, AJAX request-response will be the most suitable case; for that you just need to create a resource URL on your portlet view. Like:

<portlet:resourceURL var="ajaxResourceURL" />

Add a JavaScript method to the page, from where you can generate AJAX request to your portlet. The method will look something like this,

<script type="text/javascript">
function _callAjax(val1, val2){
    var url = '<%=ajaxResourceURL %>';    // pass resource URL you created using scriplet / EL.
    jQuery.ajax({
    type : "POST",
    url : url,
    cache:false,
    dataType: "json",
    data: {
      value1: val1,    // extra parameters if you want to pass
      value2: val2
    },    
    success : function(data){
        // do whatever you want with your response data
    },
    error : function(XMLHttpRequest, textStatus, errorThrown){
       alert(errorThrown);
       alert(textStatus);
    }
  });
};
</script>

Call that ajax method on a button / link click event:

<input type="button" onclick="_callAjax('val1', 'val2')" value="Send" /> 

And finally, in your portlet's action listener class add the following serveResource method, responsible for handling AJAX based request(s).

Here you can get your request parameters and generate a response in the sense you want:

@Override
public void serveResource(ResourceRequest request, ResourceResponse response) throws PortletException,IOException {

String value1 = ParamUtil.getString(request , "value1", "default");    // request parameters
String value2 = ParamUtil.getString(request , "value2", "");

PrintWriter writer = response.getWriter();

JSONObject jsonObject = new JSONObject();
jsonObject.put(String key, boolean/long/Collection/double/Map/Object value);

writer.write(jsonObject.toString());
}

Thats it! Hope, this will be helpful for you :)

like image 67
Parkash Kumar Avatar answered Oct 12 '22 11:10

Parkash Kumar