2016-08-07 16 views
1

私は非常に大きなデータ量のリストを含むフラグメントを持っているため、現在大きな問題に直面しています。どのような私が今までやっていることは以下の通りです:春のバックエンドでSpring、Thymeleaf、および非同期データ読み込み

function loadEvaluations() { 

    $.ajax({ 
     url : "/evaluation/data", 
     type : "POST", 
     headers : createAuthorizationTokenHeader(), 
     async : !1, 
     data : { 
      from: rangeFrom, 
      to: rangeTo 
     }, 
     success : function(data) { 
      $("#portal_container").html(data); 
     }, 
     error : function(data) { 
      $("#portal_container").html(data); 
     } 
    }); 
} 

私は、データベースからデータを選択し、内側にそれを置く:ユーザーが、私はこのようなjavascriptのポストを実行]ボタンをクリックする

これは、次のようなフラグメントに提示なっている

@RequestMapping(HelperUrls.URL_WEB_EVALUATION_DATA) 
public String data(Model model, HttpServletRequest request, 
     @RequestParam(value = Params.PARAM_FROM, required = true) long from, 
     @RequestParam(value = Params.PARAM_TO, required = true) long to) { 

    final IDM_USER user = this.idm_user_repository.findByEmail(request.getUserPrincipal().getName()); 

    if (user != null) { 
     final int unit = user.getUnit(); 
     final Locale locale = LocaleContextHolder.getLocale(); 
     final String unitValue = HelperShortcuts.getUnitForShortcut(this.messageSource, locale, unit); 

     Set<IDM_BREAD> breads = this.idm_bread_repository.findByDatesBetweenAndUser(new Date(from), new Date(to), user); 
     Set<IDM_FOOD> foods = this.idm_food_repository.findByDatesBetweenAndUser(new Date(from), new Date(to), user); 
     Set<IDM_INSULIN> insulins = this.idm_insulin_repository.findByDatesBetweenAndUser(new Date(from), new Date(to), user); 
     Set<IDM_MEASURE> measures = this.idm_measure_repository.findByDatesBetweenAndUser(new Date(from), new Date(to), user); 
     Set<IDM_MOOD> moods = this.idm_mood_repository.findByDatesBetweenAndUser(new Date(from), new Date(to), user); 
     Set<IDM_NOTE> notes = this.idm_note_repository.findByDatesBetweenAndUser(new Date(from), new Date(to), user); 
     Set<IDM_SPORT> sports = this.idm_sport_repository.findByDatesBetweenAndUser(new Date(from), new Date(to), user); 

     List<DatatransferListEntry> entries = new ArrayList<>(); 

     entries.addAll(breads); 
     entries.addAll(foods); 
     entries.addAll(insulins); 
     entries.addAll(measures); 
     entries.addAll(moods); 
     entries.addAll(notes); 
     entries.addAll(sports); 
     entries = this.initHeaders(entries, locale); 

     model.addAttribute(ReturnKeys.TIME, Helper.getDateTimePatternEvaluation(this.messageSource, locale, from, to)); 
     model.addAttribute(ReturnKeys.ENTRIES, entries); 
     model.addAttribute(ReturnKeys.USER_UNIT_VALUE, unitValue); 
    } 

    return Htmls.WEB_FRAGMENT_EVALUATION_DATA_F; 
} 

<!DOCTYPE html> 
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"> 
<body> 
    <div th:fragment="fragment_evaluations_data" class="margin-top-100 margin-bottom-100"> 

     <div th:each="entry: ${ENTRIES}"> 
      <div class="container page-small page-small-header" th:if="${entry.type == 0}"> 
       <div class="row page-row-evaluation"> 
        <span th:text="${entry.timestampLabel}"></span> 
       </div> 

       <div class="row page-row-evaluation"></div> 
      </div> 

      <div class="container page-small page-small-header-sub" th:if="${entry.type == 1}"> 
       <div class="row page-row-evaluation"> 
        <span th:text="${entry.timestampLabel}"></span> 
       </div> 

       <div class="row page-row-evaluation"></div> 
      </div> 

      <div class="container page-small" th:if="${entry.type == 2}"> 
       <div class="row page-row-evaluation"> 
        <div class="col-md-12"> 
         <span class="evaluation-font" th:text="${entry.value} + ' ' + ${USER_UNIT_VALUE}"></span> 
        </div> 
        <div class="col-md-12 margin-top-5"> 
         <span th:text="${entry.timestampLabel} + ' '"></span> 
         <span th:if="${entry.mealtime == 0}" th:text="'- ' + #{label.meal_before}"></span> 
         <span th:if="${entry.mealtime == 1}" th:text="'- ' + #{label.meal_after}"></span> 
        </div> 
       </div> 

       <div class="row page-row-evaluation"></div> 
      </div> 

      <div class="container page-small" th:if="${entry.type == 3}"> 
       <div class="row page-row-evaluation"> 
        <div class="col-md-12"> 
         <span class="evaluation-font" th:text="${entry.units} + ' ' + #{label.insulin_units}"></span> 
        </div> 
        <div class="col-md-12 margin-top-5"> 
         <span th:text="${entry.timestampLabel}"></span> 
        </div> 
       </div> 

       <div class="row page-row-evaluation"></div> 
      </div> 

      <div class="container page-small" th:if="${entry.type == 4}"> 
       <div class="row page-row-evaluation"> 
        <div class="col-md-12"> 
         <span class="evaluation-font" th:text="${entry.units} + ' ' + #{label.bread}"></span> 
        </div> 
        <div class="col-md-12 margin-top-5"> 
         <span th:text="${entry.timestampLabel}"></span> 
        </div> 
       </div> 

       <div class="row page-row-evaluation"></div> 
      </div> 

      <div class="container page-small" th:if="${entry.type == 5}"> 
       <div class="row page-row-evaluation"> 
        <div class="col-md-12"> 
         <span class="evaluation-font" th:text="${entry.sporttype.sporttype}"></span> 
        </div> 
        <div class="col-md-12 margin-top-5"> 
         <span th:if="${entry.intensity == 0}" th:text="#{label.intensity_easy}"></span> 
         <span th:if="${entry.intensity == 1}" th:text="#{label.intensity_moderate}"></span> 
         <span th:if="${entry.intensity == 2}" th:text="#{label.intensity_hard}"></span> 
         <span th:if="${entry.intensity == 3}" th:text="#{label.intensity_very_hard}"></span> 
        </div> 
        <div class="col-md-12 margin-top-5"> 
         <span th:text="${entry.duration} + ' ' + #{label.minutes}"></span> 
        </div> 
        <div class="col-md-12 margin-top-5"> 
         <span th:text="${entry.timestampLabel}"></span> 
        </div> 
       </div> 

       <div class="row page-row-evaluation"></div> 
      </div> 

      <div class="container page-small" th:if="${entry.type == 6}"> 
       <script> 
        function loadFoodImage(id) { 
         $.ajax({ 
          url : "/rest/evaluation/foodiamgeid", 
          type : "POST", 
          headers : createAuthorizationTokenHeader(), 
          async : 1, 
          data : { 
           image_id: id 
          }, 
          success : function(data) { 
           var image = JSON.parse(data).USER_IMAGE; 

           if (image != null) { 
            var selector = "#evaluation_food_image_" + id; 

            $(selector).attr("src", image); 
           } 
          }, 
          error : function(data) { 

          } 
         }); 
        } 
       </script> 

       <div class="row page-row-evaluation"> 
        <div class="col-md-12"> 
         <span class="evaluation-font" th:text="#{label.food}"></span> 
        </div> 
        <div class="col-md-12 margin-top-5"> 
         <span th:text="${entry.timestampLabel}"></span> 
        </div> 
        <div class="col-md-12 margin-top-5"> 
         <img th:id="'evaluation_food_image_' + ${entry.imageId}" src="/img/ic_rolling.gif" class="center-block img-responsiv image-upload-image" th:onload="|loadFoodImage('${entry.imageId}')|" /> 
        </div> 
       </div> 

       <div class="row page-row-evaluation"></div> 
      </div> 

      <div class="container page-small" th:if="${entry.type == 7}"> 
       <div class="row page-row-evaluation"> 
        <div class="col-md-12"> 
         <span class="evaluation-font" th:text="#{label.note}"></span> 
        </div> 
        <div class="col-md-12 margin-top-5"> 
         <span th:text="${entry.timestampLabel}"></span> 
        </div> 
        <div class="col-md-12 margin-top-5"> 
         <span th:text="${entry.note}"></span> 
        </div> 
       </div> 

       <div class="row page-row-evaluation"></div> 
      </div> 

      <div class="container page-small" th:if="${entry.type == 8}"> 
       <div class="row page-row-evaluation"> 
        <div class="col-md-12"> 
         <span class="evaluation-font" th:text="#{label.mood}"></span> 
        </div> 
        <div class="col-md-12 margin-top-5"> 
         <span th:text="${entry.timestampLabel}"></span> 
        </div> 
        <div class="col-md-12 text-center"> 
         <img th:if="${entry.mood == 1}" class="image-mood" src="/img/ic_mood_very_bad_red.png"></img> 
         <img th:if="${entry.mood == 2}" class="image-mood" src="/img/ic_mood_bad_orange.png"></img> 
         <img th:if="${entry.mood == 3}" class="image-mood" src="/img/ic_mood_neutral_yellow.png"></img> 
         <img th:if="${entry.mood == 4}" class="image-mood" src="/img/ic_mood_good_green.png"></img> 
         <img th:if="${entry.mood == 5}" class="image-mood" src="/img/ic_mood_very_good_green.png"></img> 
        </div> 
       </div> 

       <div class="row page-row-evaluation"></div> 
      </div> 
     </div> 

    </div> 
</body> 
</html> 
フラグメントをモデルと返します

問題

この方法データのロードロードはラウンド10秒程度とUIフリーズを取り、ユーザーが何かをすることはできませんので、非常に悪いユーザーエクスペリエンスです。彼は古いページにとどまり、10秒間待たなければならず、その後新しい断片にナビゲートされます。

私は

を必要と私は非同期にデータをロードするthymeleaf /春のソリューションを必要としています。だから私は彼がリンクをクリックした後、ユーザーが新しいページimmediatleyを見て、データがサーバーから収集されている間、何らかの種類のアニメーションを表示するようになります。サーバーが終了すると、ビューは自動的に更新されます。

これは可能ですか?

答えて

1

次の2つの別々のコントローラのボタンが他のページにクリックされたときに、それだけでページを表示するには、URLを呼び出すように持つことができます。ページのロードがあなたがオンロード機能を持たせることができるときに

@RequestMapping(HelperUrls.URL_WEB_EVALUATION) 
public String page(Model mode, HttpServletRequest request){ 
    model.addAttribute(ReturnKeys.TIME, new Date()); 
    model.addAttribute(ReturnKeys.ENTRIES, new ArrayList<>()); 
    model.addAttribute(ReturnKeys.USER_UNIT_VALUE, ""); 
    return Htmls.WEB_FRAGMENT_EVALUATION_DATA_F; 
} 

@RequestMapping(HelperUrls.URL_WEB_EVALUATION_DATA) 
public String data(Model model, HttpServletRequest request, 
     @RequestParam(value = Params.PARAM_FROM, required = true) long from, 
     @RequestParam(value = Params.PARAM_TO, required = true) long to) { 

    final IDM_USER user = this.idm_user_repository.findByEmail(request.getUserPrincipal().getName()); 

    if (user != null) { 
     final int unit = user.getUnit(); 
     final Locale locale = LocaleContextHolder.getLocale(); 
     final String unitValue = HelperShortcuts.getUnitForShortcut(this.messageSource, locale, unit); 

     Set<IDM_BREAD> breads = this.idm_bread_repository.findByDatesBetweenAndUser(new Date(from), new Date(to), user); 
     Set<IDM_FOOD> foods = this.idm_food_repository.findByDatesBetweenAndUser(new Date(from), new Date(to), user); 
     Set<IDM_INSULIN> insulins = this.idm_insulin_repository.findByDatesBetweenAndUser(new Date(from), new Date(to), user); 
     Set<IDM_MEASURE> measures = this.idm_measure_repository.findByDatesBetweenAndUser(new Date(from), new Date(to), user); 
     Set<IDM_MOOD> moods = this.idm_mood_repository.findByDatesBetweenAndUser(new Date(from), new Date(to), user); 
     Set<IDM_NOTE> notes = this.idm_note_repository.findByDatesBetweenAndUser(new Date(from), new Date(to), user); 
     Set<IDM_SPORT> sports = this.idm_sport_repository.findByDatesBetweenAndUser(new Date(from), new Date(to), user); 

     List<DatatransferListEntry> entries = new ArrayList<>(); 

     entries.addAll(breads); 
     entries.addAll(foods); 
     entries.addAll(insulins); 
     entries.addAll(measures); 
     entries.addAll(moods); 
     entries.addAll(notes); 
     entries.addAll(sports); 
     entries = this.initHeaders(entries, locale); 

     model.addAttribute(ReturnKeys.TIME, Helper.getDateTimePatternEvaluation(this.messageSource, locale, from, to)); 
     model.addAttribute(ReturnKeys.ENTRIES, entries); 
     model.addAttribute(ReturnKeys.USER_UNIT_VALUE, unitValue); 
    } 

    return Htmls.WEB_FRAGMENT_EVALUATION_DATA_F; 
} 

をbodyタグやどこかのJavaScript関数を呼び出すと、どこかのページでアニメーション画像を表示し、AJAX呼び出しは、このように戻ったときにそれを隠すページで:

<!DOCTYPE html> 
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"> 
<body onload="loadEvaluations()"> 
    <img src="animation-image.jpg" style="display:none" id="animationImage"/> 
    <div th:fragment="fragment_evaluations_data" class="margin-top-100 margin-bottom-100"> 

     <div th:each="entry: ${ENTRIES}"> 
      <div class="container page-small page-small-header" th:if="${entry.type == 0}"> 
       <div class="row page-row-evaluation"> 
        <span th:text="${entry.timestampLabel}"></span> 
       </div> 

       <div class="row page-row-evaluation"></div> 
      </div> 

      <div class="container page-small page-small-header-sub" th:if="${entry.type == 1}"> 
       <div class="row page-row-evaluation"> 
        <span th:text="${entry.timestampLabel}"></span> 
       </div> 

       <div class="row page-row-evaluation"></div> 
      </div> 

      <div class="container page-small" th:if="${entry.type == 2}"> 
       <div class="row page-row-evaluation"> 
        <div class="col-md-12"> 
         <span class="evaluation-font" th:text="${entry.value} + ' ' + ${USER_UNIT_VALUE}"></span> 
        </div> 
        <div class="col-md-12 margin-top-5"> 
         <span th:text="${entry.timestampLabel} + ' '"></span> 
         <span th:if="${entry.mealtime == 0}" th:text="'- ' + #{label.meal_before}"></span> 
         <span th:if="${entry.mealtime == 1}" th:text="'- ' + #{label.meal_after}"></span> 
        </div> 
       </div> 

       <div class="row page-row-evaluation"></div> 
      </div> 

      <div class="container page-small" th:if="${entry.type == 3}"> 
       <div class="row page-row-evaluation"> 
        <div class="col-md-12"> 
         <span class="evaluation-font" th:text="${entry.units} + ' ' + #{label.insulin_units}"></span> 
        </div> 
        <div class="col-md-12 margin-top-5"> 
         <span th:text="${entry.timestampLabel}"></span> 
        </div> 
       </div> 

       <div class="row page-row-evaluation"></div> 
      </div> 

      <div class="container page-small" th:if="${entry.type == 4}"> 
       <div class="row page-row-evaluation"> 
        <div class="col-md-12"> 
         <span class="evaluation-font" th:text="${entry.units} + ' ' + #{label.bread}"></span> 
        </div> 
        <div class="col-md-12 margin-top-5"> 
         <span th:text="${entry.timestampLabel}"></span> 
        </div> 
       </div> 

       <div class="row page-row-evaluation"></div> 
      </div> 

      <div class="container page-small" th:if="${entry.type == 5}"> 
       <div class="row page-row-evaluation"> 
        <div class="col-md-12"> 
         <span class="evaluation-font" th:text="${entry.sporttype.sporttype}"></span> 
        </div> 
        <div class="col-md-12 margin-top-5"> 
         <span th:if="${entry.intensity == 0}" th:text="#{label.intensity_easy}"></span> 
         <span th:if="${entry.intensity == 1}" th:text="#{label.intensity_moderate}"></span> 
         <span th:if="${entry.intensity == 2}" th:text="#{label.intensity_hard}"></span> 
         <span th:if="${entry.intensity == 3}" th:text="#{label.intensity_very_hard}"></span> 
        </div> 
        <div class="col-md-12 margin-top-5"> 
         <span th:text="${entry.duration} + ' ' + #{label.minutes}"></span> 
        </div> 
        <div class="col-md-12 margin-top-5"> 
         <span th:text="${entry.timestampLabel}"></span> 
        </div> 
       </div> 

       <div class="row page-row-evaluation"></div> 
      </div> 

      <div class="container page-small" th:if="${entry.type == 6}"> 
       <script> 
        function loadFoodImage(id) { 
         $.ajax({ 
          url : "/rest/evaluation/foodiamgeid", 
          type : "POST", 
          headers : createAuthorizationTokenHeader(), 
          async : 1, 
          data : { 
           image_id: id 
          }, 
          success : function(data) { 
           var image = JSON.parse(data).USER_IMAGE; 

           if (image != null) { 
            var selector = "#evaluation_food_image_" + id; 

            $(selector).attr("src", image); 
           } 
          }, 
          error : function(data) { 

          } 
         }); 
        } 
       </script> 

       <div class="row page-row-evaluation"> 
        <div class="col-md-12"> 
         <span class="evaluation-font" th:text="#{label.food}"></span> 
        </div> 
        <div class="col-md-12 margin-top-5"> 
         <span th:text="${entry.timestampLabel}"></span> 
        </div> 
        <div class="col-md-12 margin-top-5"> 
         <img th:id="'evaluation_food_image_' + ${entry.imageId}" src="/img/ic_rolling.gif" class="center-block img-responsiv image-upload-image" th:onload="|loadFoodImage('${entry.imageId}')|" /> 
        </div> 
       </div> 

       <div class="row page-row-evaluation"></div> 
      </div> 

      <div class="container page-small" th:if="${entry.type == 7}"> 
       <div class="row page-row-evaluation"> 
        <div class="col-md-12"> 
         <span class="evaluation-font" th:text="#{label.note}"></span> 
        </div> 
        <div class="col-md-12 margin-top-5"> 
         <span th:text="${entry.timestampLabel}"></span> 
        </div> 
        <div class="col-md-12 margin-top-5"> 
         <span th:text="${entry.note}"></span> 
        </div> 
       </div> 

       <div class="row page-row-evaluation"></div> 
      </div> 

      <div class="container page-small" th:if="${entry.type == 8}"> 
       <div class="row page-row-evaluation"> 
        <div class="col-md-12"> 
         <span class="evaluation-font" th:text="#{label.mood}"></span> 
        </div> 
        <div class="col-md-12 margin-top-5"> 
         <span th:text="${entry.timestampLabel}"></span> 
        </div> 
        <div class="col-md-12 text-center"> 
         <img th:if="${entry.mood == 1}" class="image-mood" src="/img/ic_mood_very_bad_red.png"></img> 
         <img th:if="${entry.mood == 2}" class="image-mood" src="/img/ic_mood_bad_orange.png"></img> 
         <img th:if="${entry.mood == 3}" class="image-mood" src="/img/ic_mood_neutral_yellow.png"></img> 
         <img th:if="${entry.mood == 4}" class="image-mood" src="/img/ic_mood_good_green.png"></img> 
         <img th:if="${entry.mood == 5}" class="image-mood" src="/img/ic_mood_very_good_green.png"></img> 
        </div> 
       </div> 

       <div class="row page-row-evaluation"></div> 
      </div> 
     </div> 

    </div> 
</body> 
</html> 

、あなたのJavascriptを行うことができます。

function loadEvaluations() { 
    $('#animationImage').show(); 
    $.ajax({ 
     url : "/evaluation/data", 
     type : "POST", 
     headers : createAuthorizationTokenHeader(), 
     async : !1, 
     data : { 
      from: rangeFrom, 
      to: rangeTo 
     }, 
     success : function(data) { 
      $('#animationImage').hide(); 
      $("#portal_container").html(data); 
     }, 
     error : function(data) { 
      $('#animationImage').hide(); 
      $("#portal_container").html(data); 
     } 
    }); 
} 
+0

これは動作する解決策です。 'body'タグの' onLoad'で 'loadEvaluations()'を呼び出すことはできません。なぜなら、呼び出されることはないからです。しかし、最初の 'loadEvaluations'成功の後に呼び出すことができます。ありがとうございました。 – Mulgard

+0

@Mulgard that okk .. uwc – Sohlowmawn

関連する問題