프레임워크/전자정부프레임워크

[전자정부프레임워크] 간단한 Hello World 작성하기

투칼론 2016. 4. 25. 17:10
반응형

간단하게 작성순서는 아래와 같다. 


1. helloworld controller 작성 (비즈니스 로직 처리)

2. helloworld jsp 작성 (View 처리)

3. web.xml 파일 작성 (한글 encoding 설정, DispacherSevlet 등록, spring context 파일 위치)

4. spring-context.xml 파일 작성(Bean 스캔, ViewResolver에서 사용할 jsp 등록)



1. helloworld controller 작성


package helloworld;


import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;


@Controller

public class HelloWorldController {

@RequestMapping(value="/hello.do")

public String HelloWorld() {

return "helloworld";

}

} 


DispatcherServlet에서 URL에 "/hello.do"를 매핑하여, helloworld controller로 보내주면, 처리 결과를 "helloworld" 라는 JSP로 리턴한다.


 


2. helloworld jsp 작성


<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

<!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=UTF-8">

<title>Insert title here</title>

</head>

<body>

<h1>Hello World !!</h1>

</body>

</html> 


helloworld.jsp 파일은 단순하게 "Hello World !!" 문자열을 브라우저에 디스플레이한다.



3. web.xml 파일 작성


<?xml version="1.0" encoding="UTF-8"?>

<web-app id="WebApp_ID" version="2.5" 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">

<display-name>Lab301-mvc</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>


<!-- Spring  context configuration -->

<context-param>

<param-name>contextConfigLocation</param-name>

<param-value>classpath*:spring/context-*.xml</param-value>

</context-param>

<listener>

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

</listener>

<!-- Spring WEB context configuration -->

<servlet>

<servlet-name>mvcAction</servlet-name>

<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

<init-param>

<param-name>contextConfigLocation</param-name>

<param-value>/WEB-INF/config/springmvc/context-*.xml</param-value>

</init-param>

<load-on-startup>1</load-on-startup>

</servlet>


<servlet-mapping>

<servlet-name>mvcAction</servlet-name>

<url-pattern>*.do</url-pattern>

</servlet-mapping>


<welcome-file-list>

<welcome-file>index.jsp</welcome-file>

</welcome-file-list>

</web-app> 


한글 처리를 위해 encoding 필터를 등록하고, spring 관련 context 파일 위치를 파라미터로 설정한다.



4. spring-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:p="http://www.springframework.org/schema/p"

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">


    <!-- scan component -->

    <context:component-scan base-package="helloworld"/>

    

    <!-- set view resolver -->

    <bean  class="org.springframework.web.servlet.view.InternalResourceViewResolver"

        p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" />

</beans> 


IoC 아키텍처에서 bean을 등록하기 위한 스캔 룰을 등록하고, View에 대한 Resolver에 대해 관련 정보를 주기위한 정보를 등록한다.