问题描述
我是liferay Portlet开发的初学者,我正在开发一个接收http get请求,处理一些信息并且必须返回json对象的portlet.我的问题是我的Portlet发送了整个html页面,而不仅仅是json对象.这是我的代码:
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();
我在doView()方法中执行了此操作,我知道这不是最佳实践,但目前我对此并不关心.有人可以向我解释如何仅返回json对象,但我已经阅读了有关serveResponse的内容,但无法弄清楚如何调用它.
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.
推荐答案
请注意,doView()
主要负责呈现Portlet.您的要求可以通过
更好地实现1-processAction
(Portlet操作)或
2-serveResource
(Portlet AJAX服务).
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).
在我看来,AJAX请求-响应将是最合适的情况;为此,您只需要在portlet视图上创建资源URL.喜欢:
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" />
在页面上添加一个JavaScript方法,您可以在其中向Portlet生成AJAX请求.该方法看起来像这样,
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>
在按钮/链接点击事件上调用该ajax方法:
Call that ajax method on a button / link click event:
<input type="button" onclick="_callAjax('val1', 'val2')" value="Send" />
最后,在您的portlet的动作侦听器类中,添加以下serveResource
方法,该方法负责处理基于AJAX的请求.
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 :)
这篇关于在portlet中用json对象响应http请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!