0

jsonからパラメーターを取得したいので、コードがあります:

    @RenderMode(name = "VIEW")
public void serveResource(ResourceRequest resourceRequest, ResourceResponse resourceResponse) throws PortletException, IOException {
            JSONObject jsonObject = JSONFactoryUtil.createJSONObject();

            resourceRequest.setAttribute("resourceUrl", "http://localhost:8080/");

             jsonObject.put("rowsPerPage", 10);
             jsonObject.put("page", 1);
             jsonObject.put("total", 100);

             PrintWriter writer = resourceResponse.getWriter();
             writer.write(jsonObject.toString());
        }

rowsPerPage、page、total を表示する JavaScript メソッドには何を書くべきか

私はJavaScriptコードを持っています:

$.getJSON('http://localhost:8080/', function(jd) {
      alert(jd.rowsPerPage);
   });

JSP ページ:

<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>

<portlet:defineObjects />

This is the <portlet:resourceURL var="testURL" id="view.jsp" escapeXml="false" />
<br/><br/>
<div id="myGrid" style="width:600px;height:500px;"></div>
<div id="pager" style="width:600px;height:20px;"></div>

しかし、それは間違っています、なぜですか?

4

1 に答える 1

1

http://localhost:8080/URLが JSON 応答を返すかどうかを確認します(ブラウザのアドレス バーに URL を直接貼り付けることができます)。これにより、Liferay のウェルカム ページが返されます。

メソッドを呼び出すには、JSP でまたはタグを使用して生成できるjQuery 関数に を渡すserveResource必要があります。resourceURLgetJSON<liferay-portlet:resourceURL><portlet:resourceURL>

アップデート:

resourceURLJavaScript でを使用する必要があります。

<portlet:resourceURL var="testURL" id="view.jsp" escapeXml="false" />

<script>
    $.getJSON('${testURL}', function(data) {
        alert(data.rowsPerPage);
    });
</scrip>

<!-- ${testURL}: This is EL i.e. expression language of JSP. Don't confuse it with jQuery -->
<!-- <%=testURL.toString() %>: You can also use scriptlet instead of using EL. But normally scriptlets as you might know are not recommended -->

serveResourceまた、メソッドにいくつかのログ ステートメントを含めて、メソッドが実際にスクリプトで呼び出されることを確認することをお勧めします。

Liferay で Ajax を使用する方法に関する例のリンクを次に示します。

  1. Liferay で Ajax を使用する実際の例。
  2. ajax がポータルでどのように機能するかについて、単純な例を使用して概念を説明するブログ。

お役に立てれば。

于 2012-10-26T10:40:04.613 に答える