0

현재 최대 절전 모드 - 배포 - 3.6.4. 최종 버전을 사용 중이다. Hibernate Validator를 처음 사용합니다.Hibernate Validator는 Spring Framework를 사용하지 않고 에러 메시지를 검색한다.

문제점 : databean/formbean에 주석을 추가 할 때 발생하는 오류 메시지를 어떻게 검색해야합니까? 나는 봄에 모든 사람들이 messages.properties 파일을 클래스 패스에서 사용하는 것으로 알고있다?

하지만 순수한 최대 절전 모드 3은 어떤 파일이 있습니까, 아니면 어떻게해야합니까? (온라인으로 좋은 답변을 찾지 못했습니다 ...)

답변

1

희망이 도움이 될 것입니다. 출처 : Mastering Spring MVC

유효성 확인을 위해 몇 가지를 추가해야합니다. 첫째, 컨트롤러는 양식 제출시 유효한 모델을 원한다고 말할 필요가 있습니다. 양식을 표시하는 매개 변수에 아

import org.hibernate.validator.constraints.Email; 
    import org.hibernate.validator.constraints.NotEmpty; 

로 주석으로 javax.validation.Valid를 추가하면 그냥 작업을 수행합니다 양식에 오류가 포함되어 있으면 사용자를 리디렉션하지 않는

@RequestMapping(value = "/profile", method = RequestMethod.POST) 
public String saveProfile(@Valid ProfileForm profileForm, 
BindingResult bindingResult) { 
if (bindingResult.hasErrors()) { 
return "profile/profilePage"; 
} 
System.out.println("save ok" + profileForm); 
return "redirect:/profile"; 
} 

참고. 이렇게하면 동일한 웹 페이지에 표시 할 수 있습니다. 말하자면 오류가 표시되는 웹 페이지에 장소를 추가해야합니다. profilePage.html에 다음 줄을 추가합니다 :

<form th:action="@{/profile}" th:object="${profileForm}" 
...."> 
<div class="row"> 
<div class="input-field col s6"> 
<input th:field="${profileForm.twitterHandle}" 
id="twitterHandle" type="text" th:errorclass="invalid"/> 
<label for="twitterHandle">Twitter handle</label> 
<div th:errors="*{twitterHandle}" class="redtext"> 
Error</div> 
</div> 
<div class="input-field col s6"> 
<input th:field="${profileForm.email}" id="email" 
type="text" th:errorclass="invalid"/> 
<label for="email">Email</label> 
<div th:errors="*{email}" class="red-text">Error</div> 
</div> 
</div> 
<div class="row"> 
<div class="input-field col s6"> 
<input th:field="${profileForm.birthDate}" 
id="birthDate" type="text" th:errorclass="invalid" th:placeholder="${ 
dateFormat}"/> 
<label for="birthDate">Birth Date</label> 
<div th:errors="*{birthDate}" class="red-text">Error</ 
div> 
</div> 
</div> 
<div class="row s12"> 
<button class="btn indigo waves-effect waves-light" 
type="submit" name="save">Submit 
<i class="mdi-content-send right"></i> 

</button> 
</div> 
</form> 

예 실제로, 봄 부팅은 우리에게 메시지 소스 bean 작성을 담당한다. 이 메시지 소스에 대한 기본 위치는

src/main/resources/messages. 
properties. 

에 이러한 번들을 만들고 다음 텍스트 추가 :

Size.profileForm.twitterHandle=Please type in your twitter user name 
Email.profileForm.email=Please specify a valid email address 
NotEmpty.profileForm.email=Please specify your email address 
PastLocalDate.profileForm.birthDate=Please specify a real birth date 
NotNull.profileForm.birthDate=Please specify your birth date 
typeMismatch.birthDate = Invalid birth date format 

합니다. enter image description here

+0

감사합니다. 어떻게 우리는 순수한 최대 절전 모드에 대해 언급 한 "번들"을 만들 수 있습니까? 이것은 실제로 나를 가장 혼란스럽게 만든 부분입니다. –

+0

죄송합니다, 이것은 순수한 최대 절전 모드가 아닙니다. 이러한 주석은 Bean 유효성 검증을 지정하는 JSR-303 스펙에서 가져온 것입니다. 이 스펙의 가장 보편적 인 구현은 Spring Boot에 포함되어있는 hibernate-validator이다. 순수한 최대 절전 모드의 경우 문서를 검색해야합니다. –

+0

죄송합니다. 질문에 대한 오해가 있습니다. 나는 당신이 봄 구현을 요청했다고 생각했다. –