我需要在成功方法上返回一個字符串。 這是我在jsp頁面中的AJAX代碼。如何在liferay portlet中使用ajax返回對象?
<portlet:actionURL name="filterDocuments" var="filterDocuments">
<portlet:param name="action" value="filterDocuments"/>
</portlet:actionURL>
<script type="application/javascript">
var filterDto = {
page: 0,
tags: {},
search: null
};
$.ajax({
type: "POST",
url: "<%=filterDocuments%>",
data: filterDto,
success: function (msg) {
alert(msg);
}
});
</script>
這裏是我在後端的處理程序。
@ActionMapping(params = "action=filterDocuments")
public void filterDocuments(ActionRequest actionRequest, ActionResponse actionResponse) throws IOException {
PrintWriter writer = PortalUtil.getHttpServletResponse(actionResponse).getWriter();
writer.write("text");
writer.flush();
writer.close();
}
結果它顯示了警告消息中的空字符串。 如何處理它?
UPDATE 我改變控制器方法
@ResourceMapping(value = "filterDocuments")
public void filterDocuments(ActionRequest actionRequest, ActionResponse actionResponse) throws IOException {
PrintWriter writer = PortalUtil.getHttpServletResponse(actionResponse).getWriter();
writer.write("text");
writer.flush();
writer.close();
}
和URL在我的JSP是
<portlet:resourceURL var="ajaxResourceURL"/>
<script type="application/javascript">
var filterDto = {
page: 0,
tags: {},
search: null
};
$.ajax({
type: "POST",
url: "<%=ajaxResourceURL%>",
data: filterDto,
success: function (msg) {
alert(msg);
}
});
</script>
而現在它給了我一個異常
org.springframework。 web.portlet.NoHandlerFoundException:找不到處理程序門戶請求:模式 '觀看',相位 'RESOURCE_PHASE',參數映射[ '搜索' - >陣列[ ''], '頁' - >陣列[ '0']]
全控制器類
@Controller
@RequestMapping("VIEW")
@Log4j
public class SpringMVCController extends MVCPortlet {
@RenderMapping
public String view(RenderRequest request, RenderResponse response) {
String view = PropertiesConfig.getAsString("views", request);
return view;
}
@Override
public void serveResource(ResourceRequest resourceRequest,
ResourceResponse resourceResponse)
throws IOException, PortletException {
try {
PrintWriter writer = resourceResponse.getWriter();
writer.write("text");
writer.flush();
writer.close();
CaptchaUtil.serveImage(resourceRequest, resourceResponse);
} catch (Exception e) {
log.error(e.getMessage());
}
}
@ResourceMapping(value = "action=filterDocuments")
public void filterDocuments(ActionRequest actionRequest, ActionResponse actionResponse) throws IOException {
PrintWriter writer = PortalUtil.getHttpServletResponse(actionResponse).getWriter();
writer.write("text");
writer.flush();
writer.close();
}
}
與您的代碼的問題是,你調用'使用AJAX,這不是一個有效的模式actionURL'。 –
將'portlet:actionURL'改爲'portlet:resourceURL'和'@ ActionMapping'改爲'@ResourceMapping'。 –
[在portlet中使用json對象響應http請求]的可能重複(https://stackoverflow.com/questions/17592144/respond-to-http-request-with-json-object-in-portlet) –