스프링 부트 11일차 - index 페이지, 파비콘, 템플릿 엔진, HtmlUnit, ExceptionHandler
1. index 페이지를 추가하고 싶으면 앞서 배웠던 정적 페이지들의 경로에 index.html 혹은 index.템플릿 같은 식으로 파일을 만들면 됨.
2. 파비콘은 위에 있는 경로에 favicon.ico 라는 이름으로 ico 파일을 넣어주면 됨.
파비콘은 favicon.io에서 만들 수 있음. 다른 그림파일도 파비콘으로 만들 수 있음.
* 파비콘이 안바뀌면 다음 링크 참고 : https://stackoverflow.com/questions/2208933/how-do-i-force-a-favicon-refresh
3. JSP를 스프링 부트에서 권장하지 않는 이유
- JAR 패키징을 했을 때 동작하지 않음. WAR로 패키징 해야 함.
- Undertow는 JSP를 지원하지 않음.
4. Thymeleaf를 사용하기
의존성 추가
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
템플릿 파일 위치는 /src/main/resources/template/ 경로에 위치하면 됨.
5. 의존성이 <scope>test</scope> 인경우 테스트할때만 class path에 들어감
6. HtmlUnit과 Selenium을 통한 웹 클라이언트로 뷰 테스트
다음 의존성 추가
<dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>htmlunit-driver</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>net.sourceforge.htmlunit</groupId> <artifactId>htmlunit</artifactId> <scope>test</scope> </dependency>
자세한 내용은 다음 링크 참고
http://htmlunit.sourceforge.net/gettingStarted.html
7. 전역 ExceptionHandler를 만들고 싶다면 독립적인 Class를 만든 후 그 클래스에
@ControllerAdvice를 붙인 후, 그 내부에 ExceptionHandler를 정의하면 됨.
8. 해당 컨트롤러에서만 사용하고 싶은 ExceptionHandler라면 @ExceptionHandler를 사용해서 해당 컨트롤러에 ExceptionHandler를 등록해주면 됨.
9. BasicErrorController의 @RequestMapping 부분을 보면 다음과 같이 되어있음.
@RequestMapping("${server.error.path:${error.path:/error}}")
여기서 ${error.path:/error} 라고 되어 있는 부분은 error.path가 정의되어 있으면 error.path에 정의된 값을 사용하는 것이고,
만약 정의되어 있지 않으면 /error를 사용하라는 의미이다. 즉 ${A:B} 이면 A가 정의되어있으면 A의 값, 아니면 B 같은 식으로 ":"를 기준으로 값을 선택하여 사용할 수 있다.
10. 커스텀 에러 페이지 만들기
- src/main/resources/public/error/404.html
- 404.html
- 5xx.html
- ErrorViewResolver 구현