如何在 Spring MVC 中每 3 秒更新一次 Sparkline Graph(實時更新)
已發表: 2013-06-17jQuery Sparkline:這個 jQuery 插件可以很容易地直接在瀏覽器中生成許多不同類型的迷你圖,在線使用 HTML 和 Javascript 的一行。
該插件除了 jQuery 之外沒有其他依賴項,並且適用於所有現代瀏覽器
現在讓我們在 Spring MVC 架構中為您的實時 Web 應用程序使用這個庫。 以下是我們將在此處執行的操作的快速流程:
- 在 Spring MVC 中,我的控制器每 3 秒獲取一次數據並將其發送到 .jsp 文件(視圖)。
- 查看 (.jsp) 文件,通過 AJAX 調用每 3 秒獲取一次 JSONArray 數據
- AJAX 調用將數據發送到 consumeJSONData(jsonArray) 函數
- consumeJSONData 每 3 秒更新一次 Sparkline
讓我們開始編碼:
第1步
Pre-Requisite:
https://crunchify.com/hello-world-example-spring-mvc-3-2-1/(在Tomcat上成功部署此項目)
您需要以下額外json.jar
Maven 依賴項。 打開 pom.xml 文件並添加以下依賴項。
1 2 3 4 5 |
< dependency > < groupId > org . json < / groupId > < artifactId > json < / artifactId > < version > 20150729 < / version > < / dependency > |
第2步
在com.crunchify.controller
包下創建文件CrunchifySpringSparklineConsumeJSONArray
.java。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
package com . crunchify . controller ; import java . util . Random ; import org . json . JSONArray ; import org . json . JSONException ; import org . json . JSONObject ; import org . springframework . stereotype . Controller ; import org . springframework . web . bind . annotation . RequestMapping ; import org . springframework . web . bind . annotation . RequestMethod ; import org . springframework . web . bind . annotation . ResponseBody ; import org . springframework . web . servlet . ModelAndView ; /** * @author Crunchify.com * */ @Controller public class CrunchifySpringSparklineConsumeJSONArray { @RequestMapping ( "/sparkline" ) public ModelAndView crunchifySparklineTest ( ) { return new ModelAndView ( "sparkline" , "message" , "Sparkline.js Example which accepts JSONArray value every 3 seconds & updates graphs.." ) ; } @RequestMapping ( value = "/sparklinetest" , method = RequestMethod . GET ) public @ResponseBody String constructJSONArray ( ) throws JSONException { JSONObject one = new JSONObject ( ) ; JSONObject two = new JSONObject ( ) ; JSONObject three = new JSONObject ( ) ; JSONArray result = new JSONArray ( ) ; Random r = new Random ( ) ; int [ ] r1 = { r . nextInt ( 100 ) , r . nextInt ( 100 ) , r . nextInt ( 100 ) , r . nextInt ( 100 ) , r . nextInt ( 100 ) , r . nextInt ( 100 ) , r . nextInt ( 100 ) , r . nextInt ( 100 ) } ; int [ ] r2 = { r . nextInt ( 100 ) , r . nextInt ( 100 ) , r . nextInt ( 100 ) , r . nextInt ( 100 ) , r . nextInt ( 100 ) , r . nextInt ( 100 ) , r . nextInt ( 100 ) , r . nextInt ( 100 ) } ; int [ ] r3 = { r . nextInt ( 100 ) , r . nextInt ( 100 ) , r . nextInt ( 100 ) , r . nextInt ( 100 ) , r . nextInt ( 100 ) , r . nextInt ( 100 ) , r . nextInt ( 100 ) , r . nextInt ( 100 ) } ; one . put ( "one" , r1 ) ; two . put ( "two" , r2 ) ; three . put ( "three" , r3 ) ; result . put ( one ) ; result . put ( two ) ; result . put ( three ) ; JSONObject jsonObj = new JSONObject ( ) ; jsonObj . put ( "sparkData" , result ) ; System . out . println ( "Sendig this data to view (sparkline.jsp): " + jsonObj . toString ( ) ) ; return jsonObj . toString ( ) ; } } |
第三步
在文件夾/WebContent/WEB-INF/jsp
文件夾下創建文件sparkline.jsp
。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
< html > < head > < TITLE > Crunchify - Sparkline . js Example which accepts JSONArray value every 3 seconds < / TITLE > <style type ="text/css"> body { background-image : url ( 'https://cdn.crunchify.com/wp-content/uploads/2013/03/Crunchify.bg_.300.png' ) ; } </style> <script type = "text/javascript" src = "http://code.jquery.com/jquery-1.10.1.min.js" > </script> <script src = "https://cdn.crunchify.com/wp-content/uploads/code/jquery.sparkline.js" > </script> <script type = "text/javascript" > function crunchifySparkline ( ) { $ . ajax ( { url : 'sparklinetest.html' , dataType : "json" , cache : false , contentType : 'application/json; charset=utf-8' , type : 'GET' , success : function ( result ) { var one = result . sparkData ; //alert(one); consumeJSONData ( one ) ; } } ) ; } function consumeJSONData ( sparkData ) { console . log ( sparkData ) ; for ( var i = 0 ; i < sparkData . length ; i ++ ) { var obj = sparkData [ i ] ; var crunchifyName ; var crunchifyValue ; for ( var key in obj ) { crunchifyName = key ; crunchifyValue = obj [ key ] . toString ( ) ; crunchifyValue = crunchifyValue . substr ( 1 ) ; crunchifyValue = crunchifyValue . substring ( 0 , crunchifyValue . length - 1 ) ; var n = crunchifyValue . split ( "," ) ; console . log ( n ) ; $ ( "#" + crunchifyName ) . sparkline ( n , { type : 'line' , width : '80' , fillColor : '#eeeeee' } ) ; } } } </script> <script type = "text/javascript" > var intervalId = 0 ; intervalId = setInterval ( crunchifySparkline , 3000 ) ; </script> < / head > < body > < div align = "center" > < br > < br > $ { message } < br > < br > < div id = "result" > < / div > < br > < br > Sparkline One : < span id = "one" > . < / span > < br > < br > Sparkline Two : < span id = "two" > . < / span > < br > < br > Sparkline Three : < span id = "three" > . < / span > < br > < br > < br > < br > < div style = "font-family: verdana; line-height: 25px; padding: 5px 10px; border-radius: 10px; border: 3px solid #EE872A; width: 50%; font-size: 12px;" > Sparkline . js Example which accepts JSONArray value every 3 seconds by < a href = 'https://crunchify.com' > Crunchify < / a > . Click < a href = 'https://crunchify.com/category/java-tutorials/' > here < / a > for all Java , Spring MVC , Web Development examples . < br > < / div > < / div > < / body > < / html > |
另一個必須閱讀:

- 簡單的 Java 枚舉示例
- Java MailAPI 示例 – 通過 GMail SMTP 發送電子郵件
- Java中的守護線程是什麼? 附上示例
第四步
簽出此目錄結構。
第 5 步
再次將CrunchifySpringMVCTutorial
項目重新部署到 Apache Tomcat 服務器中。
- 轉到 Eclipse 中的
Server
選項卡 - 點擊
Clean...
- 點擊
Publish
- 點擊
Restart
第 6 步
打開 Web 瀏覽器並訪問此 URL 以查看結果:http://localhost:8080/CrunchifySpringMVCTutorial/sparkline.html
如何驗證結果並檢查完整流程?
第1步
注意你的結果。它應該和你在本頁頂部看到的一樣。
第2步
檢查 Eclipse 控制台輸出。 您將每 3 秒看到一次帶有數據的控制台日誌。 這意味著 Spring MVC 控制器每 3 秒向 JSP 頁面發送數據。
第三步
結帳瀏覽器控制台以查看實時數據。
- 右鍵單擊頁面以選擇
Inspect Element
- 選擇
console
選項卡