에러메시지가 타임리프에 출력이 안됍니다..
777
작성한 질문수 2
안녕하세요 강사님, 정말 좋은강의 항상 잘 듣고 있습니다.
저가 BindingResult에 관련해서 따로 실습을 진행하다가, html에 에러가 출력되지 않아 애를먹고있어 부득이하게 질문을 올리게 돼었습니다 .. ㅠㅠ
컨트롤러에 BindingResult를 log에 출력해봐도 정상적으로 출력되고, 리다이렉션도 되는것을 보면 컨트롤러부분은 아닌것 같고, 타임리프쪽의 문제인것 같은데 도저히 해결방법을 모르겠어서 질문드립니다.. ... 강의 항상 잘 듣고 배우고있습니다. 감사합니다
순서대로 컨트롤러, messeage.properties, html입니다.
@PostMapping
public String joinUser(userInfo userinfo, mangerInfo mangerinfo, BindingResult bindingResult, RedirectAttributes redirectAttributes,Model model){
log.info("userinfo.getId()={}",userinfo.getId());
log.info("userinfo.getPassword()={}",userinfo.getPassword());
log.info("userinfo.getEmail()={}",userinfo.getEmail());
if(userinfo.getId().isEmpty()){
bindingResult.addError(new FieldError("userinfo", "id", userinfo.getId(), false, new String[]{"login.id"}, null, null));
}
if(userinfo.getPassword()==null){
bindingResult.addError(new FieldError("userinfo","password",userinfo.getPassword(),false,new String[]{"login.password"},new Object[]{45},null));
}
if(userinfo.getEmail().isEmpty()){
bindingResult.addError(new FieldError("userinfo","email",userinfo.getEmail(),false,new String[]{"login.email"},null,null));
}
if(bindingResult.hasErrors()){
model.addAttribute("errors",bindingResult);
log.info("errors={}",bindingResult);
return "Join/signUp";
}else {
if (mangerinfo.getNumber() == null) {
joinService.join(userinfo.getEmail(), userinfo.getId(), userinfo.getPassword(), null);
userInfo joinResult = joinService.myInfo(userinfo.getId());
redirectAttributes.addAttribute("id", joinResult.getId());
redirectAttributes.addAttribute("email", joinResult.getEmail());
} else {
joinService.join(userinfo.getEmail(), userinfo.getId(), userinfo.getPassword(), mangerinfo.getNumber());
userInfo mangerInfo = joinService.myInfo(userinfo.getId());
redirectAttributes.addAttribute("id", mangerInfo.getId());
redirectAttributes.addAttribute("email", mangerInfo.getId());
}
return "redirect:/Join/{id}";
}
}
login.id=id는 필수로 작성해야 합니다.
login.password=비밀번호는 필수로 작성해야 합니다. 또한 최대길이는 {0} 입니다.
login.email=이메일은 필수로 작성해야 합니다.
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>sign up</title>
<link rel="stylesheet" href="/css/signUp.css">
<link rel="stylesheet" th:href="@{/css/signUp.css}">
<style>
.field-error {
border-color: #dc3545;
color: #dc3545;
}
</style>
</head>
<body>
<form th:action th:object="${userInfo}" method="post" name="form">
<div id="join">
<h1>회원가입</h1>
<table>
<tr>
<td>ID</td>
<td>
<label>
<input type="text"
th:name="id"
th:field="*{id}"
th:errorclass="field-error">
<div class="field-error" th:field="*{id}" th:if="${#fields.hasErrors('id')}" th:errors="*{id}">
id 오류
</div>
</label>
</td>
</tr>
<tr>
<td>비밀번호</td>
<td>
<label>
<input type="password"
th:name="password"
th:field="*{password}"
th:errorclass="field-error">
<div class="field-error" th:errors="*{password}" >
password 오류
</div>
</label>
</td>
</tr>
<tr>
<td>비밀번호 확인</td>
<td><label>
<input type="password" th:field="*{password}">
</label></td>
</tr>
<tr>
<td>관리자입니까?</td>
<td>
<label>
<input type="checkbox" name="checkbutton" onclick="f_check()">
</label>
</td>
</tr>
<tr>
<td>관리자 번호</td>
<td>
<label>
<input type="text" name="textbox">
</label>
</td>
</tr>
<tr>
<td>e-mail</td>
<td>
<label>
<input type="text" name="email" th:field="*{email}" th:errorclass="field-error">
<div class="field-error" th:errors="*{email}" >
email 오류
</div>
</label>
</td>
</tr>
<tr>
<td>e-mail 인증번호</td>
<td><label>
<input type="text" name="emailCheck" >
</label></td>
</tr>
<tr>
<td colspan="2" class="answer">
<input type="button" value="나가기">
<input type="submit" value="가입하기">
</td>
</tr>
</table>
<script src="../js/signUp.js" charset="utf-8"></script>
</div>
</form>
</body>
</html>
답변 2
0
안녕하세요. 진성이님
전체 프로젝트를 압축해서 구글 드라이브로 공유해서 링크를 남겨주세요.
구글 드라이브 업로드 방법은 다음을 참고해주세요.
추가로 다음 내용도 코멘트 부탁드립니다.
1. 실행 방법을 알려주세요.
2. 어떻게 문제를 확인할 수 있는지 자세한 설명을 남겨주세요.
감사합니다.
0
https://drive.google.com/file/d/1N-CeX6LCTESOywp-iKFH3zY4K4eZ0-q1/view?usp=sharing
구글드라이브 링크입니다.. ReviewApplication.class를 run하시면 됩니다...
문제가 발생하는 부분은 signUp.html과 ,JoinController.java의 joinUser 메서드입니다..
검증의 방법대로 field를 생성해서 signUp.html의 타임리프부분에 출력시켜주었는데,
정상적으로 에러로그는 작동하지만 html에서 출력이 되지 않고 있습니다.
해당문제가 발목을잡구있어서 진도를 못나가고 있습니다 .... . 바쁘신데 도움주셔서 정말 감사드립니다 강사님 !!
0
안녕하세요. 진성이님 액세스 권한 필요하다고 나옵니다.
구글 드라이브 업로드 방법을 끝까지 참고해서 액세스 권한 부분을 확인해주세요.
감사합니다.
0
https://drive.google.com/file/d/1N-CeX6LCTESOywp-iKFH3zY4K4eZ0-q1/view?usp=sharing
링크 다시 첨부하겠습니다..! 바쁘신데 번거롭게해드려 죄송합니다 강사님
0
안녕하세요. 진성이님
다음 코드를 참고해주세요.
@PostMapping
public String joinUser(userInfo userinfo, BindingResult userBinding, mangerInfo mangerinfo, BindingResult mangerBinding, RedirectAttributes redirectAttributes,Model model){
강의에서 설명드린 것 처럼 컨트롤러에서 BindingResult의 위치는 모델 객체의 바로 다음에 와야 합니다.
UserInfo 다음에 UserInfo와 관련된 BindingResult를 넣어주세요.
추가로 userBinding.addError() 방식은 너무 복잡해서 사용하지 않는 것이 좋습니다.
강의에서 설명드린 것 처럼 더 나은 방법들을 사용해주세요.
감사합니다.
0
에러 해결했습니다 ㅠㅠ 감사합니다 강사님..!!!
정말 죄송하지만 질문 한가지만 추가로 드리겠습니다....!
첨부한 코드처럼, 각각 userInfo가 실행한 검증 결과를 저장할 BindingResult.
mangerInfo가 실행한 검증 결과를 저장할 BindingResult가 따로따로 있어야하는것이 맞나요??
프로퍼티들별로 한개씩 가지고있어야 한다고 이해했는데, 맞나요???
@PostMapping
public String joinUser(@Validated @ModelAttribute userInfo userinfo, BindingResult bindingResult2,@ModelAttribute mangerInfo mangerinfo, BindingResult bindingResult, RedirectAttributes redirectAttributes) {
...
}
0
네 맞습니다. 그런데 이렇게 나누지 마시고, UserInfo안에 userInfo + managerInfo 정보를 모두 다 넣으시고 하나만 관리하는 것이 더 나은 선택입니다.
감사합니다.
이미지 업로드와 db 트랜잭션 묶는법
0
68
1
Could not resolve org.springframework.boot:spring-boot-starter-validation:2.4.4
0
92
2
MessageSourceTest 코드
0
67
1
인터셉터 에러 설정
0
66
1
resolveArgument()메서드 질문
0
71
1
43강 검증1 에서 실패 로직 관련 질문있습니다.
0
81
2
타임리프 3.X 버전 rendering, serializer 에러 해결 방법
2
154
3
스프링 빈에 등록이 안되는거 같은데 어떻게 하면 좋을까요?ㅠㅠ
0
121
3
pdf 오타 문의
0
63
1
ItemUpdateForm 검증 관련 질문입니다.
0
62
1
22page 링크 주소 변경
0
69
2
특정 데이터와 파일을 함께 저장 시, 테이블 구조 질문
0
61
1
섹션3번 수업에 대한 질문입니다.
0
98
2
@Autowired 보다 더 좋은 방법이 어떤 걸까요?
0
97
2
타입컨버터 가 람다랑 비슷해 보이는데 저의 생각이 맞는지?.
0
81
1
자바스크립트 인라인에서 객체 직렬화 시 오류가 납니다
0
159
3
스프링부트 - 오류페이지2 에서 500.html 에서 쓰인 객체 질문
0
79
1
톰캣 에러 페이지가 안보입니다.
0
120
2
apiEceptionController에서 센드 에러 호출하면 안되는지?
0
91
1
세션 타임아웃시 쿠키 삭제 방법이 없나요?
0
135
2
ApiExceptionController 질문드립니다.
0
71
1
셀렉박스 챕터에서 option value에 ==배송 방식 선택== 이것을 넣은 이유가 궁금함, 이렇게 구상해도 되는지?
0
69
1
MemberRepository 필드의 fianl 선언 유무
0
92
2
혹시 index.html 에서는 fragment 사용이 안되는건가요
0
64
1





