【问题标题】:How to get Json object in JSP scriptlet coming from Servlet如何在来自 Servlet 的 JSP scriptlet 中获取 Json 对象
【发布时间】:2013-04-04 03:10:19
【问题描述】:

我是 Adob​​e CQ 的新手。我什至不知道如何提出这个问题

我必须动态填充一个下拉列表,下拉列表应该调用一个 JSP,该 JSP 将在 scriptlet 中有 JSON 响应对象,Jsp 应该从 servlet 获取 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?

【问题讨论】:

    标签: jquery jsp adobe


    【解决方案1】:
    $.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);
                });
    
            }
    });
    

    希望对你有帮助。

    【讨论】:

    • 我在其他页面中的选择框,当他们点击选择框时,它将触发一个jsp,该jsp应该在scriptlet中有一个json对象,json对象应该从servlet获取它。
    【解决方案2】:

    您的 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-08-11
      • 1970-01-01
      • 2014-03-10
      • 2010-10-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-14
      相关资源
      最近更新 更多