[英]Not able to display json data on jsp with ajax and struts2 integration
我無法在jsp上顯示json數據。 當我在瀏覽器中檢查控制台日志時,我能夠在控制台中看到json內容。 誰能用ajax代碼幫助我顯示數據?
Web.xml內容
<struts>
<constant name="struts.devMode" value="true" />
<package name="default" extends="json-default" namespace="/">
<action name="getCronDetails" class="myaction">
<result name="SUCCESS">/schedule.jsp</result>
</action>
</package>
</struts>
動作課內容:
List<emp> myList=new ArrayList<emp>() ;
//setter getter method for myList
public String getCronDetails (){
myList = fetchData() ;
return "SUCCESS";
}
我想以表格格式顯示emp類型的myList
。 有人可以幫我用Ajax代碼嗎?
我建議對ajax調用使用jQuery。 在html中,您需要一個元素來觸發調用,例如,我正在使用一個按鈕。
該按鈕應觸發調用該動作的js函數。 在該示例中,我添加了用於取消按鈕和顯示微調器的回調(由於微調器不存在,我給它們添加了注釋)。
您可以在response
變量中獲得ajax響應。 獲得響應后,您可以使用其內容更新DOM。
$(document).ready({ $("#getButton").click(function(event){ $.ajax({ type: "GET", url: "getCronDetails", dataType: "json", beforeSend: function(){ //Disable send button and start a spinner }, success: function(response, status, jqXHR){ //console.log("Ajax Success!"); if(typeof response !== 'object'){ response = JSON.parse(response); } //doSomethingWithThe(response); }, error: function(jqXHR, status, error){ //console.log('Ajax Error'); //showError('Ajax Error: ' + error ); }, complete: function(){ //Enable Submit Button and stop spinner } }); }); });
... <body> <!-- A button to trigger the ajax call --> <button id="getButton">Get Cron Details</button> <!-- Include jQuery --> <script src="https://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script> </body> ...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.