본문 바로가기
web/Spring

[spring기초] springMVC프로젝트 생성

by 몽구스_ 2021. 1. 20.
728x90

 

 

Spring MVC

 

 

 

 

1. pom.xml 라이브러리 추가


Configure > Convert Maven하면 pom.xml자동 생성됨

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

 

 

 

 

 

 

2. web.xml 채우기


Dynamic Web Project만들때 next누르고 체크박스 표시하면 생성됨.

<context-param>
   <param-name>contextClass</param-name>
   <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
</context-param>
  
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  
  <servlet>
    <servlet-name>mvc</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>springMVC.config.WebMvcContextConfiguration</param-value>
    </init-param>
  </servlet>
  
  <servlet-mapping>
    <servlet-name>mvc</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

 

 

 

 

 

 

3. 컨트롤러


WebMvcContextConfiguration

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = {"springMVC.controller"})
public class WebMvcContextConfiguration implements WebMvcConfigurer {
	@Bean
	public InternalResourceViewResolver getInternalResourceViewResolver() {
		InternalResourceViewResolver resolver = new InternalResourceViewResolver();
		resolver.setPrefix("/WEB-INF/views/");
		resolver.setSuffix(".jsp");
		return resolver;
	}
	
}

resolver사용하여 prefix와 suffix세팅해줌.

 

 

 

HelloController

@Controller
public class HelloController {
	
	@RequestMapping("/hello")
	public String hello() {
		return "hello";
	}
	
	@GetMapping("/test")
	public String test() {
		return "test";
	}
	
}

 

 

 

 

4. 컨트롤러는 수행하면서 뷰의 위치와 확장자 얻어옴


 

 

 

 

5. 뷰 보여줌


 

+추가

web.xml 사용하지 않는 방법

 

MvcConfig

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "exam.spring.board.controller")
public class MvcConfig implements WebMvcConfigurer{

	@Override
	public void configureViewResolvers(ViewResolverRegistry registry) {
		registry.jsp("/WEB-INF/views/", ".jsp");
	}

}

Config에서 prefix와 suffix를 한번에 설정해준다.

컨트롤러는 그대로 하고 WebAppInitializer클래스를 추가한다.

 

 

WebAppInitializer

public class WebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

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

		return null;
	}

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

		return new Class<?>[] {MvcConfig.class};
	}

	@Override
	protected String[] getServletMappings() {

		return new String[] {"/"};
	}


}

 

 

 

 

 

'web > Spring' 카테고리의 다른 글

톰캣 서버 재배포하기 (MobaXterm)  (0) 2021.05.13
ajax의 개념, 사용이유, 사용방법 정리  (0) 2021.01.27

댓글