일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 | 31 |
- db
- Web Server
- java
- Oracle
- MySQL
- php
- it
- AWS
- jsp
- IntelliJ
- ReactJS
- Spring Batch
- elasticsearch
- Spring Boot
- Git
- javascript
- redis
- devops
- Gradle
- 요리
- linux
- tool
- Design Patterns
- springboot
- laravel
- jenkins
- 맛집
- Spring
- ubuntu
- JVM
- Today
- Total
아무거나
[Spring Boot] Spring Boot에서 Tomcat연동 설정 본문
[준비]
해당 포스트는 IntelliJ IDE기반이므로 https://bkjeon1614.tistory.com/56을 참고하여 먼저 IntelliJ와 tomcat을 연동하자.
1. build.gradle dependencies 추가
dependencies {
...
providedRuntime 'org.springframework.boot:spring-boot-starter-tomcat’
...
}
2. SpringBootServletInitializer에 configure추가
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
@SpringBootApplication
public class WebApplication extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(WebApplication.class, args);
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(WebApplication.class);
}
}
3. src/main/webapp 폴더를 생성하고 web path 관련 디렉토리를 생성한다.
[ex]
4. 필자는 Thymeleaf 기반으로 작업을 하였으므로 해당 기반으로 thymeleaf config파일에 작업하였다.
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.thymeleaf.spring5.SpringTemplateEngine;
import org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver;
import org.thymeleaf.spring5.view.ThymeleafViewResolver;
@Configuration
@EnableWebMvc
public class ThymeleafConfiguration {
@Bean
public SpringTemplateEngine templateEngine() {
SpringTemplateEngine templateEngine = new SpringTemplateEngine();
templateEngine.setTemplateResolver(thymeleafTemplateResolver());
return templateEngine;
}
@Bean
public SpringResourceTemplateResolver thymeleafTemplateResolver() {
SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver();
templateResolver.setPrefix("/WEB-INF/templates/");
templateResolver.setSuffix(".html");
templateResolver.setTemplateMode("HTML5");
templateResolver.setCharacterEncoding("UTF-8");
return templateResolver;
}
@Bean
public ThymeleafViewResolver thymeleafViewResolver() {
ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
viewResolver.setCharacterEncoding("UTF-8");
viewResolver.setTemplateEngine(templateEngine());
return viewResolver;
}
}
5. view config파일에서 아래 내용처럼 입력하자.( 정적파일 추가: css, js 등..)
import com.wmp.admin.interceptor.LoginInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
@Configuration
@EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter implements WebMvcConfigurer {
@Autowired
@Qualifier(value = "loginInterceptor")
private LoginInterceptor loginInterceptor;
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/css/**").addResourceLocations("/css/");
registry.addResourceHandler("/js/**").addResourceLocations("/js/");
registry.addResourceHandler("/img/**").addResourceLocations("/img/");
}
}
6. 그리고 ide로 tomcat을 실행하면 아래와 같이 출력되는 것을 볼 수 있다.
'Java & Kotlin > Spring' 카테고리의 다른 글
[Spring Boot] @ComponentScan 이란 (0) | 2019.01.16 |
---|---|
[Spring Boot] 스케줄링 구현 (0) | 2019.01.16 |
[Spring Boot] logback을 이용한 sql log 콘솔에 출력 (0) | 2018.12.27 |
[Spring Boot] 간단한 테스트 코드 작성 (0) | 2018.12.06 |
[Spring Boot] Spring Boot + Gradle + VueJS 연동 (4) | 2018.09.28 |