2016-09-17 2 views
0

これに関連するスレッドがいくつか見つかりましたが、ほとんどのエラーは不正な命名によるものと思われますが、@ModelAttributeを使って正しく実行していると思います。検証は認識され、メッセージ表示のほかにすべて正常に動作します。ここでThymeleafは検証メッセージをレンダリングしません

は私のコントローラである:ここでは

  @GetMapping("/search") 
      public String searchPage(Model model, @ModelAttribute("searchFormBacking") SearchParamModel search) { 
       if (!model.containsAttribute("searchFormBacking")) { 
        model.addAttribute("searchFormBacking", new SearchParamModel()); 
       } else { 
        model.addAttribute("searchFormBacking", search); 
       } 
       return "search"; 
      } 

      @PostMapping("/results") 
      @SuppressWarnings("unchecked") 
      public String resultSubmit(@ModelAttribute("searchFormBacking") @Valid SearchParamModel search, BindingResult result, final RedirectAttributes redirectAttributes) throws Exception{ 

       if (result.hasErrors()) { 
        //flash errors bound to "searchFormBacking" 
        redirectAttributes.addFlashAttribute("searchFormBacking",search); 
        redirectAttributes.addFlashAttribute("org.springframework.validation.BindingResult.searchFormBacking",result); 
        return "redirect:/search"; 
       } 

       List<Object[]> queryList = GlobalMethods.baseQuery(); 

       //input into model&view 
       List<CrimeModel> crimeList = GlobalMethods.analyzeQuery(search.getSearchAddress(),search.getSearchDistance(),search.getSearchTime(), queryList); 
       List<CrimeRank> rankedList = GlobalMethods.distinctAsList(GlobalMethods.rankedMap(GlobalMethods.distinctCountMap(crimeList))); 

       redirectAttributes.addFlashAttribute("searchFormBacking",search); 
       redirectAttributes.addFlashAttribute("crimeModel", crimeList); 
       redirectAttributes.addFlashAttribute("rankedModel", rankedList); 


       return "redirect:/results"; 

      } 

は形式です:

 <!doctype html> 
     <html lang="en" xmlns:th="http://www.thymeleaf.org"> 
     <head> 
      <meta charset="UTF-8"/> 
      <meta name="viewport" 
        content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"/> 
      <meta http-equiv="X-UA-Compatible" content="ie=edge"/> 
      <title>Crime Tracker | Search</title> 
     </head> 
     <body> 
     <form th:action="@{/results}" th:object="${searchFormBacking}" method="post"> 

      <input type="text" th:field="*{searchAddress}" placeholder="Enter address."/> 
      <div class="error-message" th:if="${#fields.hasErrors('searchAddress')}" th:errors="*{searchAddress}"></div> 
      <br/> 

      <input type="text" th:field="*{searchDistance}" placeholder="Enter the distance to search."/> 
      <div class="error-message" th:if="${#fields.hasErrors('searchDistance')}" th:errors="*{searchDistance}"></div> 
      <br/> 

      <input type="text" th:field="*{searchTime}" placeholder="Time-span."/> 
      <div class="error-message" th:if="${#fields.hasErrors('searchTime')}" th:errors="*{searchTime}"></div> 
      <br/> 

      <input type="submit" value="Submit"/> 
      <br/> 
      <input type="reset" value="Reset"/> 
     </form> 
     </body> 
     </html> 

そして最後に、フォームのバッキングクラス:

  public class SearchParamModel { 

      @NotNull 
      @Size(min = 6, max = 40) 
      private String searchAddress; 

      @NotNull 
      private String searchDistance; 

      @NotNull 
      private String searchTime; 


      public String getSearchAddress() { 
       return searchAddress; 
      } 

      public void setSearchAddress(String searchAddress) { 
       this.searchAddress = searchAddress; 
      } 

      public String getSearchDistance() { 
       return searchDistance; 
      } 

      public void setSearchDistance(String searchDistance) { 
       this.searchDistance = searchDistance; 
      } 

      public String getSearchTime() { 
       return searchTime; 
      } 

      public void setSearchTime(String searchTime) { 
       this.searchTime = searchTime; 
      } 

      @Override 
      public boolean equals(Object o) { 
       if (this == o) return true; 
       if (o == null || getClass() != o.getClass()) return false; 

       SearchParamModel that = (SearchParamModel) o; 

       if (searchAddress != null ? !searchAddress.equals(that.searchAddress) : that.searchAddress != null) 
        return false; 
       if (searchDistance != null ? !searchDistance.equals(that.searchDistance) : that.searchDistance != null) 
        return false; 
       return searchTime != null ? searchTime.equals(that.searchTime) : that.searchTime == null; 

      } 

      @Override 
      public int hashCode() { 
       int result = searchAddress != null ? searchAddress.hashCode() : 0; 
       result = 31 * result + (searchDistance != null ? searchDistance.hashCode() : 0); 
       result = 31 * result + (searchTime != null ? searchTime.hashCode() : 0); 
       return result; 
      } 
     } 

人がいるように見えることを主なエラー@ModelAttributeを使用しない場合、デフォルト名はこの場合はsearchParamModelになります。さらに、/ search getマッピングのリダイレクトを処理して、新しいSearchParamModelがまだ存在しない場合にのみ作成するようにしました。これらは、検証メッセージを失う2つの最も一般的な理由であるように思われるので、私は何が間違っているのだろうかと思っています。

+0

ご迷惑をおかけして申し訳ございません。私はSpringとthymeleafチュートリアルの両方を見てきました。私のコードは他の人が見ているものとほとんど同じですので、私が間違っていることを実際に理解することはできません。 –

+0

私はウィルスミスのように "私は伝説です"と感じています。助けてくれる人は誰ですか? –

答えて

0

Passing BindingResult through RedirectionAttributes

検索のかなりの量の後、私はこのスレッドを発見し、あなたがflashAttributeをオーバーライドして、手動でGETメソッドでモデルに結合結果を置く下、近くの回避策を使用していました。私はまだこれをしないと、なぜこれが動作しないのか分かりません。また、@NotNullアノテーションはエラーを捕捉しませんでしたが、@ NotEmptyを使用するとうまくいきました。私の唯一の推測では、これらの不明瞭な問題は、Springのさまざまなバージョンやプロジェクトの設定などによって引き起こされているため、この問題が発生していれば、永遠に見つかるリンクを見つけることができます。

関連する問題