2017-12-11 3 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이다 날짜가 자바입니다 .sql.Date

또한 가져올 때 내가 사용자 배열을 가지고있는 JSON 파일을 통해이 작업을 성공적으로 수행 할 수 있습니다.

뷰를 통해 실행하면 validFromvalidTill의 값이 null로 표시됩니다.

또한 제출을 클릭 한 후 양식 데이터를 확인하면 필요한 모든 입력이 올바르게 설정됩니다. 이벤트에 Date 매개 변수가 설정됩니다.

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] 

나는 여기에 놓치고 무엇 : 나는 BindingResult 오류를 인쇄 할 때

이 무엇입니까?

참고 : 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"; 
} 

그리고 그 자바 코드 쇼는 (당신이 다른 사람이 귀하의 질문에 게시하지 뭔가를하고있는 경우 제외) 무엇 때문에 양식 동작은 created 대신 users/created 사용해야합니다.

+0

컨트롤러에 @RequestMapping ("사용자")을 지정했습니다. 그래서 나는 그것에 능숙합니다. 그리고 예, 이미 @Get 매핑이 있습니다. 그리고 그것은 당신의 대답에서 언급 한 것과 정확하게 같습니다. 그러나 여전히 문제가 있습니다. – nirvair

0

HTML의 input type='date'...에서 input type='text'...으로 변경하십시오.

입력에 스타일을 지정해야하는 경우 사용 가능한 날짜 선택기 중 하나를 선택하십시오.

Thymeleaf 및 입력 유형 Date과 관련된 특정 문제가 있으며 답변은 here입니다.

+0

이것이 어떻게 문제를 해결합니까? – nirvair

+0

이미 @DateTimeFormat을 dto의 필드에 추가했습니다. – nirvair

+0

변경 사항은'type = 'date ''에서'type ='text ''까지의 HTML 입력 필드 여야합니다. 귀하의 DTO는 괜찮아 보입니다. –