1

要件は、いくつかのデータを保存した後、ポートレット クラスからアラート ボックスを表示することです。
これどうやってするの?

ofメソッドPrintWriterを使用して、Portlet クラスにオブジェクトを作成できますか?actionresponseprocessAction()

次のコードが機能しません...

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

I understand that you are not using any Ajax calls to save the data, so the simplest way would be to set a request attribute and redirect the control to a JSP, based on the value of the request attribute you can execute a javascript call.

Using javascript in liferay is nothing different from using it in any other web-application.

Here is some sample code snippet (not the whole code):

In your portlet's action method:

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

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

Now in the JSP which would be rendered after successful save you can have:

<%
// 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>

<%
}
%>

Note: It would be better to run the javascript on DOM ready.

于 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 に答える