728x90
Controller 사용해서 에러 페이지 반환 - 정적 페이지
CustomErrorController: Spring이 기본 제공하는 ErrorController 인터페이스 자체 구현
-> 에러 시 statusCode에 따라 표기 페이지 분기
@Controller
public class CustomErrorController implements ErrorController {
@RequestMapping(value = "/error", method = RequestMethod.GET)
public String handleError(HttpServletRequest request) {
Object status = request.getAttribute(RequestDispatcher.ERROR_STATUS_CODE);
if (status != null) {
Integer statusCode = Integer.valueOf(status.toString());
if (statusCode == HttpStatus.NOT_FOUND.value()) {
return "error/404";
} else if (statusCode == HttpStatus.INTERNAL_SERVER_ERROR.value()) {
return "error/500";
}
}
return "error";
}
}
Controller없이 에러페이지를 어떻게 유저에게 제공할 수 있었는가?
-> Spring이 제공하는 ErrorController인터페이스 기본 구현체를 통해서 제공: BasicErrorController
package org.springframework.boot.autoconfigure.web.servlet.error;
@Controller
@RequestMapping("${server.error.path:${error.path:/error}}")
public class BasicErrorController extends AbstractErrorController {
@RequestMapping(produces = MediaType.TEXT_HTML_VALUE)
public ModelAndView errorHtml(
HttpServletRequest request,
HttpServletResponse response
) { ... }
@RequestMapping
public ResponseEntity<Map<String, Object>> error(
HttpServletRequest request
) { ... }
}
Controller 사용해서 에러 페이지 반환 - 동적 페이지
CustomErrorController: Spring이 기본 제공하는 ErrorController 인터페이스 자체 구현
-> 에러 시 반환 Statuscode에 따라서 단일 페이지 내 Model을 통해 분기
@Controller
public class CustomErrorController implements ErrorController {
@RequestMapping(value = "/error", method = RequestMethod.GET)
public ModelAndView handleError(HttpServletRequest request) {
ModelAndView modelAndView = new ModelAndView();
Object status = request.getAttribute(RequestDispatcher.ERROR_STATUS_CODE);
if (status != null) {
Integer statusCode = Integer.valueOf(status.toString());
if (statusCode == HttpStatus.NOT_FOUND.value()) {
modelAndView.addObject("status", statusCode);
modelAndView.addObject("reason", HttpStatus.NOT_FOUND.getReasonPhrase());
} else if (statusCode == HttpStatus.INTERNAL_SERVER_ERROR.value()) {
modelAndView.addObject("status", statusCode);
modelAndView.addObject("reason", HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase());
}
}
modelAndView.setViewName("error");
return modelAndView;
}
}
ModelAndView를 통해 ViewTemplate + Model을 반환 (상황별 동적 에러 페이지)
웹 페이지를 반환하기 위한 @Controller 생성
기존 WelcomPageHandlerMapping 아닌 RequestMappingHandlerMapping으로 대체
- `WelcompageHandlerMapping`: 디렉토리 내 페이지 기반으로 반환, Spring 알아서 처리
- `RequestMappingHandlerMapping`: `@RequestMapping` 통해 직접 페이지 반환
@Controller에서 String으로 ViewTempalge 반환시 Thymeleaf 접두사 & 접미사 자동 적용
`/resource/templates`: Thymeleaf 자체적으로 ViewTemplate 을 저장하기 위해 사용하는 디렉터리
- 접두사 : spring.thymeleaf.prefix = "classpath: /templates"
- 접미사: spring.thymeleaf.suffix = ".html"
위 값은 `EnabledAutoConfiguration`을 통해 설정된 설정값들이다.
이 설정 값을 변경하려면 `application.properties(.yml)` 내 thymeleaf 관련 설정
# 정적 리소스에 변화가 있을 때 바로 반영한다.
spring.devtools.livereload.enabled=true
# thymeleaf 참조 경로
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
# thymeleaf 에 대한 캐시를 남기지 않는다. cache=false 설정 (운영시는 true)
spring.thymeleaf.cache=false
# templates 디렉토리에 파일이 있는지 없는지 체크, 없으면 에러를 발생시킨다.
spring.thymeleaf.check-template-location=true
# templates 디렉토리에서 반환하기 위한 파일 타입 (HTML, UTF-8) 필수 설정이 아님
spring.thymeleaf.mode=HTML
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.servlet.content-type=text/html
728x90
'ASAC 웹 풀스택 > Spring Boot' 카테고리의 다른 글
Spring MVC 원리(2) - Tomcat + Spring 상세구조 (0) | 2024.10.07 |
---|---|
Spring MVC 원리(1) - 동작 과정 (0) | 2024.10.07 |
[Spring Boot] 기본 MVC 개발을 위한 Annotations 과 그 이해(1) - 컨트롤러 없이 정적 페이지 반환 (0) | 2024.10.06 |
Java 기본 문법 및 JVM 구성(14) - SOLID (3) | 2024.09.30 |
Java 기본 문법 및 JVM 구성(13) - Enum 활용 (0) | 2024.09.30 |