초보자를 위한 잠자리엑셀 강좌에 방문하신 것을 진심으로 환영합니다. 잠자리엑셀은 컴퓨터자격증 중 정보기술자격증(ITQ)에 대한 강좌입니다.

새 프로젝트 만들기

1. 이클립스의 메뉴에서 [File-New-Other]을 클릭합니다.

2. Select a wizard 창에서 Dynamic Web Project을 선택하고 [Next] 버튼을 클릭합니다.

3. 다음의 프로젝트명을 입력합니다.

  • Project name : spring-3-mvc-hello

스프링 라이브러리 설정하기

1. WebContent-WEB-INF-lib 폴더를 삭제합니다.

2. 기존 라이브러 폴더를 복사합니다.

web.xml 작성하기

다음의 내용을 참고하여 web.xml을 작성합니다.

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	version="2.5">

	<filter>
		<filter-name>CharacterEncodingFilter</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>CharacterEncodingFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>

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

	<servlet>
		<servlet-name>spring-3-mvc-hello</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<load-on-startup>1</load-on-startup>
	</servlet>

	<servlet-mapping>
		<servlet-name>spring-3-mvc-hello</servlet-name>
		<url-pattern>*.html</url-pattern>
	</servlet-mapping>

</web-app>

스프링 설정파일 작성하기

다음의 내용을 참고하여 spring-3-mvc-hello-servlet.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:p="http://www.springframework.org/schema/p"
	xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
	http://www.springframework.org/schema/mvc
	http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
	http://www.springframework.org/schema/context
	http://www.springframework.org/schema/context/spring-context-3.0.xsd">

	<mvc:annotation-driven />

	<!-- HandlerMapping -->
	<bean
		class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping" />

	<context:component-scan base-package="com.jamjalee.spring" />

	<!-- ViewResolver -->
	<bean 
	  id="internalResourceViewResolver"
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="viewClass">
			<value>org.springframework.web.servlet.view.JstlView</value>
		</property>
		<property name="prefix">
			<value>/WEB-INF/jsp/</value>
		</property>
		<property name="suffix">
			<value>.jsp</value>
		</property>
	</bean>
</beans>

어플리케이션 컨텍스트 파일 작성하기

다음의 내용을 참고하여 applicationContext.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:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
	http://www.springframework.org/schema/context
	http://www.springframework.org/schema/context/spring-context-3.0.xsd
	http://www.springframework.org/schema/aop
	http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
	http://www.springframework.org/schema/tx
	http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

	<bean 
	  id="dataSource"
		class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<!-- JDBC 드라이버 클래스명을 설정  -->
		<property name="driverClassName">
			<value>oracle.jdbc.driver.OracleDriver</value>
		</property>
		<!-- JDBC 접속 문자열 설정 -->
		<property name="url">
			<value>jdbc:oracle:thin:@localhost:1521:orcl</value>
		</property>
		<!-- 오라클 유저 ID 설정 -->
		<property name="username">
			<value>tester1</value>
		</property>
		<!-- 오라클 패스워드 설정 -->
		<property name="password">
			<value>1234</value>
		</property>
	</bean>
</beans>

컨트롤러 클래스 작성하기

다음의 내용을 참고하여 컨트롤러 클래스 작성합니다.

package com.jamjalee.spring;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class HelloController {
  @RequestMapping
  public ModelAndView hello() {
    ModelAndView modelAndView = 
        new ModelAndView("hello");
    return modelAndView;
  }

  @RequestMapping
  public ModelAndView welcome() {
    System.out.println("welcome() 메소드시작");
    ModelAndView modelAndView = new ModelAndView("welcome");
    modelAndView.addObject("message", "Hello,Jamjalee");
    System.out.println("welcome() 메소드종료");

    return modelAndView;
  }
  
} // End of class HelloController

웹 페이지 작성하기

다음의 내용을 참고하여 웹 페이지 작성합니다.

<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
		<title>Hello,Spring Web MVC</title>
	</head>
	<body>
		<h1>Hello,Spring Web MVC</h1>	
	</body>
</html>
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
		<title>Insert title here</title>
	</head>
	<body>
		<h1>${message}</h1>	
	</body>
</html>

<저작권자 ⓒ잠자리(jamjalee.com) 무단 전재-재배포 금지>