Eclipse + SpringBoot + JSP ๊ฐœ๋ฐœํ™˜๊ฒฝ ์„ธํŒ…ํ•˜๊ธฐ 2ํƒ„์ž…๋‹ˆ๋‹ค.

์˜ค๋Š˜์€ backend ๋ณด๋‹ค๋Š” frontend ์ž‘์—…์„ ์œ„ํ•œ Bootstrap ์ถ”๊ฐ€๋ฅผ webjar๋ฅผ ์ด์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ์„ค์ •ํ•ด๋ณด๋„๋ก ํ•  ์˜ˆ์ •์ž…๋‹ˆ๋‹ค.

์ €๋Š” backend ๊ฐœ๋ฐœ์ž๋‹ค๋ณด๋‹ˆ front์ชฝ์€ ์ž์„ธํžˆ ์•Œ๋ ค๋“œ๋ฆฌ๊ธฐ๋Š” ํž˜๋“ค์ง€๋งŒ ๋น ๋ฅด๊ฒŒ ์„œ์น˜ํ•ด์„œ ์‚ฌ์šฉํ•ด๋ณธ ๊ฒฝํ—˜์ƒ webjar๋Š” maven ์˜์กด์„ฑ ๊ด€๋ฆฌ๋ฅผ ํ†ตํ•ด ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ๋‹ค์šด๋ฐ›์•„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์–ด ๋งค์šฐ ์‰ฝ๊ฒŒ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ถ”๊ฐ€๊ฐ€ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. org.webjars์—๋Š” bootstrap, jquery, font-awesome, swagger-ui ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ angularJS, momentJS, d3js ๋“ฑ ๋งค์šฐ ๋งŽ์€ ํ”„๋ก ํŠธ ๊ฐœ๋ฐœ์„ ์œ„ํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋“ค์„ ์ œ๊ณตํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๋งค๋ฒˆ ๋‹ค์šด๋กœ๋“œ๋ฐ›์•„์„œ ํ”„๋กœ์ ํŠธ์— ์ถ”๊ฐ€ํ•  ํ•„์š” ์—†์ด ๋ฐฑ์—”๋“œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋“ค์„ ๊ด€๋ฆฌํ•˜๋“ฏ์ด ๋ฉ”์ด๋ธ ์˜์กด์„ฑ์œผ๋กœ ์‰ฝ๊ฒŒ ๊ด€๋ฆฌํ•  ์ˆ˜ ์žˆ๋„๋ก ๋„์™€์ฃผ๋Š” ์—ญํ• ์„ ํ•œ๋‹ค๊ณ  ๋ณด์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

์šฐ์„  ์ง€๋‚œ์‹œ๊ฐ„์— ํ–ˆ๋˜ Eclipse + SpringBoot + JSP ๊ฐœ๋ฐœํ™˜๊ฒฝ ์„ธํŒ…ํ•˜๊ธฐ 1ํƒ„ ์„ ์™„์„ฑํ•˜์…จ๋˜ ๋ถ„๋“ค์„ ๊ธฐ์ค€์œผ๋กœ ์„ค๋ช…์„ ํ•  ์˜ˆ์ •์ด๋‹ˆ ํ•ด๋‹น ํฌ์ŠคํŒ…์„ ์•ˆ๋ณด์‹  ๋ถ„๋“ค์€ ํ•œ๋ฒˆ ์ญˆ์šฑ~ ํ›‘์–ด๋ณด์‹œ๊ณ  ๋‹ค์‹œ ์˜ค์‹œ๊ธฐ ๋ฐ”๋ž๋‹ˆ๋‹ค.

webjar ์„ค์ •์„ ์œ„ํ•ด์„œ ์ถ”๊ฐ€ํ•  ํŒŒ์ผ์€ ๋”ฑ ํ•˜๋‚˜์ž…๋‹ˆ๋‹ค. org.springframework.web.servlet.config.annotation.WebMvcConfigurer๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” WebMVCConfig ํด๋ž˜์Šค๋ฅผ ํ•˜๋‚˜ ์ƒ์„ฑํ•˜๊ณ  ๋‹ค์Œ๊ณผ ๊ฐ™์ด addResourceHandlers ๋ฉ”์„œ๋“œ๋ฅผ ์˜ค๋ฒ„๋ผ์ด๋“œํ•˜์—ฌ ์ž‘์„ฑํ•ด์ค๋‹ˆ๋‹ค.

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
	    if (!registry.hasMappingForPattern("/webjars/**")) {
	        registry.addResourceHandler("/webjars/**").addResourceLocations(
	                "classpath:/META-INF/resources/webjars/");
	    }
	    if (!registry.hasMappingForPattern("/**")) {
	        registry.addResourceHandler("/**").addResourceLocations(
	        		new String[] {"classpath:/static/script", "classpath:/static/style"});
	    }
    }

 

์šฐ์„  webjars ๊ฒฝ๋กœ๋ฅผ ๋“ฑ๋กํ•ด์ฃผ๋Š” ๊ฒƒ ๋•Œ๋ฌธ์— ์ด ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค์–ด ์‚ฌ์šฉํ•˜๋Š”๋ฐ ์ด๋•Œ @EnableWebMvc ์–ด๋…ธํ…Œ์ด์…˜์„ ์‚ฌ์šฉํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ์—ฌ๊ธฐ์„œ ์ฃผ์˜ํ•  ์ ์ด ์ด ์–ด๋…ธํ…Œ์ด์…˜์„ ์‚ฌ์šฉํ•˜๊ฒŒ๋˜๋ฉด WebMvcAutoConfiguration ์ด disable๋˜๋ฉด์„œ ๊ธฐ์กด ์„ค์ •๋“ค์ด ์ •์ƒ์ ์œผ๋กœ ๋™์ž‘ํ•˜์ง€ ์•Š๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ๊ธฐ์กด์— ์ž˜ ์ฝ์–ด์˜ค๋˜ staticํ•˜์œ„์˜ javascriptํŒŒ์ผ๋“ค๊ณผ cssํŒŒ์ผ๋“ค์„ ๋ชป์ฐพ์•„ 404 ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•˜๊ฒŒ ๋˜์ฃ . ๊ทธ๋ž˜์„œ addResourceHandlers์— ์ด ๋‘ ํŒŒ์ผ๋“ค์ด ์กด์žฌํ•˜๋Š” ๋””๋ ‰ํ† ๋ฆฌ๋ฅผ classpath๊ฒฝ๋กœ๋กœ ์ถ”๊ฐ€ํ•ด์ค˜์•ผ ํ•ฉ๋‹ˆ๋‹ค.

๋˜ํ•œ, application.properties ํŒŒ์ผ์— ์ž‘์„ฑํ•˜์˜€๋˜ ์•„๋ž˜ ๋‘ ๋ผ์ธ์€ ๋”์ด์ƒ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  ๋ฐฉ๊ธˆ ์ถ”๊ฐ€ํ•œ WebMVCConfig ํด๋ž˜์Šค์— ๋ณ„๋„๋กœ ViewResolver๋ฅผ ๋“ฑ๋กํ•˜์—ฌ ์‚ฌ์šฉํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค.

	@Bean
	public ViewResolver getViewResolver(){
	    InternalResourceViewResolver resolver = new InternalResourceViewResolver();
	    resolver.setPrefix("/WEB-INF/views/");
	    resolver.setSuffix(".jsp");
	    resolver.setViewClass(JstlView.class);
	    return resolver;
	}

 

์•„๋ž˜๋Š” ์™„์„ฑ๋œ WebMVCConfig ํด๋ž˜์Šค์˜ ์ฝ”๋“œ์ž…๋‹ˆ๋‹ค.

package com.example.demo.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;

@Configuration
@EnableWebMvc
public class WebMVCConfig implements WebMvcConfigurer {

	@Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
		if (!registry.hasMappingForPattern("/webjars/**")) {
	        registry.addResourceHandler("/webjars/**").addResourceLocations(
	                "classpath:/META-INF/resources/webjars/");
	    }
	    if (!registry.hasMappingForPattern("/**")) {
	        registry.addResourceHandler("/**").addResourceLocations(
	        		new String[] {"classpath:/static/script", "classpath:/static/style"});
	    }
    }
	
	@Bean
	public ViewResolver getViewResolver(){
	    InternalResourceViewResolver resolver = new InternalResourceViewResolver();
	    resolver.setPrefix("/WEB-INF/views/");
	    resolver.setSuffix(".jsp");
	    resolver.setViewClass(JstlView.class);
	    return resolver;
	}
}

 

์ž, ๋ชจ๋“  ์ค€๋น„๋Š” ์™„๋ฃŒ๊ฐ€ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์ด์ œ pom.xml์— ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ถ€ํŠธ์ŠคํŠธ๋žฉ ์˜์กด์„ฑ์„ ์ถ”๊ฐ€ํ•ด์ค๋‹ˆ๋‹ค.

		<!-- Bootstrap CSS -->
		<dependency>
			<groupId>org.webjars</groupId>
			<artifactId>bootstrap</artifactId>
			<version>4.5.0</version>
		</dependency>

 

๊ทธ๋ฆฌ๊ณ  test.jsp ํŒŒ์ผ์„ ์—ด์–ด ์•„๋ž˜์™€ ๊ฐ™์ด ๋ถ€ํŠธ์ŠคํŠธ๋žฉ์„ ๋งํฌ๊ฑธ์–ด์ค๋‹ˆ๋‹ค.

<link rel="stylesheet" href="/webjars/bootstrap/4.5.0/css/bootstrap.min.css" />

 

๋งŒ์•ฝ ๋ถ€ํŠธ์ŠคํŠธ๋žฉ์ด ์•„๋‹Œ jQuery์™€ ๊ฐ™์€ javascript ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜๊ณ ์ž ํ•  ๊ฒฝ์šฐ ์˜์กด์„ฑ ์ถ”๊ฐ€๋Š” ๋™์ผํ•˜๊ฒŒ, jspํŒŒ์ผ์— ๋งํฌ๋ฅผ ๊ฑธ์–ด์ค„ ๋•Œ์—๋Š” ์•„๋ž˜์™€ ๊ฐ™์€ ํ˜•ํƒœ๋กœ ํ•ด์ฃผ์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

<script src="/webjars/jqeury/3.5.1/js/jqeury.min.js"></script>

 

์ž, ์—ฌ๊ธฐ๊นŒ์ง€ ์™„๋ฃŒ๋˜๋ฉด ๋ชจ๋“  ์ค€๋น„๋Š” ๋๋‚œ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด์ œ ๋ถ€ํŠธ์ŠคํŠธ๋žฉ์—์„œ ์ œ๊ณตํ•˜๋Š” ๊ธฐ๋Šฅ์„ ๋งˆ์Œ๊ป ์‚ฌ์šฉํ•˜์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

์ €๋Š” ์ง€๋‚œ ์‹œ๊ฐ„์— ์ตœ์ข…๊ฒฐ๊ณผ๋ฌผ์—์„œ ๋ฒ„ํŠผ์„ ํ•˜๋‚˜ ์ถ”๊ฐ€ํ•ด๋ณด๋Š” ์ •๋„๋กœ ์ด๋ฒˆ ํฌ์ŠคํŒ…์„ ๋งˆ์น˜๊ฒ ์Šต๋‹ˆ๋‹ค.

test.jsp์˜ body์— ๋‹ค์Œ ํ•œ ์ค„์„ ์ถ”๊ฐ€ํ–ˆ์Šต๋‹ˆ๋‹ค.

<button type="button" class="btn btn-danger btn-lg">๋ฒ„ํŠผ</button>

 

๊ทธ๋ฆฌ๊ณ  ๊ฒฐ๊ณผ๋ฅผ ํ™•์ธํ•ด๋ณด๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด ๋นจ๊ฐ„์ƒ‰ ๋ฒ„ํŠผ์ด ์ถ”๊ฐ€๋œ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

์ด์ƒ์œผ๋กœ Eclipse + SpringBoot + JSP ๊ฐœ๋ฐœํ™˜๊ฒฝ ์„ธํŒ…ํ•˜๊ธฐ 2ํƒ„ ํฌ์ŠคํŒ…์„ ๋งˆ์นฉ๋‹ˆ๋‹ค.

์˜ค๋Š˜๋„ ์ฆ๊ฒ๊ณ  ํ–‰๋ณตํ•œ ์ฝ”๋”ฉ์„ ํ•˜๋Š” ์ผ€์ด์น˜์˜€์Šต๋‹ˆ๋‹ค~