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

[IntelliJ] Spring Web MVC ํ”„๋กœ์ ํŠธ ์ƒ์„ฑ ๋ฐ ์„ธํŒ… - ์ž๋ฐ” ์„ค์ • ์‚ฌ์šฉ

by Leica 2020. 5. 1.
๋ฐ˜์‘ํ˜•

[IntelliJ] Spring Web MVC ํ”„๋กœ์ ํŠธ ์ƒ์„ฑ ๋ฐ ์„ธํŒ… - ์ž๋ฐ” ์„ค์ • ์‚ฌ์šฉ

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

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

Create from archetype ์ฒดํฌ, maven-archetype-webapp์„ ์„ ํƒํ•˜๊ณ  Next ํด๋ฆญ

 

ํ”„๋กœ์ ํŠธ ์ •๋ณด ์ž…๋ ฅํ•˜๊ณ  Next ํด๋ฆญ

 

์‚ฌ์šฉํ•  Maven ์„ ํƒ ํ›„ Finish ํด๋ฆญ

 

๊ธฐ๋ณธ์œผ๋กœ ์ƒ์„ฑ๋˜๋Š” pom.xml

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.atoz_develop</groupId>
  <artifactId>project03</artifactId>
  <version>1.0-SNAPSHOT</version> 
  <packaging>war</packaging>

  <name>project03 Maven Webapp</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <build>
    <finalName>project03</finalName>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
      <plugins>
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.1.0</version>
        </plugin>
        <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.8.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.22.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-war-plugin</artifactId>
          <version>3.2.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.5.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.8.2</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>

maven-archetype-webapp์œผ๋กœ ํ”„๋กœ์ ํŠธ ์ƒ์„ฑ ์‹œ ๊ธฐ๋ณธ์œผ๋กœ ์ƒ์„ฑ๋˜๋Š” pom.xml์€ ์œ„์™€ ๊ฐ™๋‹ค.

 

2. ์ž๋ฐ” ๋ฒ„์ „ ์„ค์ •

๋จผ์ € ์ž๋ฐ” ์ปดํŒŒ์ผ๋Ÿฌ ๋ฒ„์ „์„ ๋ณ€๊ฒฝํ•œ๋‹ค.

์—ฌ๊ธฐ์„œ๋Š” 8๋กœ ๋ณ€๊ฒฝํ•˜๋„๋ก ํ•˜๊ฒ ๋‹ค.

 

<properties>
  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  <maven.compiler.source>8</maven.compiler.source>
  <maven.compiler.target>8</maven.compiler.target>
</properties>

pom.xml์˜ <maven.compiler.source>, <maven.compiler.target>์„ 8๋กœ ๋ณ€๊ฒฝํ•œ๋‹ค.

 

IntelliJ Project Structure์—์„œ ํ”„๋กœ์ ํŠธ/๋ชจ๋“ˆ์˜ Language level์„ 8๋กœ ๋ณ€๊ฒฝํ•œ๋‹ค.

 

Preferences์—์„œ Build, Execution, Deployment - Compiler - Java Compiler ํด๋ฆญํ•˜๊ณ  ํ”„๋กœ์ ํŠธ/๋ชจ๋“ˆ์˜ Target bytecode version์„ 8๋กœ ๋ณ€๊ฒฝํ•œ๋‹ค.

์ด๋ ‡๊ฒŒ ์„ธ ๊ฐ€์ง€๋ฅผ ์„ค์ •ํ•˜๋ฉด ์ž๋ฐ” ๋ฒ„์ „ ์„ค์ •์€ ์™„๋ฃŒ๋œ๋‹ค.

 

3. ์˜์กด์„ฑ ์„ค์ •

๋‹ค์Œ์œผ๋กœ Spring MVC, Servlet/JSP API, jstl ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์˜์กด์„ฑ์„ ์ถ”๊ฐ€ํ•œ๋‹ค.

์•„๋ž˜์™€ ๊ฐ™์ด <properties>์™€ <dependencies>๋ฅผ ๋ณ€๊ฒฝํ•˜๋ฉด ๋œ๋‹ค.

์ด ์ค‘ Spring MVC๋Š” ํ•„์ˆ˜์ด๊ณ  Servlet/JSP API, jstl์€ ์„ ํƒ์‚ฌํ•ญ์ด๋‹ค.

JSP ์™ธ์˜ ๋‹ค๋ฅธ ๋ทฐ ํ…œํ”Œ๋ฆฟ์„ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด ํ•ด๋‹น ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์ถ”๊ฐ€ํ•œ๋‹ค.

<properties>
  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  <maven.compiler.source>8</maven.compiler.source>
  <maven.compiler.target>8</maven.compiler.target>
  <spring-webmvc-version>5.2.6.RELEASE</spring-webmvc-version>
  <javax.servlet-api-version>4.0.1</javax.servlet-api-version>
  <javax.servlet.jsp-api-version>2.3.3</javax.servlet.jsp-api-version>
  <javax.servlet.jsp.jstl-api-version>1.2.2</javax.servlet.jsp.jstl-api-version>
  <taglibs-standard-impl-version>1.2.5</taglibs-standard-impl-version>
  <junit-version>4.11</junit-version>
</properties>

<dependencies>
  <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>${spring-webmvc-version}</version>
  </dependency>

  <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
  <dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>${javax.servlet-api-version}</version>
    <scope>provided</scope>
  </dependency>

  <!-- https://mvnrepository.com/artifact/javax.servlet.jsp/javax.servlet.jsp-api -->
  <dependency>
    <groupId>javax.servlet.jsp</groupId>
    <artifactId>javax.servlet.jsp-api</artifactId>
    <version>${javax.servlet.jsp-api-version}</version>
    <scope>provided</scope>
  </dependency>

  <!-- https://mvnrepository.com/artifact/javax.servlet.jsp.jstl/javax.servlet.jsp.jstl-api -->
  <dependency>
    <groupId>javax.servlet.jsp.jstl</groupId>
    <artifactId>javax.servlet.jsp.jstl-api</artifactId>
    <version>${javax.servlet.jsp.jstl-api-version}</version>
  </dependency>

  <!-- https://mvnrepository.com/artifact/org.apache.taglibs/taglibs-standard-impl -->
  <dependency>
    <groupId>org.apache.taglibs</groupId>
    <artifactId>taglibs-standard-impl</artifactId>
    <version>${taglibs-standard-impl-version}</version>
  </dependency>

  <dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>${junit-version}</version>
    <scope>test</scope>
  </dependency>
</dependencies>

 

4. ์›น ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜ ์„ค์ •

XML๋กœ ์Šคํ”„๋ง MVC ์„ค์ •์„ ํ•  ๋•Œ web.xml์— ํ•œ ์„ค์ •๋“ค์€ ์ž๋ฐ”๋กœ ์„ค์ •ํ•  ๋•Œ๋Š” AbstractAnnotationConfigDispatcherServletInitializer๋ฅผ ์ƒ์†ํ•˜๋˜์ง€ WebApplicationInitializer ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.

AbstractAnnotationConfigDispatcherServletInitializer๋ฅผ ์ƒ์†ํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ๋” ์‰ฝ๊ณ  ๊ฐ„๋‹จํ•˜์ง€๋งŒ ์„ค์ •์˜ ์ž์œ ๋„๊ฐ€ ๋–จ์–ด์ง€๋Š” ํŠน์ง•์ด ์žˆ๋‹ค.

WebApplicationInitializer ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๊ธฐ์ค€์œผ๋กœ ๋ณด๊ณ  ๋’ค์—์„œ AbstractAnnotationConfigDispatcherServletInitializer๋ฅผ ์ƒ์†ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์‚ดํŽด๋ณผ ๊ฒƒ์ด๋‹ค.

 

1) WebApplicationInitializer ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” ๋ฐฉ๋ฒ•

config ํŒจํ‚ค์ง€์™€ WebConfig, WebContext, RootContext ํด๋ž˜์Šค๋ฅผ ์ƒˆ๋กœ ์ƒ์„ฑํ•œ๋‹ค.

๊ฐ๊ฐ web.xml, web context config, root context config ์—ญํ• ์„ ํ•œ๋‹ค.

๋จผ์ € WebConfig ํด๋ž˜์Šค๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ž‘์„ฑํ•œ๋‹ค.

 

WebConfig

public class WebConfig implements WebApplicationInitializer {

    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {

    }
}

 

โ‘  DispatcherServlet ์„ค์ •

public class WebConfig implements WebApplicationInitializer {

    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {

        // DispatcherServlet ์„ค์ • -s
        // 1. DispatcherServlet WebApplicationContext ๊ฐ์ฒด ์ƒ์„ฑ
        AnnotationConfigWebApplicationContext webContext = new AnnotationConfigWebApplicationContext();
        webContext.register(WebContext.class);

        // 2. DispatcherServlet ๊ฐ์ฒด ์ƒ์„ฑ ๋ฐ ์ถ”๊ฐ€
        DispatcherServlet dispatcherServlet = new DispatcherServlet(webContext);
        ServletRegistration.Dynamic servlet = servletContext.addServlet("dispatcher", dispatcherServlet);

        // 3. ์„œ๋ธ”๋ฆฟ ๋งคํ•‘ ๋ฐ ๋ถ€๊ฐ€ ์„ค์ •
        servlet.addMapping("/");
        servlet.setLoadOnStartup(1);
        // DispatcherServlet ์„ค์ • -e
    }
}

 

์ด ์ฝ”๋“œ๋Š” web.xml์˜ ๋‹ค์Œ ์„ค์ •๊ณผ ๋™์ผํ•œ ๋‚ด์šฉ์ด๋‹ค.

<servlet>
  <servlet-name>appServlet</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <init-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/config/webmvc-context.xml</param-value>
  </init-param>
  <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
  <servlet-name>appServlet</servlet-name>
  <url-pattern>/</url-pattern>
</servlet-mapping>

 

โ‘ก Root Context Config ์„ค์ •

 

public class WebConfig implements WebApplicationInitializer {

    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {

        // DispatcherServlet ์„ค์ • -s
        // ...
        // DispatcherServlet ์„ค์ • -e
        
        // Root Context Config ์„ค์ • -s
        AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
        rootContext.register(RootContext.class);

        ContextLoaderListener listener = new ContextLoaderListener(rootContext);
        servletContext.addListener(listener);
        // Root Context Config ์„ค์ • -e
    }
}

 

์ด ์ฝ”๋“œ๋Š” web.xml์˜ ๋‹ค์Œ ์„ค์ •๊ณผ ๋™์ผํ•œ ๋‚ด์šฉ์ด๋‹ค.

<context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>/WEB-INF/config/root-context.xml</param-value>
</context-param>

<listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

 

โ‘ข Filter ์„ค์ •

public class WebConfig implements WebApplicationInitializer {

    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {

        // DispatcherServlet ์„ค์ • -s
        // ...
        // DispatcherServlet ์„ค์ • -e

        // Root Context Config ์„ค์ • -s
        // ...
        // Root Context Config ์„ค์ • -e

        // Filter ์„ค์ • -s
        FilterRegistration.Dynamic filter = servletContext.addFilter("encodingFilter", CharacterEncodingFilter.class);
        filter.setInitParameter("encoding", "UTF-8");
        filter.addMappingForServletNames(null, false, "dispatcher");
        // Filter ์„ค์ • -e
    }
}

 

์ด ์ฝ”๋“œ๋Š” web.xml์˜ ๋‹ค์Œ ์„ค์ •๊ณผ ๋™์ผํ•œ ๋‚ด์šฉ์ด๋‹ค.

<filter>
  <filter-name>encodingFilter</filter-name>
  <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  <init-param>
    <param-name>encoding</param-name>
    <param-value>UTF-8</param-value>
  </init-param>
  <init-param>
    <param-name>forceEncoding</param-name>
    <param-value>true</param-value>
  </init-param>
</filter>

<filter-mapping>
  <filter-name>encodingFilter</filter-name>
  <url-pattern>/*</url-pattern>
</filter-mapping>

 

์ตœ์ข… WebConfig ํด๋ž˜์Šค

public class WebConfig implements WebApplicationInitializer {

    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {

        // DispatcherServlet ์„ค์ • -s
        // 1. DispatcherServlet WebApplicationContext ๊ฐ์ฒด ์ƒ์„ฑ
        AnnotationConfigWebApplicationContext webContext = new AnnotationConfigWebApplicationContext();
        webContext.register(WebContext.class);

        // 2. DispatcherServlet ๊ฐ์ฒด ์ƒ์„ฑ ๋ฐ ์ถ”๊ฐ€
        DispatcherServlet dispatcherServlet = new DispatcherServlet(webContext);
        ServletRegistration.Dynamic servlet = servletContext.addServlet("dispatcher", dispatcherServlet);

        // 3. ์„œ๋ธ”๋ฆฟ ๋งคํ•‘ ๋ฐ ๋ถ€๊ฐ€ ์„ค์ •
        servlet.addMapping("/");
        servlet.setLoadOnStartup(1);
        // DispatcherServlet ์„ค์ • -e

        // Root Context Config ์„ค์ • -s
        AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
        rootContext.register(RootContext.class);

        ContextLoaderListener listener = new ContextLoaderListener(rootContext);
        servletContext.addListener(listener);
        // Root Context Config ์„ค์ • -e

        // Filter ์„ค์ • -s
        FilterRegistration.Dynamic filter = servletContext.addFilter("encodingFilter", CharacterEncodingFilter.class);
        filter.setInitParameter("encoding", "UTF-8");
        filter.addMappingForServletNames(null, false, "dispatcher");
        // Filter ์„ค์ • -e
    }
}

 

2) AbstractAnnotationConfigDispatcherServletInitializer๋ฅผ ์ƒ์†ํ•˜๋Š” ๋ฐฉ๋ฒ•

์ง€๊ธˆ๊นŒ์ง€ WebApplicationInitializer ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ํ†ตํ•ด ์„ค์ •ํ•œ ๊ฒƒ์„ AbstractAnnotationConfigDispatcherServletInitializer๋ฅผ ์ƒ์†ํ•˜๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ ์„ค์ •ํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

public class WebConfig extends AbstractAnnotationConfigDispatcherServletInitializer {

	// DispatcherServlet ์„ค์ •
	@Override
	protected String[] getServletMappings() {
		return new String[] {"/"};
	}

	// Web Context Config ์„ค์ •
	@Override
	protected Class<?>[] getServletConfigClasses() {
		return new Class[] {WebContext.class};
	}

	// Root Context Config ์„ค์ •
	@Override
	protected Class<?>[] getRootConfigClasses() {
		return new Class[] {RootContext.class};
	}

	// Filter ์„ค์ •
	@Override
	protected Filter[] getServletFilters() {
		CharacterEncodingFilter encodingFilter = new CharacterEncodingFilter();
		encodingFilter.setEncoding("UTF-8");
		return new Filter[] {encodingFilter};
	}
}

๋” ์‰ฝ๊ณ  ๊ฐ„๋‹จํ•˜์ง€๋งŒ ์„ค์ •์˜ ์ž์œ ๋„๊ฐ€ ๋–จ์–ด์ง„๋‹ค.

 

5. Web Context ์„ค์ •

1) Controller ์„ค์ •

WebContext ํด๋ž˜์Šค๋ฅผ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ž‘์„ฑํ•œ๋‹ค.

 

WebContext

@Configuration
@EnableWebMvc
@ComponentScan("com.atoz_develop.project04.controller")
public class WebContext implements WebMvcConfigurer {
    
}

 

์ด ์ฝ”๋“œ๋Š” web.xml์˜ ๋‹ค์Œ ์„ค์ •๊ณผ ์œ ์‚ฌํ•œ ๋‚ด์šฉ์ด๋‹ค.

<!-- @Controller ์• ๋…ธํ…Œ์ด์…˜ ์‚ฌ์šฉ -->
<annotation-driven/>

<!-- Component Scan ํŒจํ‚ค์ง€ ์„ค์ • -->
<context:component-scan base-package="com.atoz_develop.project03.controller"/>

 

2) ViewResolver ์„ค์ •

@Configuration
@EnableWebMvc
@ComponentScan("com.atoz_develop.project03.controller")
public class WebContext implements WebMvcConfigurer {

    @Override
    public void configureViewResolvers(ViewResolverRegistry registry) {
        WebMvcConfigurer.super.configureViewResolvers(registry);
        // ViewResolver ์„ค์ •
        registry.jsp("/WEB-INF/views/", ".jsp");
    }
}

configureViewResolvers()๋ฅผ overrideํ•˜์—ฌ ViewResolver๋ฅผ ์„ค์ •ํ•œ๋‹ค.

 

์ด ์ฝ”๋“œ๋Š” xml์˜ ๋‹ค์Œ ์„ค์ •๊ณผ ์œ ์‚ฌํ•œ ๋‚ด์šฉ์ด๋‹ค.

<!-- ๋ทฐ prefix, suffix ์„ค์ • -->
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <beans:property name="prefix" value="/WEB-INF/views/"/>
    <beans:property name="suffix" value=".jsp"/>
</beans:bean>

 

3) Static Resources ์„ค์ •

@Configuration
@EnableWebMvc
@ComponentScan("com.atoz_develop.project03.controller")
public class WebContext implements WebMvcConfigurer {

    //...

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        WebMvcConfigurer.super.addResourceHandlers(registry);
        // Static Resources ์„ค์ •
        registry.addResourceHandler("/**").addResourceLocations("/resources/");
    }
}

addResourceHandlers()๋ฅผ overrideํ•˜์—ฌ ์ •์  ๋ฆฌ์†Œ์Šค๋ฅผ ์„ค์ •ํ•œ๋‹ค.

 

์ด ์ฝ”๋“œ๋Š” xml์˜ ๋‹ค์Œ ์„ค์ •๊ณผ ์œ ์‚ฌํ•œ ๋‚ด์šฉ์ด๋‹ค.

<!-- ์ •์  ๋ฆฌ์†Œ์Šค ๊ฒฝ๋กœ ์„ค์ • -->
<resources mapping="/**" location="/resources/"/>

 

์ตœ์ข… WebContext ํด๋ž˜์Šค

@Configuration
@EnableWebMvc
@ComponentScan("com.atoz_develop.project03.controller")
public class WebContext implements WebMvcConfigurer {

    @Override
    public void configureViewResolvers(ViewResolverRegistry registry) {
        WebMvcConfigurer.super.configureViewResolvers(registry);
        // ViewResolver ์„ค์ •
        registry.jsp("/WEB-INF/views/", ".jsp");
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        WebMvcConfigurer.super.addResourceHandlers(registry);
        // Static Resources ์„ค์ •
        registry.addResourceHandler("/**").addResourceLocations("/resources/");
    }
}

 

6. Root Context ์„ค์ •

Root Context ์„ค์ • ํด๋ž˜์Šค๋Š” ์ดˆ๊ธฐ์— ์„ค์ •ํ•  ๊ฒƒ์ด ์—†๋‹ค.

@Configuration์„ ๋ถ™์—ฌ์„œ ์„ค์ • ํด๋ž˜์Šค๋กœ ์„ ์–ธํ•˜๊ณ  ์ถ”ํ›„ ํ”„๋กœ์ ํŠธ๋ฅผ ์ง„ํ–‰ํ•˜๋ฉฐ ํ•„์š”ํ•œ ๋นˆ์„ ๋“ฑ๋กํ•˜๋ฉด ๋œ๋‹ค.

 

RootContext

@Configuration
public class RootContext {
    
}

 

7. ํ…Œ์ŠคํŠธ

์„ค์ •์ด ์ œ๋Œ€๋กœ ๋๋Š”์ง€ ํ…Œ์ŠคํŠธํ•ด๋ณด์ž.

 

์œ„ ๊ทธ๋ฆผ์—์„œ ์„ ํƒ๋˜์–ด ์žˆ๋Š” ํŒŒ์ผ๊ณผ ๋””๋ ‰ํ† ๋ฆฌ๋ฅผ ์ƒˆ๋กœ ์ƒ์„ฑํ•œ๋‹ค.

resources ์•ˆ์—๋Š” ํ…Œ์ŠคํŠธ์šฉ ์ด๋ฏธ์ง€๋ฅผ ์•„๋ฌด๊ฑฐ๋‚˜ ํ•˜๋‚˜ ๋„ฃ์–ด๋‘”๋‹ค.

 

HelloController

package com.atoz_develop.project03.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class HelloController {

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

 

hello.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Hello Spring Web MVC</title>
</head>
<body>
<h1>Hello Spring Web MVC</h1>
<img src="spring.png"/>
</body>
</html>

 

๐Ÿ–ฅ ์‹คํ–‰ ๊ฒฐ๊ณผ

์œ„์™€ ๊ฐ™์ด /hello๋กœ ์š”์ฒญํ–ˆ์„ ๋•Œ ์‹คํ–‰์ด ์ž˜ ๋˜๊ณ  ์ด๋ฏธ์ง€๊ฐ€ ์ž˜ ๋œจ๋ฉด ๋ชจ๋“  ์„ค์ •์ด ์ •์ƒ์ ์œผ๋กœ ์™„๋ฃŒ๋œ ๊ฒƒ์ด๋‹ค.

 

๊ด€๋ จ ๊ธ€

- [IntelliJ] Spring Web MVC ํ”„๋กœ์ ํŠธ ์ƒ์„ฑ ๋ฐ ์„ธํŒ… - XML ์„ค์ • ์‚ฌ์šฉ

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€