Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
429 views
in Technique[技术] by (71.8m points)

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 :)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...