56698
springの@DateTimeFormatを使ってactorForm.javaでvalidationしたいのですがmessageという要素が無いが為にエラーページが出力されるだけでエラーメッセージが出力されません。  
いろいろと調べたのですがやり方がわかりませんでした。  
良ければ教えてもらますか？

これがactorFormです

```

    //@Pattern(regexp="(\\d{4}/\\d{2}/\\d{2})", message="{actor.validation.birthday}")
        @DateTimeFormat(pattern = "yyyy/MM/dd")
        private String birthday;
    
```

これがControllerです

```

    @GetMapping("/actor/create")
      public String create(ActorForm form, Model model) {
        List<Prefecture> pref = prefectureRepository.findAll();
        model.addAttribute("pref", pref);
        return "Actor/create";
      }
    
      @PostMapping("/actor/save")
      public String save(@Validated @ModelAttribute ActorForm form, BindingResult result, Model model, Locale locale) {
        if (result.hasErrors()) {
          String message = msg.getMessage("actor.validation.error", null, locale);
          model.addAttribute("errorMessage", message);
          return create(form, model);
        }
        Actor actor = convert(form);
        actor = actorRepository.saveAndFlush(actor);
        return "redirect:/actor/" + actor.getId().toString();
      }
    
    /**
       * convert form to model.
       */
      private Actor convert(ActorForm form) {
        Actor actor = new Actor();
        actor.setName(form.getName());
        if (StringUtils.isNotEmpty(form.getHeight())) {
          actor.setHeight(Integer.valueOf(form.getHeight()));
        }
        if (StringUtils.isNotEmpty(form.getBlood())) {
          actor.setBlood(form.getBlood());
        }
        if (StringUtils.isNotEmpty(form.getBirthday())) {
          DateTimeFormatter withoutZone = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
          LocalDateTime parsed = LocalDateTime.parse(form.getBirthday() + " 00:00:00", withoutZone);
          Instant instant = parsed.toInstant(ZoneOffset.ofHours(9));
          actor.setBirthday(Date.from(instant));
        }
        if (StringUtils.isNotEmpty(form.getBirthplaceId())) {
          actor.setBirthplaceId(Integer.valueOf(form.getBirthplaceId()));
        }
        actor.setUpdateAt(new Date());
        return actor;
      }
    
```

propertiesファイル

```

    # @DateTimeFormat
    typeMismatch=  is invalid.
    typeMismatch.int=  must be an integer.
    typeMismatch.double=  must be a double.
    typeMismatch.float=  must be a float.
    typeMismatch.long=  must be a long.
    typeMismatch.short=  must be a short.
    typeMismatch.actorForm.java.lang.Integer=  must be an integer.
    typeMismatch.actorForm.java.lang.Double=  must be a double.
    typeMismatch.actorForm.java.lang.Float=  must be a float.
    typeMismatch.actorForm.java.lang.Long=  must be a long.
    typeMismatch.actorForm.java.lang.Short=  must be a short.
    typeMismatch.actorForm.java.util.Date=  is not a date.
    typeMismatch.actorForm.birthday=aaaaaaaaaa
    typeMismatch.birthday=aaaaaaaa
    
```

htmlファイル

```

    <form role="form" action="/actor/save" th:action="@{/actor/save}" th:object="${actorForm}"
              method="post">
    
    <!-- birthday -->
              <div class="form-group">
                <label for="id_birthday" class="col-sm-2 control-label" th:utext="#{actor.birthday}">birthday</label>
                <div class="col-sm-12">
                  <input id="id_birthday" class="form-control" type="text" name="birthday" th:field="*{birthday}" th:placeholder="#{actor.create.birthday}"/>
                  <span th:if="${#fields.hasErrors('birthday')}" th:errors="*{birthday}" class="help-block">error!</span>
                </div>
              </div>
    
```
CC BY-SA 4.0
2019-07-16T07:54:53.747
56679
2019-07-17T06:36:53.430
15607
question
spring-boot thymeleaf
Spring boot & Thymeleafで@DateTimeFormatを使ってエラーメッセージを出力する方法
[`@DateTimeFormat`](https://docs.spring.io/spring/docs/current/javadoc-
api/org/springframework/format/annotation/DateTimeFormat.html) は
`String`でなく日付時刻型に付与するのが正しいかと思います。

**`ActorForm.java`** :

```

        @DateTimeFormat(pattern = "uuuu/MM/dd")
        private LocalDate birthday;
    
```

また、コントローラで使用しているメッセージプロパティ `actor.validation.error` も必要です。

**`messages.properties`** :

```

    actor.validation.error=error occurred
    
```

([差分](https://github.com/yukihane/stackoverflow-
qa/commit/aef0b4befdd1de5434499247e2915fa71642da3e))
CC BY-SA 4.0
2019-07-17T06:36:53.430
56698
2019-07-17T06:36:53.430
2808
56679
answer