2016-08-30 7 views
0

스프링 MVC + 타임 호프를 사용하고 있으며 폼을 제출할 때 바인딩 결과에 항상 유효성 검사 오류 (Null)가 표시됩니다. 모든 조언을 부탁드립니다! 감사!폼이 제출 될 때 Null을 반환합니다.

오류 메시지

"Field error in object 'createForm' on field 'authorId': rejected value [null]; codes [NotNull.createForm.authorId,NotNull.authorId,NotNull]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [createForm.authorId,authorId]; arguments []; default message [authorId]]; default message [may not be null]" 
@NotNull 
@Size(min= 2, max = 100, message = "your title should be between 2 and 100 symbols") 
private String title; 

@NotNull 
@Size(min = 2, message = "Please fill your message") 
private String content; 

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

@NotNull 
private String authorId; 



public String getTitle() { 
    return title; 
} 

public void setTitle(String title) { 
    this.title = title; 
} 

public String getContent() { 
    return content; 
} 

public void setContent(String content) { 
    this.content = content; 
} 

public Date getDate() { 
    return date; 
} 

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

public String getId() { 
    return authorId; 
} 

public void setId(String authorId) { 
    this.authorId = authorId; 
} 
} 

그리고 내 HTML 질문에 대한 코멘트에 작성된

<form id="create-form" method="post" th:object="${createForm}"> 
    <div><label for="title">Title:</label></div> 
    <input id="title" type="text" name="title" th:value="*{title}"/> 
    <span class="formError" th:if="${#fields.hasErrors('title')}" th:errors="*{title}">Invalid title</span> 
    <div><label for="content">Content:</label></div> 
    <textarea name="content" rows="30" cols="100" id="content" th:value="*{content}"></textarea> 
    <span class="formError" th:if="${#fields.hasErrors('content')}" th:errors="*{content}">Invalid content</span> 
    <div><label for="date">Date:</label></div> 
    <input id="date" type="date" name="date" th:value="*{date}"/> 
    <span class="formError" th:if="${#fields.hasErrors('date')}" th:errors="*{date}">Invalid date</span> 
    <div><label for="authorId">Author ID:</label></div> 
    <input id="authorId" type="text" name="authorId" th:value="*{authorId}"/> 
    <span class="formError" th:if="${#fields.hasErrors('id')}" th:errors="*{authorId}">Invalid id</span> 

    <br/> 
    <br/> 
    <div><input type="submit" value="Create"/></div> 
</form> 
+0

당신은 어떤 저자 ID를 통과합니까? –

+0

'@ NotNull'은 'null 보호 서비스'가 아닙니다. 네가 스스로 널 관리하지 않겠다는 계약서 야. – xenteros

+1

$ {# fields.hasErrors ('id')} 또는 $ {# fields.hasErrors ('authorID')} – Aroniaina

답변

1

: @NotNullnull에서 당신을 보호하지 않습니다

주석 . 이 코드를 사용하는 개발자와 다른 개발자와의 계약은 null 일 수 없습니다.

이것은 사용자가 직접 데이터의 유효성을 검사해야 함을 의미합니다. 두 가지 접근 방식이 있습니다. null을 허용하는 AuthorTO을 생성 한 다음 예기치 않은 null이 없거나 프런트 엔드 측에서 유효성이 확인 된 경우에만 Author을 생성하십시오.

관련 문제