Liferay:AJAX 无法在配置模式下调用?

Liferay: AJAX call in configuration mode impossible?

是否可以在配置模式下使用AJAX?

我正在使用自定义 class 扩展 DefaultConfigurationAction 以在配置模式下自定义我的 portlet。我重写了 processAction 和 render 方法,它们工作正常,但是当我尝试实现 serveResource 方法时,它永远不会被调用(返回状态为 200 OK,但没有数据被获取并且没有调试消息被打印到 Liferay 控制台)。

我的 serveResource 方法代码:

public class TestConfigurationController extends DefaultConfigurationAction {
...
    @Override
    public void serveResource(PortletConfig portletConfig, ResourceRequest resourceRequest,
            ResourceResponse resourceResponse) throws PortletException, IOException, Exception {
        String resourceID = resourceRequest.getResourceID();
        System.out.println("Resource id=" + resourceID
                + " in TestConfigurationController.serveResource().");         // this message never prints, method is not invoked
        if (IMG_EDIT_ADD_NEW.equals(resourceID)) {
            // more code
            include(EDIT_NEW_IMAGE, context, resourceRequest, resourceResponse);    // uses PortletRequestDispatcher, returns a JSPF fragment
        } else {
            super.serveResource(portletConfig, resourceRequest, resourceResponse);
        }
    }
}

我尝试了 JS 方面的所有选项,包括 JQuery 和 AUI。这是 configuration.jsp 中的相关代码:

<portlet:resourceURL var="newImageJsp" id = "<%=IMG_EDIT_ADD_NEW%>">
</portlet:resourceURL>

<aui:button name="addNewImage" type="button" value="${addImage}"/>
<div id="<portlet:namespace/>newImageContainer">
    <aui:field-wrapper name="newImageContainer" label="${addImage}">
    </aui:field-wrapper>
</div>

<script type="text/javascript" charset="utf-8">
// Even this simple AUI AJAX call does not trigger serveResource method!

//    AUI().ready('aui-base', 'aui-module', 'node', 'aui-io-request', function (A) {
//        A.io.request('<%=newImageJsp.toString()%>');
//    });

    jQuery(document).ready(function () {
        jQuery('#<portlet:namespace/>addNewImage').on('click', function (event) {
            console.log('addNewImage clicked, url: ${newImageJsp}');                   // returns correct url
            jQuery.ajax({
                dataType: 'text',
                url: '${newImageJsp}',
                success: function (data, status) {
                    console.log('returned resource: ' + data);          // returns empty string
                    console.log('returned status: ' + status);            // returns 200 OK, which is also in the Firebunetwork panel
                    $('#<portlet:namespace/>newImageContainer').html(data);
                }
            });
            return false;
        });
    });
</script>

在控制台中调试显示,JS 工作正常,函数被调用并返回状态为 200 OK。但是,返回的数据是空的,服务器上的 serveResource 方法从未被调用过。

作为实验,我也尝试设置

<aui:form action="${newImageJsp}" method="get" name="fm1">

它也没有调用 serveResource 方法,而是返回了配置的 portlet 的 view.jsp。

最后是我的配置,与 working case 完全一样:

portlet.xml:

<portlet>
        <portlet-name>test-portlet</portlet-name>
        <portlet-class>org.springframework.web.portlet.DispatcherPortlet</portlet-class>
        <init-param>
            <name>contextConfigLocation</name>
            <value>/WEB-INF/spring-context/portlet/test-portlet.xml</value>
                </init-param>
                <init-param>
                    <name>config-template</name>
                    <value>/WEB-INF/jsp/carousel/configuration.jsp</value>
                </init-param>
        <expiration-cache>0</expiration-cache>
        <supports>
            <mime-type>text/html</mime-type>
                        <portlet-mode>view</portlet-mode>
                        <portlet-mode>edit</portlet-mode>
        </supports>
        <portlet-info>
            <title>Test</title>
        </portlet-info>
    </portlet>

和liferay-portlet.xml:

<liferay-portlet-app>
    <portlet>
        <portlet-name>test-portlet</portlet-name>
        <icon>/icon.png</icon>
                <configuration-action-class>com.test.TestConfigurationController</configuration-action-class>
                <requires-namespaced-parameters>false</requires-namespaced-parameters>
                <ajaxable>true</ajaxable>
        <header-portlet-css>/css/main.css</header-portlet-css>
                <header-portlet-javascript>/js/jquery-1.11.3.min.js</header-portlet-javascript>
        <header-portlet-javascript>/js/main.js</header-portlet-javascript>
    </portlet>
</liferay-portlet-app>

所以,看来我也有类似的问题,因为这个 unresolved issue

我在想,也许是window状态?配置模式总是使用 'pop-up',但在所有示例中我只发现 AJAX 调用使用 'normal' window 状态。也许这就是问题所在?甚至可以在弹出模式下进行异步 JSPF 加载吗?甚至在配置 window 中?我从来没有找到在 配置模式 中使用 AJAX 的工作示例,官方 Liferay 只有视图模式的示例。

最后但同样重要的是,我在 view.jsp 中针对视图模式测试了相同的代码,并且 TestViewController 中的资源服务方法被调用为 OK。我在这里使用了 Spring 注释(@ResourceMapping)。所以问题一定出在 Liferay 和配置模式上。可能是一个错误?

谢谢!

我做了类似的事情并在 resourceResponse 中使用了 PrintWriter 对象:

PrintWriter writer = resourceResponse.getWriter();
writer.print([yourResult]);

http://liferayiseasy.blogspot.hk/2015/03/ajax-call-in-spring-mvc-portlet.html

您还可以添加一个 class extends MVCPortlet

你之前的view.jsp

<portlet:resourceURL var="newImageJsp" name="newImageResource"
</portlet:resourceURL>

...

// create a new class:
    public class CustomResourceController extends MVCPortlet {
    ...
        @Override(name="newImageResource") // <---- define the name attribute which match with view.jsp
        public void serveResource(PortletConfig portletConfig, ResourceRequest resourceRequest,
                ResourceResponse resourceResponse) throws PortletException, IOException, Exception {
            String resourceID = resourceRequest.getResourceID();
            System.out.println("Resource id=" + resourceID
                    + " in TestConfigurationController.serveResource().");         // this message never prints, method is not invoked
            if (IMG_EDIT_ADD_NEW.equals(resourceID)) {
                // more code
                include(EDIT_NEW_IMAGE, context, resourceRequest, resourceResponse);    // uses PortletRequestDispatcher, returns a JSPF fragment
            } else {
                super.serveResource(portletConfig, resourceRequest, resourceResponse);
            }
        }
    }

所以,我尝试了liferay-portlet:resourceURL portletConfiguration="true"portlet:resourceURL,也在发送前手动解析和修改url。资源服务方法(无论是 serveResource 的实现,还是使用 Spring MVC 或 Liferay MVC(MVCPortlet 的 class 的实现)的全新方法),none 在配置模式下工作。对我来说似乎是一个错误,因为在官方文档中甚至没有提到这个特性。

我的解决方案是完全避免资源服务,而是选择行动阶段 (p_p_lifecycle=1)。它在 AJAX 中是完全可行的,只需在我的 DefaultConfigurationAction 实现 class.

中重写 processAction 方法

希望这可以节省我花费的无数时间。