显示通过JSP pag中的RESTful Web服务获取的数据库值

时间:2016-04-08 18:52:53

标签: java web-services rest jsp

我正在使用Jax-Rs实现一个消息。我有一个使用实现的API的客户端应用程序。在数据库message_id中,存储消息,日期,发件人字段。我需要在客户端应用程序的jsp页面的div中显示这些值。

client.jsp

    <form action="ClientServlet" method ="post"  onclick="onTechIdChange();"></form>
         <div id="uploadSuggest" class="center-block">
         <div id="suggestHeading" class="row">
                    <h4 class="textTitle center-block"> Messages  </h4>
                </div>      
                    <div class="row">
                    <div class="col-md-8">
           <a  id="btn_padding" href="#download" class="btn btn-image pull-left" onclick="onTechIdChange();">Create Profile</a>
           <p>   </p>
                    </div>
                    </div>

    </div>
      </div>  
    </section>

    <script>

    function onTechIdChange()   {  

          var urlPath = "http://localhost:8081/messanger/webapi/messages" ;
          $.ajax({
           url : urlPath,
           dataType : "json",
           cache: false,
           type : 'GET',
           success : function(result) {
                var details = result[0];
                var name;
                for(name in details)
                    {
                    dispatchEvent(event)
                    }
                alert(details.sender);
           },
           error : function(jqXHR, exception) {
            alert('An error occurred at the server');
           }
          });
          function display(msg) {
                var p = document.createElement('p');
                p.innerHTML = msg;
                document.body.appendChild(p);
              }
         } 
    </script>

Through this code nothing is displayed in the div. But values are printed in the tomcat console which ensures that all the methods within API are working properly. Do you have any idea? Thank you in advance


UPDATE

I Updated the javascript code snippet. But nothing is displayed inside the <p> tag



  var urlPath = "http://localhost:8081/messanger/webapi/messages" ;
  $.ajax({
   url : urlPath,
   dataType : "json",
   cache: false,
   type : 'GET',
   success : function(result) {
        var details = result[0];
        var name;
        for(name in details.sender)
            {
            display(name);
            }
        alert(details.sender);
   },
   error : function(jqXHR, exception) {
    alert('An error occurred at the server');
   }
  });
  function display(msg) {
        var p = document.createElement('p');
        p.innerHTML = msg;
        document.body.appendChild(p);
      }
 } 

This is the section with the <p>

    <form action="ClientServlet" method ="post"  onclick="onTechIdChange();"></form>
         <div id="uploadSuggest" class="center-block">
         <div id="suggestHeading" class="row">
                    <h4 class="textTitle center-block"> Messages  </h4>
                </div>      
                    <div class="row">
                    <div class="col-md-8">
           <a  id="btn_padding" href="#download" class="btn btn-image pull-left" onclick="onTechIdChange();">Create Profile</a>
           <p>   </p>
                    </div>
                    </div>

    </div>
      </div>  
    </section>

如果有人知道有关此问题的教程,您可以上传链接吗?

1 个答案:

答案 0 :(得分:0)

我认为你没有调用将值添加到DOM的显示功能。

  for(name in details){
   display(name)
  }

此外,我不确定您的解析器是否正确

  success : function(result) {
            var details = result[0];  -- Extract the first value of response
            var name;
            for(name in details)  --- should be an array as well
                {
                dispatchEvent(event) -- I think here you need to call print function
                }
            alert(details.sender); -- If details.sender have the details probably the 
                                       iteration should be for(name in details.sender)
       },

我也重新考虑$.ajax的使用,可能使用$.get更好,而且,使用jsp我认为不需要使用带有javascript的简单html。