๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Spring

[IntelliJ] ์Šคํ”„๋ง ๋ถ€ํŠธ ์—†์ด ์Šคํ”„๋ง ์›น MVC ํ”„๋กœ์ ํŠธ ๋งŒ๋“ค๊ณ  ์„ค์ •ํ•˜๊ธฐ

by Leica 2020. 3. 17.
๋ฐ˜์‘ํ˜•

[IntelliJ] ์Šคํ”„๋ง ๋ถ€ํŠธ ์—†์ด ์Šคํ”„๋ง ์›น MVC ํ”„๋กœ์ ํŠธ ๋งŒ๋“ค๊ณ  ์„ค์ •ํ•˜๊ธฐ

โ€ป ์‚ฌ์šฉ IDE : IntelliJ IDEA 2019.3 Ultimate

 

1. ์ƒˆ ํ”„๋กœ์ ํŠธ ์ƒ์„ฑ

์‚ฌ์šฉํ•˜๋Š” ๋นŒ๋“œํˆด(Maven, Gradle) ๊ธฐ๋ฐ˜์˜ ํ”„๋กœ์ ํŠธ๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.

 

์ƒˆ ํ”„๋กœ์ ํŠธ๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.

 

1) ์‚ฌ์šฉํ•  ๋นŒ๋“œํˆด ์„ ํƒ - ์ด ๊ธ€์—์„œ๋Š” Maven์„ ์‚ฌ์šฉํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

2) ์‚ฌ์šฉํ•  JDK ์„ ํƒ

3) Next ํด๋ฆญ

 

ํ”„๋กœ์ ํŠธ ์ •๋ณด๋ฅผ ์ž…๋ ฅํ•˜๊ณ  Finish๋ฅผ ํด๋ฆญํ•ด์„œ ํ”„๋กœ์ ํŠธ ์ƒ์„ฑ์„ ์™„๋ฃŒํ•œ๋‹ค.

 

2. ํ”„๋กœ์ ํŠธ ์„ธํŒ…

1. Java Compiler ๋ฒ„์ „ ๋ณ€๊ฒฝ
2. ํ”„๋กœ์ ํŠธ์— Web Facet ์ถ”๊ฐ€
3. war packaging ์„ค์ •

 

Maven ํ”„๋กœ์ ํŠธ๋ฅผ ์ƒ์„ฑํ•˜๋ฉด ๊ธฐ๋ณธ Java Compiler ๋ฒ„์ „์ด 1.5๋กœ ๋˜์–ด์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๋ณ€๊ฒฝ์ด ํ•„์š”ํ•˜๋‹ค.

 

Settings - Build, Execution, Deployment - Compiler - Java Compiler์—์„œ Target bytecode version์„ ์‚ฌ์šฉํ•˜๋Š” JDK ๋ฒ„์ „์œผ๋กœ ๋ฐ”๊พผ๋‹ค.

 

๊ทธ๋ฆฌ๊ณ  pom.xml์— ์ž๋ฐ” ๋ฒ„์ „์„ ๋ช…์‹œํ•œ๋‹ค.

 

pom.xml

<properties>
    <maven.compiler.source>11</maven.compiler.source>
    <maven.compiler.target>11</maven.compiler.target>
</properties>

 

๋‹ค์Œ์œผ๋กœ ํ”„๋กœ์ ํŠธ์— Web Facet์„ ์ถ”๊ฐ€ํ•œ๋‹ค.

 

ํ”„๋กœ์ ํŠธ์—์„œ ๋งˆ์šฐ์Šค ์šฐ์ธก ๋ฒ„ํŠผ - Add Framework Support... ๋ฅผ ํด๋ฆญํ•œ๋‹ค.

 

๋ชฉ๋ก์—์„œ Web Application์— ์ฒดํฌ, Create web.xml์— ์ฒดํฌํ•˜๊ณ  OK๋ฅผ ํด๋ฆญํ•œ๋‹ค.

 

web framework ์ถ”๊ฐ€ ํ›„ ๋ณ€๊ฒฝ๋œ ํ”„๋กœ์ ํŠธ ๊ตฌ์กฐ

Maven ํ”„๋กœ์ ํŠธ๋Š” web resource directory๊ฐ€ ๊ธฐ๋ณธ์ ์œผ๋กœ src/main/webapp์œผ๋กœ ์žกํžˆ๋ฏ€๋กœ ์ƒ์„ฑ๋œ web ๋””๋ ‰ํ† ๋ฆฌ๋ฅผ src/main ๋ฐ‘์œผ๋กœ ์˜ฎ๊ธฐ๊ณ  ์ด๋ฆ„์„ webapp์œผ๋กœ ๋ณ€๊ฒฝํ•œ๋‹ค.

 

์ตœ์ข… ํ”„๋กœ์ ํŠธ ๊ตฌ์กฐ

 

๋งˆ์ง€๋ง‰์œผ๋กœ pom.xml์— <packaging>war</packaging>์„ ์ถ”๊ฐ€ํ•œ๋‹ค.

 

pom.xml

...
<groupId>com.atoz_develop</groupId>
<artifactId>spring-mvc-sample</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>

<properties>
    <maven.compiler.source>11</maven.compiler.source>
    <maven.compiler.target>11</maven.compiler.target>
</properties>
...

 

3. ์˜์กด์„ฑ(Dependency) ์„ค์ •

โš™๏ธ ํ•„์š” ์˜์กด์„ฑ

- servlet-api(jsp๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค๊ณ  ๊ฐ€์ •)
- spring-webmvc

 

pom.xml

<dependencies>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>4.0.1</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>5.1.3.RELEASE</version>
    </dependency>
</dependencies>

 

4. ์Šคํ”„๋ง ์›น MVC ์„ค์ •

1. Configuration ์„ค์ •
- @EnableWebMvc

2. DispatcherServlet ์„ค์ •
- web.xml ๋˜๋Š” WebApplicationInitializer

3. DispatcherServlet ์ปค์Šคํ„ฐ๋งˆ์ด์ง• ์„ค์ •
- WebMvcConfigurer

 

1) Configuration ์„ค์ •

Configuration์šฉ ํด๋ž˜์Šค๋ฅผ ์ƒˆ๋กœ ์ƒ์„ฑํ•œ๋‹ค.

 

@Configuration
@EnableWebMvc
@ComponentScan
public class WebConfiguration {
    
}

์ƒ์„ฑํ•œ ํด๋ž˜์Šค์— @configuration, @EnableWebMvc ์• ๋…ธํ…Œ์ด์…˜์„ ๋ถ™์ธ๋‹ค.

์ด ๋‘ ๊ฐœ๊ฐ€ ์Šคํ”„๋ง ์›น MVC์˜ ์„ค์ •์— ํ•„์ˆ˜์ ์ธ ์• ๋…ธํ…Œ์ด์…˜์ด๋‹ค.

@ComponentScan์€ ํŽธ์˜์ƒ ๋นˆ ์„ค์ • ํŒŒ์ผ์„ ๋”ฐ๋กœ ๋งŒ๋“ค์ง€ ์•Š๊ธฐ ์œ„ํ•ด ๊ฐ™์ด ๋ถ™์—ฌ์ฃผ์—ˆ๋‹ค.

 

2) DispatcherServlet ์„ค์ •

์Šคํ”„๋ง MVC์˜ ํ•ต์‹ฌ์€ DispatcherServlet์ด๋‹ค.

์Šคํ”„๋ง MVC ์›น ์„œ๋น„์Šค๋Š” ํด๋ผ์ด์–ธํŠธ์˜ ์š”์ฒญ์„ DispatcherServlet์ด ๊ฐ€์žฅ ์•ž๋‹จ์—์„œ ์ œ์ผ ๋จผ์ € ๋ฐ›๊ธฐ ๋•Œ๋ฌธ์— DispatcherServlet์„ ์„ค์ •ํ•ด ์ฃผ์–ด์•ผ ํ•œ๋‹ค.

DispatcherServlet์€ ๋‘ ๊ฐ€์ง€ ๋ฐฉ๋ฒ•์œผ๋กœ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ web.xml๊ณผ WebApplicationInitializer ์ธํ„ฐํŽ˜์ด์Šค์˜ ๊ตฌํ˜„์ฒด์—์„œ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.

๋‘˜ ์ค‘ ํŽธํ•œ ๋ฐฉ๋ฒ•์„ ํƒํ•˜๋ฉด ๋œ๋‹ค.

 

(1) web.xml์— DispatcherServlet ์„ค์ •ํ•˜๊ธฐ

web.xml - ์• ๋…ธํ…Œ์ด์…˜ ์Šคํ”„๋ง ์„ค์ • ์‚ฌ์šฉ ์‹œ

<servlet>
    <servlet-name>app</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextClass</param-name>
        <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
    </init-param>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>com.atoz_develop.WebConfiguration</param-value>
    </init-param>
</servlet>

<servlet-mapping>
    <servlet-name>app</servlet-name>
    <url-pattern>/app/*</url-pattern>
</servlet-mapping>

 

<servlet>์— org.springframework.web.servlet.DispatcherServlet์„ ์„ ์–ธํ•˜๊ณ  <init-param>์— DispatcherServlet์ด ์• ๋…ธํ…Œ์ด์…˜ ๊ธฐ๋ฐ˜์˜ ์„ค์ •์„ ์‚ฌ์šฉํ•ด์„œ ApplicationContext๋ฅผ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ๋„๋ก ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์„ค์ •ํ•œ๋‹ค.

 

์ฐธ๊ณ ๋กœ ์Šคํ”„๋ง XML ์„ค์ •์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ์—๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ž‘์„ฑํ•˜๋ฉด ๋œ๋‹ค.

 

web.xml - XML ์Šคํ”„๋ง ์„ค์ • ์‚ฌ์šฉ ์‹œ

<servlet>
    <servlet-name>app</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
    </init-param>
</servlet>

<servlet-mapping>
    <servlet-name>app</servlet-name>
    <url-pattern>/app/*</url-pattern>
</servlet-mapping>

contextConfigLocation์˜ value์— ํ”„๋กœ์ ํŠธ์˜ ์Šคํ”„๋ง XML ์„ค์ • ๊ฒฝ๋กœ๋ฅผ ์ ์–ด์•ผ ํ•œ๋‹ค.

 

(2) WebApplicationInitializer ๊ตฌํ˜„์ฒด์— DispatcherServlet ์„ค์ •ํ•˜๊ธฐ

web.xml์— DispatcherServlet์„ ์„ค์ •ํ–ˆ๋‹ค๋ฉด ์ด ๋‹จ๊ณ„๋Š” ๋„˜์–ด๊ฐ„๋‹ค. DispatcherServlet์€ ๋‘ ๊ฐ€์ง€ ๋ฐฉ๋ฒ• ์ค‘ ํ•˜๋‚˜๋ฅผ ํƒํ•˜์—ฌ ์„ค์ •ํ•˜๋ฉด ๋œ๋‹ค.

 

WebApplicationInitializer๋ฅผ ๊ตฌํ˜„ํ•  ํด๋ž˜์Šค๋ฅผ ์ƒˆ๋กœ ์ƒ์„ฑํ•œ๋‹ค.

 

public class WebApplication implements WebApplicationInitializer {

    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
    
        AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
        context.setServletContext(servletContext);    // @EnableWebMvc ์ ์šฉ์„ ์œ„ํ•œ ํ•„์ˆ˜ ์„ธํŒ…
        context.register(WebConfiguration.class);
        context.refresh();

        DispatcherServlet dispatcherServlet = new DispatcherServlet(context);
        ServletRegistration.Dynamic app = servletContext.addServlet("app", dispatcherServlet);
        app.addMapping("/app/*");
    }
}

์œ„์™€ ๊ฐ™์ด onStartup()์„ overrideํ•œ๋‹ค.

web.xml์— DispatcherServlet์„ ์„ค์ •ํ•˜๋Š” ๊ฒƒ๊ณผ ๋™์ผํ•œ ๋™์ž‘์„ ํ•˜๋Š” ์ฝ”๋“œ์ด๋‹ค.

 

3) DispatcherServlet ์ปค์Šคํ„ฐ๋งˆ์ด์ง• ์„ค์ •

WebMvcConfigurer ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ implementsํ•ด์„œ DispatcherServlet์ด ์‚ฌ์šฉํ•˜๋Š” ๋นˆ๋“ค์„ ์ปค์Šคํ„ฐ๋งˆ์ด์ง• ํ•  ์ˆ˜ ์žˆ๋‹ค.

 

์—ฌ๊ธฐ์„œ๋Š” WebConfiguration ํด๋ž˜์Šค๊ฐ€ WebMvcConfigurer ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•˜๋„๋ก ํ•˜๊ฒ ๋‹ค.

ํ•„์š”ํ•˜๋‹ค๋ฉด WebMvcConfigurer ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•  ํด๋ž˜์Šค๋ฅผ ๋ณ„๋„๋กœ ์ƒ์„ฑํ•ด๋„ ๋œ๋‹ค.

 

@Configuration
@EnableWebMvc
@ComponentScan
public class WebConfiguration implements WebMvcConfigurer {

}

 

WebMvcConfigurer ๋ฉ”์†Œ๋“œ๋“ค

 

5. ์„œ๋ฒ„ ๋ฐฐ์น˜ ์„ค์ •

์‚ฌ์šฉํ•˜๋Š” ์„œ๋ฒ„(ํ†ฐ์บฃ ๋“ฑ)์— ํ”„๋กœ์ ํŠธ๋ฅผ ์ถ”๊ฐ€ํ•œ๋‹ค.


์—ฌ๊ธฐ๊นŒ์ง€๊ฐ€ ์Šคํ”„๋ง ๋ถ€ํŠธ ์—†์ด ๊ธฐ๋ณธ์ ์ธ ์Šคํ”„๋ง ์›น MVC ํ”„๋กœ์ ํŠธ๋ฅผ ๋งŒ๋“ค๊ณ  ์„ค์ •ํ•˜๋Š” ๋ฐฉ๋ฒ•์ด๋‹ค.

์ถ”๊ฐ€๋กœ ์ด ํ”„๋กœ์ ํŠธ๋ฅผ ์‚ฌ์šฉํ•ด ๊ฐ„๋‹จํ•œ ์š”์ฒญ์„ ์ฒ˜๋ฆฌํ•˜๋Š” ์›น ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ๋งŒ๋“ค์–ด ๋ณด์ž.

 

WEB-INF์— sample.jsp๋ฅผ ๋งŒ๋“ ๋‹ค.

 

sample.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <h2>Hello Spring Web MVC!</h2>
</body>
</html>

 

SampleController๋ฅผ ๋งŒ๋“ ๋‹ค.

 

SampleController.java

@Controller
public class SampleController {

    @GetMapping("/sample")
    public String sample() {
        return "/WEB-INF/sample.jsp";
    }
}

 

์œ„์™€ ๊ฐ™์ด ์ž‘์„ฑํ•˜๊ณ  ์„œ๋ฒ„๋ฅผ ๊ธฐ๋™ํ•ด์„œ ๊ฒฐ๊ณผ๋ฅผ ํ™•์ธํ•ด๋ณธ๋‹ค.

 

๊ฒฐ๊ณผ ํ™•์ธ

 

WebMvcConfigurer๋ฅผ ํ™œ์šฉํ•ด์„œ ViewResolver๋ฅผ ์ปค์Šคํ„ฐ๋งˆ์ด์ง•ํ•˜์—ฌ '/WEB-INF/sample.jsp'์™€ ๊ฐ™์ด ๋ฆฌํ„ดํ•˜๋˜ ๋ทฐ๋ฅผ 'sample'๋กœ ๊ฐ„๋‹จํžˆ ๋ฆฌํ„ดํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•ด๋ณด์ž.

 

@Configuration
@EnableWebMvc
@ComponentScan
public class WebConfiguration implements WebMvcConfigurer {
    
    @Override
    public void configureViewResolvers(ViewResolverRegistry registry) {
        registry.jsp("/WEB-INF/", ".jsp");
    }
}

WebMvcConfigurer ๊ตฌํ˜„์ฒด์—์„œ configureViewResolvers()๋ฅผ ์œ„์™€ ๊ฐ™์ด overrideํ•œ๋‹ค.

 

@Controller
public class SampleController {

    @GetMapping("/sample")
    public String sample() {
        return "sample";
    }
}

๊ทธ๋ฆฌ๊ณ  ๋ทฐ ๋ฆฌํ„ด๋ถ€๋ฅผ ์œ„์™€ ๊ฐ™์ด ๋ณ€๊ฒฝํ•œ๋‹ค.

 

์‹คํ–‰ ๊ฒฐ๊ณผ๋Š” ์ด์ „๊ณผ ๋™์ผํ•˜๋‹ค.

 

์ตœ์ข… ํ”„๋กœ์ ํŠธ ๊ตฌ์กฐ


#์ธํ…”๋ฆฌJ ์Šคํ”„๋ง ์›น MVC ํ”„๋กœ์ ํŠธ

#Maven ์›น ํ”„๋กœ์ ํŠธ

#Maven ์Šคํ”„๋ง

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€