[Spring] Spring 프로젝트 생성

haram·2023년 6월 7일
0

문제 : STS 플러그인 설치 오류 발생 – 기존에 사용하던 이클립스에서 마켓플레이스를 통해 STS를 설치하려고 하였으나 플러그인이 기존 이클립스의 버전을 지원하지 않아 설치 오류 발생

해결 : STS설치또는 이클립스 버전 변경후 플러그인 설치(STS는 이클립스 기반의 스프링에 최적화된 IDE)
(스프링 홈페이지에서 STS3다운 최신 버전인 STS4는 legacy project생성이 불가함)
STS3다운로드 링크 - https://github.com/spring-attic/toolsuite-distribution/wiki/Spring-Tool-Suite-3 //자바 11이상

다음부터 주의 할 점 : 똑같이 했는데도 실패하면 버전을 의심해야 하고 그래도 안되면 해당 기능을 더 이상 지원하지 않기 때문에 오류가 뜨는 것인지 확인해볼 필요가 존재

  • ex)이클립스에 sts plug-in버전이 일치하지 않아 생긴 오류, spring버전과 tomcat버전이 일치하지 않아 생긴 오류

이번 포스팅에서는 spring legacy project를 생성하는 법을 정리하고자 함 STS3를 통해 간단하게 생성 가능하지만 spring프로젝트의 구조를 이해하고자 Dynamic Web Project를 통해 spring프로젝트를 생성 해 봄

Spring project 생성과정

  1. 다이나믹 웹프로젝트 생성
  2. 메이븐 프로젝트로 변환
  3. pom.xml설정 : 태그 안에 dependencies작성 spring-web, spring-webmvc를 기본적으로 설정(mvnrepository에서 복사해오기)
  4. pom.xml 저장 후 update project
  5. web.xml 설정 : 1. ApplicationContext설정 2. 리스너 등록 3. 디스패처 서블릿 생성 및 매핑 4. UTF-8필터 설정
  6. WEB-INF폴더아래 spring폴더를 새로 만들고 root-context.xml, servlet-context.xml 생성
  7. 실행 테스트를 위해 src폴더아래 com.spring.test패키지 생성 후 TestController, TestService클래스 작성
  8. WEB-INF폴더 아래 views폴더 생성 후 hello.jsp파일 작성

참고사항

  • xml 문서의 태그 구조를 정의하기 위해서는 xsd파일이 필요한데 servlet-context.xml에서는 beans태그를 사용하고 beans태그의 xsd파일을 가져오기 위해 “xmlns:xsi”, “xsi:schemaLocation”을 사용한다
  • 한번에 여러 태그를 사용하는 경우 중복되는 태그가 존재 할 가능성이 있는데 이런 경우를 해결하기 위해 “xmlns:context”와 같이 태그에 접두사를 생성한 후 “<context:component-scan>”와 같은 방식으로 사용한다

Xml스키마 관련 참조 - https://linuxism.ustd.ip.or.kr/911

코드

===============================================================
web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" version="4.0">
	
	<!-- ApplicationContext설정 -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>/WEB-INF/spring/root-context.xml</param-value>
	</context-param>
	
	<!-- 리스너 등록 -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	
	<!-- 디스패처 서블릿 생성 및 매핑 -->
	<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/spring/appServlet/servlet-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>
	
	<!-- UTF-8필터 설정 -->
	<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>
	
</web-app>
===============================================================
root-context.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd">
</beans>
===============================================================
servlet-context.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
						http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd
						http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">
	
	<!-- 어노테이션 활성화 -->
	<mvc:annotation-driven></mvc:annotation-driven>
	
	<!-- view 경로 설정 -->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/views/" />
		<property name="suffix" value=".jsp" />
	</bean>
	
	<!-- java공통패키지 스캔 -->
	<context:component-scan base-package="com.spring.test"/>
</beans>
===============================================================
TestController.java

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class TestController {
	
	@Autowired
	private TestService testservice;
	
	@RequestMapping("/hello")
	public String sysHello(Model model) {
		model.addAttribute("value", testservice.sayHello());
		return "hello";
	}
}
===============================================================
TestService.java

import org.springframework.stereotype.Service;

@Service
public class TestService {
	
	public String sayHello() {
		return "Spring_MVC";
	}
}
===============================================================

0개의 댓글