개발

스프링 MVC의 기본 구조

Handy Smurf 2020. 8. 24. 13:45

XML을 이용하는 경우

 

ex01 (Spring Legacy Project 생성)  > org.zerock.controller (패키지 지정)

master로 변경됨 

 

 

 

 

 

pom.xml

 

 

<properties> 
<java-version>1.8</java-version> 
<org.springframework-version>5.0.7.RELEASE</org.springframework-version> 
<org.aspectj-version>1.6.10</org.aspectj-version> 
<org.slf4j-version>1.6.6</org.slf4j-version> 
</properties>



<!-- 추가 작성 시작 --> 
<dependency> 
<groupId>org.springframework</groupId> 
<artifactId>spring-test</artifactId> 
<version>${org.springframework-version}</version> 
</dependency> 

<dependency> 
<groupId>org.projectlombok</groupId> 
<artifactId>lombok</artifactId> 
<version>1.18.0</version> 
<scope>provided</scope> 
</dependency> 
<!-- 추가 작성 종료 -->







<dependency> 
<groupId>javax.servlet</groupId> 
<artifactId>javax.servlet-api</artifactId> 
<version>3.1.0</version> 
<scope>provided</scope> 
</dependency>


<plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.5.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <compilerArgument>-Xlint:all</compilerArgument>
                    <showWarnings>true</showWarnings>
                    <showDeprecation>true</showDeprecation>
                </configuration>
            </plugin>

tomcat 연결

 

 

 

 

자바 설정을 이용하는 경우

jex01 (Spring Legacy Project 생성)  > org.zerock.controller (패키지 지정)

 

web.xml

servlet-context.xml

root-context.xml 

 

제거

 

    <plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-war-plugin</artifactId>
				<version>3.2.0</version>
				<configuration>
					<failOnMissingWebXml>false</failOnMissingWebXml>
				</configuration>
			</plugin>

plugin 추가 (xml을 지우면 오류가 난다. 이를 해결하기 위하여 plugin을 추가한다.)

 

 

지운 xml의 역할을 하는 RootConfig, WebConfig 클래스를 작성한다.

 

RootConfig 

@configuration 추가

 

 

package org.zerock.config;

import org.springframework.context.annotation.Configuration;

@Configuration
public class RootConfig {

}

 

WebConfig

AbstractAnnotationConfigDispatcherServletInitializer 상속(상속이 되지 않은 이유는 버전을 올리지 않았기 때문 pom에서 1.8과 5.0.7로 변경을 해주면 된다.

 

 

package org.zerock.config;


import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

public class WebConfig extends AbstractAnnotationConfigDispatcherServletInitializer {

	@Override
	protected Class<?>[] getRootConfigClasses() {
		return new Class[] { RootConfig.class };
	}

	@Override
	protected Class<?>[] getServletConfigClasses() {

		return new Class[] { ServletConfig.class };
	}

	@Override
	protected String[] getServletMappings() {
		return new String[] { "/" };
	}



}

ServletConfig

 

@EnableWebMvc

@ComponentScan

implements WebMvcConfigurer

@override * 2 작성 override 선택해 configureViewResolvers & addResouceHandlers 체크하여 만든다.

 

 

package org.zerock.config;

import java.io.IOException;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.core.io.FileSystemResource;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewResolverRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;

@EnableWebMvc
@ComponentScan(basePackages = { "org.zerock.controller", "org.zerock.exception" })
public class ServletConfig implements WebMvcConfigurer {

	@Override
	public void configureViewResolvers(ViewResolverRegistry registry) {

		InternalResourceViewResolver bean = new InternalResourceViewResolver();
		bean.setViewClass(JstlView.class);
		bean.setPrefix("/WEB-INF/views/");
		bean.setSuffix(".jsp");
		registry.viewResolver(bean);
	}

	@Override
	public void addResourceHandlers(ResourceHandlerRegistry registry) {

		registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
	}



}

 

 jex01 실행