스프링부트

스프링 부트에서 jsp 사용하기

클라스하이 2023. 7. 15. 19:26

스프링 부트에선 jsp를 기본으로 지원하지 않는다. dependency에 따로 추가해야 하는 것들과 만들어야할 폴더들과 설정이 필요하다. 다음의 순서대로 jsp 사용을 위한 설정을 해보자. 

프로젝트셋팅

  • Dependency 추가 
    • Spring Web

디펜던씨

  • STS4에서 import
    • Maven > Existing Maven Projects 클릭해서  해당 프로젝트 위치 넣고 pom.xml 체크하여 import 한다. 

임포트

  • pom.xml 파일에 아래의 의존성 추가 
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>jstl</artifactId>
</dependency>
<dependency>
    <groupId>org.apache.tomcat.embed</groupId>
    <artifactId>tomcat-embed-jasper</artifactId>
</dependency>
  • application.properties에 View Resolver 설정 추가
# 서버 포트 번호
server.port = 8092

# JSP View Resoler, webapp, WEB-INF 폴더가 없음 
spring.mvc.view.prefix=/WEB-INF/views/
spring.mvc.view.suffix=.jsp
  • src/main 폴더에 webapp 폴더 만들어서 그 안에 WEB-INF, 그 안에 views 폴더 만들어서 test.jsp 파일 만들기

이런구조

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<link rel="stylesheet" href="https://me2.do/5BvBFJ57">
<title>Insert title here</title>
</head>
<body>
<!-- localhost:8092 -->
	<h1>Spring Boot</h1>
	<!-- test.jsp -->
	<div>${name}</div>
</body>
</html>
  • com.test.bootjsp.controller 패키지를 만들고 TestController.java 작성
  • 브라우저로 http://localhost:8092/test.do 접속하면 끝
    • boot는 context root를 별다른 설정을 안하면 / 로 지정이 된다. 
    • application.properties에 아래 한줄을 추가하면 그 path로 컨텍스트루트가 지정이 된다.
      • server.servlet.context-path=/지정할컨텍스트루트

 

● 실행결과