인프런 커뮤니티 질문&답변
jsp, thymeleaf 동시 사용
작성
·
1.6K
0
스프링 부트에서 jsp와 thymeleaf를 어떻게 동시에 사용할 수 있는지 궁금합니다. jsp는 tiles.xml에서
<definition name="*" extends="main-layout">
<put-attribute name="body" value="/WEB-INF/views/{1}.jsp"/>
</definition>
이런식으로 jsp파일을 읽어오는데 thymeleaf는 어떻게 하는지 모르겠습니다.
답변 1
0
안녕하세요, 인프런 AI 인턴이에요.
스프링 부트에서 jsp와 thymeleaf를 동시에 사용하기 위해서는 몇 가지 설정이 필요합니다.
- 의존성 추가: pom.xml 파일에 thymeleaf와 jsp 관련 의존성을 추가해야 합니다. 아래는 예시입니다.
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
</dependencies>
- ViewResolver 설정: 스프링 부트의 application.properties 파일에 다음과 같이 ViewResolver를 설정해야 합니다.
# JSP 설정
spring.mvc.view.prefix: /WEB-INF/views/
spring.mvc.view.suffix: .jsp
# Thymeleaf 설정
spring.thymeleaf.prefix: classpath:/templates/
spring.thymeleaf.suffix: .html
spring.thymeleaf.mode: HTML5
- Thymeleaf 사용 스프링 컨트롤러: Thymeleaf를 사용하려면 컨트롤러에서 Thymeleaf를 통해 렌더링할 템플릿을 리턴해야 합니다. 예를 들어, 아래와 같이 작성할 수 있습니다.
@Controller
public class SampleController {
@GetMapping("/sample")
public String sample(Model model) {
model.addAttribute("message", "Hello, World!");
return "sample"; // sample.html 템플릿을 사용해 렌더링
}
}
- JSP 사용 스프링 컨트롤러: JSP를 사용하기 위해서는 컨트롤러에서 JSP 파일의 경로를 리턴해야 합니다. 예를 들어, 다음과 같이 작성할 수 있습니다.
@Controller
public class SampleController {
@GetMapping("/sample")
public String sample(Model model) {
model.addAttribute("message", "Hello, World!");
return "sample"; // sample.jsp 파일을 사용해 렌더링
}
}
이렇게 설정하면 jsp와 thymeleaf를 동시에 사용할 수 있습니다. 각각의 ViewResolver 설정을 통해 각각의 템플릿 엔진에서 사용할 파일의 경로를 지정해 주세요.
더 자세한 내용은 스프링 부트와 thymeleaf, jsp에 대한 공식 문서 및 인프런의 강의를 참고하시면 도움이 될 것입니다.
궁금한 점이나 추가로 도움이 필요하시면 언제든지 물어보세요. 좋은 하루 되세요!




