카테고리 없음

Spring Boot에서 JSP 사용하기

sh0seo 2020. 11. 26. 13:50

Spring Boot에서 JSP를 사용하는 방법에 대해 정리한다. Spring Boot 환경에서 JSP는 Pivotal에서 추천하는 방법이 아니다. Thymeleaft와 같은 서버 렌더링 엔진을 추천한다.

JSP Limitations

When running a Spring Boot application that uses an embedded servlet container (and is packaged as an executable archive), there are some limitations in the JSP support.

- With Jetty and Tomcat, it should work if you use war packaging. An executable war will work when launched with java -jar, and will also be deployable to any standard container. JSPs are not supported when using an executable jar.

- Undertow does not support JSPs.

- Creating a custom error.jsp page does not override the default view for error handling. Custom error pages should be used instead.

하지만 이런 제약조건에 영향이 적다면 익숙한 JSP를 사용하는 것도 좋을 수 있다.

빌드 툴은 maven을 사용하면 아래와 같이 pom.xml에 추가하다.

 

빌드 툴은 Gradle을 사용하면 아래와 같이 gradle.build에 추가한다.

 

jsp 페이지를 처리하기 위한 prefix와 suffix를 application.properties에 추가한다.

spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp

Spring Boot project 구성은 아래와 같이 구성한다.

-+=src
|--+ main
| |--+ com.example.demo-jsp
| |--- DemoApplication.java
| |--+ controller
| |--- HomeController.java
|--+ resource
| |--- application.properties
|--+ webapp
|--+ WEB-INF
|--+ jsp
|--- index.jsp

http://localhost:8080 URL에 접속하면 처리할 HomeController 클래스를 아래처럼 구현한다.

index.jsp는 Controller에서 전달받은 title을 출력한다.

 

Reference