在 Liferay 6 的 portlet 中发出 Ajax 请求

发布于 2024-11-28 16:40:48 字数 118 浏览 0 评论 0原文

我想在我的jsp文件中进行ajax调用,该文件调用portlet的processAction方法,根据processAction方法的成功消息,我需要再次调用portlet的serveResource方法,请提供一些示例。

I want to make an ajax call inside my jsp file which calls processAction method of a portlet, based on the success message from processAction method i need to make another call to serveResource method of portlet,please provide some examples..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

〆一缕阳光ご 2024-12-05 16:40:48

在 portlet 中,processAction() 方法会自动跟随 render 方法,因此 ajax 响应将嵌入由 render 方法生成的 HTML 片段。所以在 portlet 中编写 ajax 有点棘手。

看看我的这个博客。

http://ajax-and-portlets .blogspot.com/2011/09/ajax-best-practice-in-portlets.html

它深入了解了在 portlet 中实现 ajax 的最佳实践(对于JSR-168 和 JSR-286 portlet)。

如果您需要示例 portlet,可以通过博客中的联系方式与我联系。我很乐意为您提供帮助。

谢谢
吉涅什

In portlets, processAction() methods are automatically followed by render method and hence ajax response will get embedded with HTML fragment generated by render method. So writing ajax in portlets is a bit tricky.

Have a look at this blog of mine.

http://ajax-and-portlets.blogspot.com/2011/09/ajax-best-practice-in-portlets.html

It gives an insight view of what's the best practice to implement ajax in portlets (for both JSR-168 and JSR-286 portlets).

In case you want sample portlets, you can contact me through the contact details from the blog. I'll be happy to help you.

Thanks
Jignesh

简单爱 2024-12-05 16:40:48

这个问题适用于我。

基本上,控制器

@Controller
@RequestMapping("VIEW") // VIEW mapping (as opposed to EDIT)
public class MyPortlet {
    @RenderMapping
    public String handleRenderRequest(RenderRequest request, RenderResponse response) {
        return "defaultRender";
    }

    @ResourceMapping("myURL")
    public void handleMyResource(ResourceRequest request, ResourceResponse response) {
        OutputStream outStream;
        try {
            outStream = response.getPortletOutputStream();
            ObjectMapper mapper = new ObjectMapper();

            mapper.writeValue(outStream, "Hello world!");
        } catch (IOException ex) {
            // TODO : Do something with errors.
        }
    }
}

和 JSP:

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

<script type="text/javascript">
    var urlink = "<%= myURL %>";
    $.ajax({
        url: urlink,
        cache: false,
        type: "POST",
        success: function(jsondata) {
            console.log(jsondata);
        }
    });
</script>

This question worked for me.

Basically, the Controller

@Controller
@RequestMapping("VIEW") // VIEW mapping (as opposed to EDIT)
public class MyPortlet {
    @RenderMapping
    public String handleRenderRequest(RenderRequest request, RenderResponse response) {
        return "defaultRender";
    }

    @ResourceMapping("myURL")
    public void handleMyResource(ResourceRequest request, ResourceResponse response) {
        OutputStream outStream;
        try {
            outStream = response.getPortletOutputStream();
            ObjectMapper mapper = new ObjectMapper();

            mapper.writeValue(outStream, "Hello world!");
        } catch (IOException ex) {
            // TODO : Do something with errors.
        }
    }
}

And the JSP:

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

<script type="text/javascript">
    var urlink = "<%= myURL %>";
    $.ajax({
        url: urlink,
        cache: false,
        type: "POST",
        success: function(jsondata) {
            console.log(jsondata);
        }
    });
</script>
淡淡的优雅 2024-12-05 16:40:48

基于来自 processAction 方法的成功消息
这不是正确的做法。
在调用 portlet 操作 URL 作为响应时,您将获得通常的渲染响应,因此您将获得包含所有 portlet 的页面。
相反,您应该使用 Portlet 2.0 资源服务功能,并将您的响应作为资源返回。

based on the success message from processAction method
That's not the right way to do it.
On calling portlet action URL in response you get usual render response, so you'll get page with all the portlets.
Instead you should use Portlet 2.0 resource serving feature, and return your response as a resource.

絕版丫頭 2024-12-05 16:40:48

您可以查看我的 portlet,其中包含serveResource 和 processAction 方法调用的示例。
Ajax Jquery Portlet

You can check out my portlet which has examples for both serveResource and processAction methods calling.
Ajax Jquery Portlet

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文