2017-12-11 2 views
1

これは私のHTMLフォームです:日は常にnull送られ

<form ui-jp="parsley" th:action="@{/users/created}" th:object="${userCreateDto}" method="post"> 
<div class="row m-b"> 
           <div class="col-sm-6"> 
            <div class="form-group"> 
             <label>User Valid From</label> 
             <input type='date' class="form-control" th:field="*{validFrom}" required/> 
            </div> 
           </div> 
           <div class="col-sm-6"> 
            <div class="form-group"> 
             <label>User Valid To</label> 
             <input type='date' class="form-control" th:field="*{validTill}" required/> 
            </div> 
           </div> 
          </div> 
</form> 

これはコントローラです:

@RequestMapping(value = "/created", method = RequestMethod.POST) 
    public String processCreateUsers(@ModelAttribute UserCreateDto userCreateDto, BindingResult error) { 
     log.info("Creating users with this input :"+ userCreateDto.toString()); 

     this.userService.createUser(userCreateDto); 
     return "redirect:/users"; 
    } 

そして、これは

public class UserCreateDto { 

    private String username; 

    private String firstName; 

    private String lastName; 

    private String password; 

    @DateTimeFormat(pattern = "yyyy-mm-dd") 
    private Date validFrom; 

    @DateTimeFormat(pattern = "yyyy-mm-dd") 
    private Date validTill; 
} 

ここDTOある日は、Javaで.sql.Date

また、インポートすると私はユーザーの配列を持ってJSONファイルを介して - 私はこれを正常に実行することができます。

ビューで実行すると、validFromvalidTillの値がnullになります。

また、送信をクリックした後にフォームデータをチェックすると、必要な入力がすべて正しく設定されます。 Dateパラメータが設定されているイベント。

私はBindingResultエラーを印刷するとき、これは次のとおりです。

Failed to convert property value of type 'java.lang.String' to required type 'java.sql.Date' for property 'validFrom'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [@org.springframework.format.annotation.DateTimeFormat java.sql.Date] for value '2017-12-10'; nested exception is org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [org.joda.time.DateTime] to type [@org.springframework.format.annotation.DateTimeFormat java.sql.Date] 

は、私がここで何をしないのですか?

注:java.util.Date works!

+0

'BindingResult error'を出力した場合、それは何と言いますか? – holmis83

+0

質問に追加されました。 – nirvair

答えて

0

フォームには、これらのパラメータをバインドできるBeanが必要です。簡略表記として@GetMapping(および@PostMapping)を使用できます。

@GetMapping("/theHtmlFormInYourQuestion") 
public String createUsers(Model model) { 

    model.addAttribute("userCreateDto", new UserCreateDto()); 
    return "theHtmlFormInYourQuestion"; 
} 

そして、それは(あなたがあなたの質問に投稿されたていない何かをやっている場合を除き)何をお使いのJavaコードのショーなので、フォームのアクションではなくusers/createdcreatedを使用する必要があります。

+0

私はコントローラの@RequestMapping( "users")を与えました。だから、私はそれでいいです。そして、はい、私はすでにこのために@GetMappingを持っています。それはあなたの答えに述べられている通りです。しかし、まだいくつかの問題があります。 – nirvair

0

input type='date'...input type='text'...をHTMLで変更してください。

入力時にスタイリングが必要な場合は、使用可能な日付ピッカーの1つを選択してください。

Thymeleafと入力タイプDateには特段の問題があり、これは受け入れられた回答hereで説明されています。

+0

これはどのようにして問題を解決しますか? – nirvair

+0

すでに@DateTimeFormatをdtoのフィールドに追加しました。 – nirvair

+0

変更は 'type = 'date''から' type =' text''までのHTML入力フィールドでなければなりません。あなたのDTOはうまくいくようです。 –

関連する問題