출처 : https://stackoverflow.com/questions/29842584/send-date-parameter-from-html-form-to-controller-in-thymeleaf



mariaDB의 timestamp 칼럼을 nullable로 작업중에

저장을 하니 오류가 난다. 


thymeleaf에서 timestamp를 th:field로 사용하니 공백("")의 값을 가지고 넘기고 

이걸 java.sql.timestamp로 컨버팅하니 오류가 난다. 


그래서 찾다보니 Controller 클래스에서 사용할 수 있는 @InitBinder 라는 인터페이스가 있더라.


이것의 하는 일은 @ModelAttribute로 넘어오는 모델을 바인딩 해줄 때 약간의 조작을 할 수? 있도록 해주는 인터페이스 인 것 같다.

나는 다음과 같이 수정하여 해결하였다.



@Slf4j

@Controller

@RequestMapping("/admin/worktable")

public class AdminWorktableController {

    @RequestMapping(value = { "/specialday", "/specialday/save" }, method = RequestMethod.POST)

    public ModelAndView setSpecialdaySave(HttpServletRequest request, HttpServletResponse response,

            @Valid @ModelAttribute Tpo_special_day_template template) {

 

        try {

            specialDayTemplateService.save(template);

        } catch (Exception ex) {

            return modelAndView;

        }

        return modelAndView;

    }

   

    @InitBinder

    public void initBinder(WebDataBinder dataBinder){

        dataBinder.registerCustomEditor(java.sql.Timestamp.class, "applyDate",

        new PropertyEditorSupport() {

            @Override

            public void setAsText(String value) {

                try {

                    setValue(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").parse(value));

                } catch (ParseException e) {

                    setValue(null);

                }

            }

        });

    }

}

 








Posted by motolies
,