1

要求是在保存一些数据后显示来自 portlet 类的警报框。
我怎样才能做到这一点?

我们可以PrintWriter在 Portlet 类中使用actionresponseofprocessAction()方法创建对象吗?

以下代码不起作用...

PrintWriter out = actionresponse.getWriter();
String str = "/web/guest/newpage.jsp";
out.println("<script language=\"Javascript\">");
out.println("alert(\"SAVED\");");
out.println("window.location.href=\'"+str+"\'; ");
out.println("</script>");

怎么做?请帮忙..

4

2 回答 2

1

我知道您没有使用任何 Ajax 调用来保存数据,因此最简单的方法是设置请求属性并将控件重定向到 JSP,根据您可以执行 javascript 调用的请求属性的值。

在 liferay 中使用 javascript 与在任何其他 web 应用程序中使用它没有什么不同。

这是一些示例代码片段(不是整个代码):

在您的 portlet 的操作方法中:

// either set the renderParameter 
actionRequest.setRenderParameter("saveSuccessfulPARAM", "SAVED");

// OR set the request attribute
actionRequest.setAttribute("saveSuccessfulATTR", "SAVED");

现在在成功保存后将呈现的 JSP 中,您可以拥有:

<%
// Note: You can use jstl or liferay tags instead of scriptlets if you want

String savedAttribute = renderRequest.getAttribute("saveSuccessfulATTR");

// OR you can use this, to fetch the renderParamter set by actionResponse
// String saveParameter = ParamUtil.getString(renderRequest, "saveSuccessfulPARAM")

if("SAVED".equals(savedAttribute)) { // if this is true show an alert
%>

<script>
// you can use <aui:script> tag as well

// .. your javascript code will go here
// better would be to run this script on DOM ready

    alert("Data Saved Successfully!");  // for example
</script>

<%
}
%>

注意:最好javascriptDOM准备好的情况下运行。

于 2013-02-05T08:07:45.443 回答
1

使用 Liferay 的 JavaScript API:http ://www.liferay.com/community/wiki/-/wiki/Main/Liferay+JavaScript+API

于 2013-01-21T10:26:54.467 回答