웹페이지 연동
1. 라이브러리 받기
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
2. 환경설정
- application.properties
- 재시작 필수
# 포트번호 설정
server.port=8080
# devtools 설정
spring.devtools.livereload.enabled=true
# jsp 위치 설정
spring.thymeleaf.prefix=classpath:/templates/
# jsp 확장자 설정
spring.thymeleaf.suffix=.jsp
3. controller 생성
- controller/HomeController.java
@ResponseBody
지우고,
return "jsp파일명";
: 해당 웹페이지가 뜬다.
: 확장자명 필요없음
// Controller = 주소를 쳤을때 웹페이지와 일치시키는 역할
@Controller
public class HomeController {
// 127.0.0.1:8080/home
@GetMapping(value = "/home")
public String homeGET() {
return "home";
}
// 127.0.0.1:8080/join
@GetMapping(value = "/join")
public String joinGET() {
return "join";
}
}
4. jsp 생성
- resources/templates/
- 확장자가 .jsp인것만 인식

5. 실행
- Boot20220225Application.java
- Run을 하면 서버가 돌아가고,
- 크롬에서 해당 서버에 접속 시 웹페이지가 뜨도록 설정
: 웹페이지 = jsp파일
@SpringBootApplication
// 임의로 만든 컨트롤 파일(Controller) 가져오기(위치설정)
@ComponentScan(basePackages = { "com.example.controller" })
public class Boot20220225Application {
public static void main(String[] args) {
// 실행시키면 서버가 돌아감 -> 서버 = Tomcat
SpringApplication.run(Boot20220225Application.class, args);
System.out.println("Hello World!");
}
}