0

Adobe CQ は初めてです。この質問の仕方がわかりません

ドロップダウンを動的に設定する必要があり、ドロップダウンはスクリプトレットに JSON 応答オブジェクトを持つ JSP を呼び出す必要があり、Jsp はサーブレットから Json オブジェクトを取得する必要があります。

私のjspは以下の形式のようになります:

dropdownpopulate.jsp

<%@ page import="com.day.cq.wcm.api.WCMMode,
                   com.day.cq.wcm.api.components.DropTarget%>

<%
  [
  {key1,value1},
   {key2,value2},
 {key2,value3}

]

%>

したがって、私の jsp で次の jquery を使用する予定です。

<script>
$(document).ready(function() {
    $.get('\ActionServlet',function(responseJson) {                          
          alert('response json:' + responseJson);   
    });
});      
</script>

しかし、これを上記の形式で JSP に入れる方法は?

4

2 に答える 2

0
$.ajax({

        url : "NameServlet",
        dataType : 'json',
        error : function() {

            alert("Error");
        },
        success : function(data) {
            $.each(data.jsonArray, function(index) {
                var selectBox="<select>"
                  $.each(data.jsonArray[index], function(key, value) {
                    selectBox+="<option>"+key + " & value " + value + "</option>";

                 }); 
                 selectBox+="</select>";
                 // given html id which you want to put it 
                 $("#htmlid").html(selectBox);
            });

        }
});

それがあなたの助けになることを願っています。

于 2013-04-04T06:18:18.940 に答える
0

jsp は、応答で JSON を出力する必要があります。

JSP ファイル:

<%
    //obtain the data from a query
    //asuming getClients() return a String in JSON format
    String clients = DB.getClients();

    //this prints de json in the response out
    out.print(clients);
%>

この後、ajax コールバックで json オブジェクトを含む文字列にアクセスできます。

HTML ファイル (または別の JSP ファイル):

<script type="text/javascript">
    //url from your JSP page
    //data contains the output printed previously
    $ajax(url,function(data){
        //it is convenient to clean de output
        var stringData = data.trim();

        //now that you have a json formated String you need to use a parser that
        //converts the String into a JsonObject
        var jsonData = JSON.parse(stringData);

        //take some actions with the data obtained
        var htmlVar = '';
        for (var i=0; i<jsonData.length; i++){
            //add to htmlVar the HTML code for the options 
            htmlVar += '<option value="'+jsonData[i].clientId+'">'+jsonData[i].clientName+'</option>'
        }
        //load the data into the dropDown element
        $('#clientsDropDown').html(htmlVar)
    });
</script>
于 2013-04-04T04:09:23.177 に答える