Java Liferay:配置模式下的AJAX调用不可能?
可以在配置模式下使用AJAX吗 我正在使用自定义类扩展DefaultConfigurationAction在配置模式下自定义我的portlet。我重写了processAction和render方法,它们工作正常,但当我尝试实现ServerResource方法时,它从未被调用(返回的状态为200 OK,但未提取数据,也未向Liferay控制台打印调试消息) 服务器资源方法的我的代码:Java Liferay:配置模式下的AJAX调用不可能?,java,ajax,configuration,liferay,portlet,Java,Ajax,Configuration,Liferay,Portlet,可以在配置模式下使用AJAX吗 我正在使用自定义类扩展DefaultConfigurationAction在配置模式下自定义我的portlet。我重写了processAction和render方法,它们工作正常,但当我尝试实现ServerResource方法时,它从未被调用(返回的状态为200 OK,但未提取数据,也未向Liferay控制台打印调试消息) 服务器资源方法的我的代码: public class TestConfigurationController extends DefaultC
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>
:
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-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>
测试portlet
org.springframework.web.portlet.DispatcherPortlet
上下文配置位置
/WEB-INF/spring上下文/portlet/test-portlet.xml
配置模板
/WEB-INF/jsp/carousel/configuration.jsp
0
文本/html
看法
编辑
试验
和liferay-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-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>
测试portlet
/icon.png
com.test.TestConfigurationController
假的
我在想,也许是窗口状态?配置模式总是使用“弹出”,但在所有示例中,我只发现使用“正常”窗口状态的AJAX调用。也许这就是问题所在?甚至可以在弹出模式下进行异步JSPF加载吗?甚至在配置窗口中?我从未找到在配置模式下使用AJAX的有效示例,而官方Liferay只有视图模式的示例
最后但并非最不重要的一点是,我在view.jsp中测试了视图模式的相同代码,TestViewController中的资源服务方法被称为OK。我在这里使用了Spring注释(@ResourceMapping)。因此,问题一定出在Liferay和配置模式上。也许是虫子
谢谢大家! 我做了类似的操作,并在resourceResponse中使用了PrintWriter对象:
PrintWriter writer = resourceResponse.getWriter();
writer.print([yourResult]);
您还可以添加一个扩展MVCPortlet的类
您以前的view.jsp
<portlet:resourceURL var="newImageJsp" name="newImageResource"
</portlet:resourceURL>
因此,我尝试了liferayportlet:resourceURL portletConfiguration=“true”
和portlet:resourceURL
,也尝试了在发送之前手动解析和修改url。资源服务方法(无论是serversource
的实现,还是使用Spring MVC或Liferay MVC(实现类MVCPortlet
)的全新方法)都无法在配置模式下工作。对我来说,这似乎是一个bug,因为在官方文档中甚至没有提到这个特性
我的解决方案是完全避免资源服务,而是选择操作阶段(p_p_lifecycle=1)。这在AJAX中是完全可行的,只需在我的DefaultConfigurationAction
实现类中重写processAction
方法
希望这能帮某人省下我与它共度的无数时间。好吧,这不是我的情况,因为ServerSource方法甚至没有被调用。这是因为ConfigurationAction不提供resourceAction方法。你可以做的一个方法是编写另一个MVCportlet页面并听取你的建议。这是因为ConfigurationAction不提供resourceAction方法,所以我不明白他们为什么要这么麻烦使用准抽象ServerResource方法和整个ResourceServingConfigurationAction类?无论如何,我用了行动阶段,看我的答案。