복붙노트

[SPRING] 양식이있는 봄 mvc 날짜 형식 : 입력

SPRING

양식이있는 봄 mvc 날짜 형식 : 입력

엔티티와 빈을 최대 절전 모드로 가지고 있습니다.

@Entity
public class GeneralObservation {
    @DateTimeFormat(pattern = "dd/MM/yyyy")
    Date date;

    @Column
    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }
}

나는 또한 가지고있다.

@InitBinder
protected void initBinder(WebDataBinder binder) {
    SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
    binder.registerCustomEditor(Date.class, new CustomDateEditor(
            dateFormat, false));
}

        form:input
                id = "datepicker"
                name="date"
                itemLabel="date"
                path="newObservation.date"

내가 내 URL로 갈 때 나는 본다 :

mm / dd / yyyy 형식을 갖도록하려면 어떻게해야합니까? 고맙습니다.

해결법

  1. ==============================

    1.당신은 fmt : formatDate jstl 태그를 사용할 수 있습니다 :

    당신은 fmt : formatDate jstl 태그를 사용할 수 있습니다 :

    <fmt:formatDate value="${yourObject.date}" var="dateString" pattern="dd/MM/yyyy" />
    <form:input path="date" value="${dateString} .. />
    
  2. ==============================

    2.해결 방법은 다음과 같습니다. 에 mvc-dispatcher-servlet.xml 또한 xmlns : mvc = "http://www.springframework.org/schema/mvc" xml 파일의 시작 부분에.

    해결 방법은 다음과 같습니다. 에 mvc-dispatcher-servlet.xml 또한 xmlns : mvc = "http://www.springframework.org/schema/mvc" xml 파일의 시작 부분에.

  3. ==============================

    3.내 코드에서는 바인더를 다음과 같이 사용합니다.

    내 코드에서는 바인더를 다음과 같이 사용합니다.

    @InitBinder
    public void initBinder(WebDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
        dateFormat.setLenient(false);
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
    }
    
  4. ==============================

    4.HTML5에서 입력 유형 = "날짜", 스프링 (Chrome, Opera 및이 Safari도 동일한 항목이지만 Firefox에서는 아직 없습니다.

    HTML5에서 입력 유형 = "날짜", 스프링 (Chrome, Opera 및이 Safari도 동일한 항목이지만 Firefox에서는 아직 없습니다.

    <form:input type="date" ... >
    
  5. from https://stackoverflow.com/questions/18163404/spring-mvc-date-format-with-forminput by cc-by-sa and MIT license