Spring Framrwork

[Spring Boot] welcome-file-list 적용

gregorio 2018. 8. 13. 13:12

Spring Boot에서 webcome-file-list를 등록하는 방법이다.


먼저 Spring MVC에서는 web.xml 파일에 다음과 같이 등록한다.


■ web.xml


     <welcome-file-list>

        <welcome-file>index.jsp</welcome-file>

    </welcome-file-list>

 

index.jsp 파일을 welcome-file로 등록한다.


Spring Boot에서는 web.xml을 사용하지 않을 경우 index.jsp를 welcome-file로 다음과 같이 등록한다.


■WebConfig.java


 import java.util.ArrayList;

import java.util.List;

import java.util.Locale;


import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.boot.web.servlet.FilterRegistrationBean;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

import org.springframework.context.support.ReloadableResourceBundleMessageSource;

import org.springframework.core.Ordered;

import org.springframework.web.servlet.LocaleResolver;

import org.springframework.web.servlet.config.annotation.InterceptorRegistry;

import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;

import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;

import org.springframework.web.servlet.i18n.SessionLocaleResolver;

import org.springframework.web.servlet.view.json.MappingJackson2JsonView;


import dymn.demo.filter.ABCFilter;

import dymn.demo.interceptor.SessionInterceptor;

import dymn.demo.util.PropertiesUtil;


@Configuration

public class WebConfig implements WebMvcConfigurer {


/** 

* Add Index Page

*/

@Override

public void addViewControllers(ViewControllerRegistry registry) {

registry.addViewController("/").setViewName("forward:/index.jsp");

}


}



WebMvcConfigurer addViewControllers를 재정의하여 index.jsp로 forward한다.


즉 http://localhost:8080을 입력하면 http://localhost:8080/index.jsp를 호출하도록 한다.