전자정부프레임워크을 이용해서 웹 애플리케이션을 개발하면 통상적으로 MVC 모델 형태로 개발을 하게 된다.
이때 Controller 클래스(@Controller가 붙여지는 클래스)에 각종 사용자의 request 처리하는 코드가 모이게 된다.
따라서 request 관련을 살펴봐야 한다면 Controller 클래스만 보면 되므로 한 자리에서 파악할수 있는 잇점이 있어 편리하다.

그런데 Spring 프레임웤이 Controller 클래스를 인식하도록 할려면 환경을 어떻게 설정해야 하는가?
물론 전자정부프레임웤에서 

eGovFrame - Start - New Web Project - Project name과 Group Id 설정 후 - Next - Generate Example 창에서 

"Generate Example"을 체크하게 되면 전자정부프레임웤에서 모든 환경설정을 해주게 되고 Controller 클래스가 정상적으로 인식이 된다.
그러나 만일 전자정부프레임웤의 기본 환경설정과 다르게 설정하기 원한다거나 그렇게 디폴트로 생성된 파일들 등을 사용하기 원치않는다면 이때 어떻게 설정해야
Controller 클래스를 인식하게 할수 있을까?
이에 대한 환경 설정에 대해서 정리해 보고자 한다.
본 포스트는 이해를 수이하기 위해서 DB 설정은 제외하고 순전히 Controller 클래스의 등록에 대한 부분만 다룬다.

eGovFrame - Start - New Web Project - Project name과 Group Id 설정 후 - Next - Generate Example 창에서 - "Generate Example"을 체크해제 - Finish

이렇게 프로젝트를 생성해서 아래와 같이 해당 프로젝트를 실행하면 404 에러를 출력하는 페이지만 보일 것이다(프로젝트명이 TestTest라고 하자).

http://localhost:8081/TestTest/

그렇다고 프로젝트가 잘못 생성된 것은 아니다. 확인하기 위해서 간단하게 index.jsp를 생성해서 실행해 보자.
해당 프로젝트의 src/main/webapp/ 아래에 index.jsp를 생성해야 한다. 중요한것은 webapp 폴더가 Root 폴더로 인식되기 때문이다.
Webapp 폴더에서 마우스 우측 클릭 - New - JSP File - File name을 index.jsp로 지정 후 Finish

<body> 태그 안에 
<h2>Hello world. I am index.jsp</h2>
를 추가후 해당 프로젝트 명에서 우측 클릭 후 Run As - Run on Server - Finish

정상적으로 index.jsp의 내용이 보일것이다.

이제부터 Controller 클래스가 인식되도록 환경 설정을 할 것이다. 우선 web.xml 파일을 아래의 내용과 같이 설정 내용을 작성한다.
최초 web.xml에는 아래의 내용만 되어 있을 것이다.

  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>


아래는 web.xml의 Controller 클래스가 인식되도로 하기 위한 기본 설정 내용이다.


*** web.xml의 기본 설정 내용 ***

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
    id="WebApp_ID" version="2.5">
  <display-name>MyTest</display-name>
  
        <!-- 인코딩 관련 -->
  	<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>
	</filter>
	<filter-mapping>
		<filter-name>encodingFilter</filter-name>
		<url-pattern>*.do</url-pattern>
	</filter-mapping>
    
	<!-- 크로스 스크립팅이라는 해킹 기법을 사전에 막는 기능. -->
	<filter>
		<filter-name>HTMLTagFilter</filter-name>
		<filter-class>egovframework.rte.ptl.mvc.filter.HTMLTagFilter</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>HTMLTagFilter</filter-name>
		<url-pattern>*.do</url-pattern>
	</filter-mapping>
    

	<!-- 어떤 객체들을 미리 만들어 놓을지가 작성된 설정 파일의 경로를 값으로 할당 -->
 	<context-param>
		<param-name>contextConfigLocation</param-name>
		<!-- Spring 환경설정 파일인 context-*.xml을 읽은다. 
		     src/main/resources/egov/spring/context-*.xml을 의미한다. -->
		<param-value>classpath*:egov/spring/context-*.xml</param-value>
	</context-param>

	<!--  -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
    
	<!-- MVC에서 Controller 역할을 하게 될 DispatcherServlet 객체 등록.
		*.do라는 요청이 들어오면 appServlet이름의 서블릿 클래스인
		org.springframework.web.servlet.DispatcherServlet를 실행한다.
		DispatcherServlet 클래스에 대한 설정 내용은 /WEB-INF/spring/appServlet/dispatcher-servlet.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/spring/appServlet/dispatcher-servlet.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>
    
	<servlet-mapping>
		<servlet-name>appServlet</servlet-name>
		<url-pattern>*.do</url-pattern>
	</servlet-mapping>
		
	
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
</web-app>


아래는 context-root.xml을 설정내용인데 현재 시점에서는 아래와 같이 파일만 생성해 둔다.

*** context-root.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"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
    
</beans>


아래는 DispatcherServlet을 위한 환경 설정이다.


*** dispatcher-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:context="http://www.springframework.org/schema/context"
		xmlns:mvc="http://www.springframework.org/schema/mvc"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
                http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
                http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
                
    <!-- xmlns:mvc="http://www.springframework.org/schema/mvc" -->
    
    	<!-- annotation을 사용하도록 설정  -->
    	<mvc:annotation-driven /> <!-- 근데 이것 없이도 정상적으로 동작함  -->
    	
   <!--
    base-package는 src/main/java/ 하위에 있는 패키지 명 중에서 ""안에 지정한 패키지 이하를 스캔하라는 뜻임.
    즉 base-package="egovframework"라고 하면 
    egovframework.example.cmmn
    egovframework.example.cmmn.web
    egovframework.example.sample.service
    egovframework.example.sample.service.impl
    egovframework.example.sample.web
    모두가 다 해당된다.
    
    base-package="egovframework.example"과 같이 해도 된다.
    
    다음과 같은 식으로도 가능
    <context:component-scan base-package="com.yk.yboard, com.yk.common"/>
    
    아래는 버스앱의 경우에 대한 것이다.
    <context:component-scan base-package="com.hubizict.bus" />
    
    <context:include-filter type="" expression=""/>는 자동 스캔 대상에 포함시킬 클래스를 의미
    <context:exclude-filter type="" expression=""/>는 자동 스캔 대상에 포함시키지 않을 클래스를 지정하는 의미 
     -->
    <context:component-scan base-package="com.joe">
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Repository"/>
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Service"/>
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Component"/>
    </context:component-scan>
    	
</beans>


이상의 작업을 통해서 eGov 프레임워크가 Controller 클래스를 인식하도록 설정이 되었다.
이제 Controller 클래스를 간단하게 만들어 보자. 해당 프로젝트의 src/main/java/ 아래에 패키지를 만든다.
src/main/java/ 위에서 마우스 우측 클릭 - New - Package
패키지 이름을 com.joe.egov.test를 만들어 보자.
생성된 패키지명 위에서 마우스 우측 클릭 - New - Class - MyController로 컨트롤러 클래스를 만들자.
이제 MyController 클래스를 다음과 같이 작성해 보자.


package com.joe.egov.test;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class MyController {
	@RequestMapping(value="/mytest.do", method=RequestMethod.GET)
	public String test(){
		System.out.println("####### Hello this is MyController 클래스 ");
		return "test.jsp";
	}
}


이제 webapp 폴더 아래에 test.jsp파일을 생성해서 원하는 내용으로 작성해두면 MyController 실행후 test.jsp가 실행되어 확인이 편리할 것이다.
아래와 같이 실행해서 컨트롤러 클래스가 정상적으로 작동하는지 확인해 보자.

http://localhost:8081/TestTest/mytest.do

+ Recent posts