2017-08-05 219 views
0

我對Spring引導和ThyemeLeaf很新,但仍然在學習它,所以也許我在Internet上沒有正確地搜索它,但我沒有在任何地方看到這樣的問題。Thymeleaf HTML頁面的表格在刷新頁面後停止工作

這裏是關於我在做什麼規格:

1)我使用的春天開機,與ThyemeLeaf加載一個寺廟。

2)我從一個DBF文件

3)在HTML頁面中提取數據,我只加載每個行和表中的元素。

問題: 重新部署整個應用程序後,頁面正常工作,它加載一切都很好。 當我刷新頁面表不加載。

這是thymeleaf或我的HTML或Spring的問題嗎?

這是正在運行的代碼。

什麼從DBF

@Component("DBFInterface") 
public class DBFInterface { 
    private String fileName; 
    private DBFReader reader; 

    //DBF reader is from a library to work with .dbf files, 
    // I have tested it in pure java, it works fine 
    // https://github.com/albfernandez/javadbf 
    public DBFInterface(String fileName) { 
     // location of the .dbf file 
     this.fileName = fileName == null? DATA.DATA_FILE_NAME : fileName;; 
     init(); 
    } 
    public DBFInterface(){ 
     fileName = DATA.DATA_FILE_NAME; 
     init(); 
    } 

    // starting the "reader" with lets me interface with the database 
    private void init(){ 
     try { 
      reader = new DBFReader(new FileInputStream(fileName)); 
     } catch (FileNotFoundException e) { 
      e.printStackTrace(); 
     } 
    } 


    public List<DBFField> getFieldList(){ 
     int count = reader.getFieldCount(); 
     List<DBFField> list = new ArrayList<>(count); 

     for(int i = 0; i < count; i ++) list.add(reader.getField(i)); 
     return list; 
    } 

    // all of the records in the database, 
    // each column in an element in the Object array 
    public List<Object[]> getRawData(){ 
     int count = reader.getRecordCount(); 
     List<Object[]> list = new ArrayList<>(count); 
     Object[] rowObjects; 
     while ((rowObjects = reader.nextRecord()) != null) { 
      list.add(rowObjects); 
     } 
     return list; 
    } 

    // getters and setters 

控制器的一部分,它讓HTML訪問數據

@RequestMapping(method = RequestMethod.GET) 
    public String getSimpleTable(Model model){ 
     List<Object[]> l = dbfInterface.getRawData(); 
     model.addAttribute("rawData",l); 
     model.addAttribute("tableFields",dbfInterface.getFieldList()); 
     if(l.size() > 0) System.out.println("RAW DATA NOT EMPTY"); 
     return "simple_table"; 
    } 

HTML的一部分是加載數據拉數據(我會使它成爲動態列以後,當我可以先解決這個問題)

  <tr th:each="data:${rawData}"> 
       <td th:text="${data[0]}"></td> 
       <td th:text="${data[1]}"></td> 
       <td th:text="${data[2]}"></td> 
       <td th:text="${data[3]}"></td> 
       <td th:text="${data[4]}"></td> 
       <td th:text="${data[5]}"></td> 
       <td th:text="${data[6]}"></td> 
       <td th:text="${data[7]}"></td> 
       <td th:text="${data[8]}"></td> 
       <td th:text="${data[9]}"></td> 
       <td th:text="${data[10]}"></td> 
      </tr> 

我試過切換Thymeleaf的緩存以及HTML的擺弄,事情是我不知道問題出在哪裏。

我可以告訴你,刷新不會再執行這個位,它是在我第一次加載頁面時執行的。

2017-08-05 16:09:43.837 INFO 10409 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]  : Initializing Spring FrameworkServlet 'dispatcherServlet' 
2017-08-05 16:09:43.838 INFO 10409 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet  : FrameworkServlet 'dispatcherServlet': initialization started 
2017-08-05 16:09:43.860 INFO 10409 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet  : FrameworkServlet 'dispatcherServlet': initialization completed in 22 ms 
RAW DATA NOT EMPTY 

任何建議?或資源的指導,我可以看看自己? 同樣,它在我重新部署後第一次加載時正常工作,但在刷新頁面後表不會加載。

編輯

一些更多的東西,我發現,當我在Chrome瀏覽器的開發者模式,張貼的HTML位被註釋掉。第一次不是刷新後,邏輯被註釋掉了?

回答

0

根本原因分析

事實:

  1. Component註釋被使用而無需Scope註釋,所以DBFInterface類的實例的生命週期是單元素。

  2. DBFReader字段僅由構造函數初始化一次,讀者使用該流。

結論是,第一個getRawData()方法調用工作得很好。在第一次調用之後,該流似乎達到了結尾:在後續的方法調用getRawData()中不會有記錄可用。

解決方案

考慮更新getRawData()方法的實現。替代品:

  1. 在每個方法調用中,在getRawData()方法中創建讀取器(流)。所以,讀者變成了一個局部變量。
  2. 將讀取器(流)倒回到開頭。多線程時需要閱讀器狀態同步。

第一個選擇似乎比第二個選擇更簡單,更直接。

此外,請確保閱讀器(其流)適當關閉。

0
  1. @Component("DBFInterface")註釋僅創建DBFInterface的一個實例。
  2. 第一次加載頁面while ((rowObjects = reader.nextRecord()) != null) { retreives數據,因爲netxRecord()不爲空,但第二次加載頁面沒有更多的行要讀取,因爲是第一次相同reader對象。

一個可能的解決方案是:

  1. 轉到DBFInterface
  2. 轉到DBFInterface構造和刪除init()方法調用從它

像這樣:

public DBFInterface(){ 
     fileName = DATA.DATA_FILE_NAME; 
    } 
  • 更改init方法公開
  • init()方法

    public void init(){ 
         try { 
          reader = new DBFReader(new FileInputStream(fileName)); 
         } catch (FileNotFoundException e) { 
          e.printStackTrace(); 
         } 
        } 
    
  • 轉到getSimpleTable方法和之前調用init()方法getRawData
  • 像這樣:

    @RequestMapping(method = RequestMethod.GET) 
         public String getSimpleTable(Model model){ 
          dbfInterface.init(); 
          List<Object[]> l = dbfInterface.getRawData(); 
          model.addAttribute("rawData",l); 
          model.addAttribute("tableFields",dbfInterface.getFieldList()); 
          if(l.size() > 0) System.out.println("RAW DATA NOT EMPTY"); 
          return "simple_table"; 
         } 
    

    這種替代方法的缺點是,每一個請求你必須從頭閱讀文件。

    也請嘗試在某個時候調用DBFUtils.close(reader);,也許如果您在DBFInterface這樣創建close()方法:

    public void close(){ 
         try { 
          DBFUtils.close(reader); 
         } catch (Exception e) { 
          e.printStackTrace(); 
         } 
        } 
    

    ,並調用它,當你完成讀取數據。