728x90
반응형
🎯 Goal
- 컨트롤러 클래스 없이 특정 view에대한 컨트롤러 추가
💡 개념
web과 WEB-INF 폴더 / WEB-INF에서의 앵커 태그<a>에 관해서
webapp폴더에 파일이 있을 경우
비즈니스 로직없이도 가상의 결과물을 만들어 직접적으로 뷰를 살펴 볼 수 있다.
그렇기 때문에 http://localhost:8080/test.jsp로 접속 시 해당 페이지를 볼 수 있었던 것이다.
🔑 에러 원인
WEB-INF 폴더에 있는 main.jsp 파일은 왜 볼 수 없었던 것일까?
WEB-INF 폴더의 경우 브라우저에서 직접적으로 접근이 불가한 경로다.
직접적으로 볼 수 없기 때문에 <a> 앵커 태그로 이동이 불가하며, Controller를 통해서만 이동을 해야 한다.
http://localhost:8080/main.jsp와 직접 같이 접근이 불가하다는 것을 의미한다.
💊 해결 방법
WebMvcConfigurer 를 구현하는 클래스를 만들고
addViewControllers(ViewControllerRegistry registry) 메소드를 오버라이딩 하면 된다.
1. WebMvcConfiguration.java 신규 파일 생성
이미 기존에 컨트롤러(Controller) 파일이 있는 위치에 새로운 클래스를 만든다.
위 코드는 "/ main" url이 요청되면 main라는 view로 이동하게 해준다.
package com.nara.start.main;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class WebMvcConfiguration implements WebMvcConfigurer {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/main").setViewName("main");
}
}
2. JSP 파일 경로 수정
a링크에 "/main" 으로 수정해준다.
<div class="app-menu navbar-menu">
<!-- LOGO -->
<div class="navbar-brand-box">
<a href="/main" class="logo logo-light">
<span class="logo-sm">
<img src="/resources/bootstrap/images/logo-sm.png" alt="" height="22">
</span>
<span class="logo-lg">
<img src="/resources/bootstrap/images/logo-light.png" alt="" height="22">
</span>
</a>
<button type="button" class="btn btn-sm p-0 fs-20 header-item float-end btn-vertical-sm-hover"
id="vertical-hover">
<i class="ri-record-circle-line"></i>
</button>
</div>
</div>
3. 서버 재구동
잘 실행되는 것을 볼 수 있다.
📌 Spring Boot 프로젝트 생성(STS) / 빌드 / 실행 방법
https://creative103.tistory.com/96
728x90
반응형
'📌 Back End > └ Spring Boot' 카테고리의 다른 글
[Spring Boot] 스프링부트 Select a wizard에 jsp 검색 해결 방법 (0) | 2024.09.27 |
---|---|
[JSP] <jsp:include> 액션 태그를 이용한 레이아웃 템플릿 (0) | 2024.05.16 |
[Spring Boot] webjars 사용하기 (bootstrap 의존성 추가) (0) | 2024.04.30 |
[Spring Boot] 스프링부트 프로젝트 생성(STS) / 빌드 / 실행 (1) | 2024.04.30 |
[Spring Boot] 스프링부트 Whitelabel Error Page 에러 해결 방법 (0) | 2023.03.16 |