如何使用liferay portlet中的ajax返回响应的对象?

时间:2017-08-11 11:22:39

标签: jquery ajax liferay portlet

我需要在success方法上返回一个字符串。 这是我在jsp页面中的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();
    }

结果它在警告信息中显示一个空字符串。 怎么处理呢?

更新 我将控制器方法更改为

 @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();
}

我的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:找不到portlet请求的处理程序:mode'view',阶段'RESOURCE_PHASE',参数map ['search' - &gt; array [''],'page' - &gt;阵列[ '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();
    }


}

1 个答案:

答案 0 :(得分:2)

以下是您更新的代码的问题:

我。您尚未根据idportlet:resourceURL上指定portlet:actionURL属性(包含所需值)。将其更改为:

<portlet:resourceURL id="filterDocuments" var="filterDocuments" />

II。操作类中的映射方法签名与UI上的id属性值portlet:resourceURL不同,请将其更改为:

@ResourceMapping("filterDocuments")
public void filterDocuments(ResourceRequest request, ResourceResponse response)
throws IOException, SystemException {
    PrintWriter writer = response.getWriter();
    writer.write("text");
    writer.flush();
    writer.close();
}

III。从操作类中删除其他serveResource方法,因为它仅适用于Generic(默认)portlet类型。

<强>参考: Liferay Spring Portlet Tutorial