Java/Struts2

스트럿츠2 설정하기

체리필터 2009. 4. 28. 15:22
728x90
반응형
스트럿츠를 사용하기 위해 스트럿츠 환경을 구축할 필요가 있다.
우선은 이클립스에서 다음과 같이 'Dynamic Web Project'를 하나 생성한다.


그런 다음 위에서 볼 수 있는 것처럼 몇가지 내용을 추가해야 한다.
우선은 필요한 라이브러리를 추가해 보자.
http://struts.apache.org/에서 해당 버전을 다운받는다. 현재 시점에서 가장 최신 버젼은 2.1.6이다.(http://struts.apache.org/download.cgi#struts216)
다운 받은 파일의 압축을 풀면 lib 디렉토리에 jar 파일들이 있는데, 그 중에서 필요한 jar 파일들을 추가한다.
추가해야 할 파일 목록은 다음과 같다.

antlr-2.7.2.jar
commons-beanutils-1.7.0.jar
commons-chain-1.2.jar
commons-fileupload-1.2.1.jar
commons-logging-1.0.4.jar
commons-logging-api-1.1.jar
commons-validator-1.3.1.jar
freemarker-2.3.13.jar
jstl.jar
ognl-2.6.11.jar
oro-2.0.8.jar
standard.jar
struts-core-1.3.10.jar
struts2-core-2.1.6.jar
xwork-2.1.2.jar

jstl.jar 파일과 standard.jar 파일은 JSTL을 사용하기 위해 이미 앞에서 추가한 파일이며, 그외 다른 파일들도 추가한다.
에이콘에서 나온 '스트럿츠2 프로그래밍' 책을 기초로 셋팅을 따라 한 것인데 해당 책에는 특히 commons-fileupload-1.2.1.jar 라이브러리에 대한 업급이 없다.
commons-fileupload-1.2.1.jar 파일 없이 server를 구동 시킬 경우 에러가 발생 했었다.

라이브러리 추가 후에는 설정 파일을 수정해야 한다.

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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 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>struts2</display-name>
  <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>
 
  <filter>
      <filter-name>struts</filter-name>
      <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
  </filter>
  <filter-mapping>
      <filter-name>struts</filter-name>
      <url-pattern>/*</url-pattern>
  </filter-mapping>
 
</web-app>

struts.xml

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

<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
    <package name="default" extends="struts-default" namespace="">
    </package>
</struts>

struts.properties

struts.i18n.reload=true
struts.devMode=false
struts.configuration.xml.reload=true
struts.continuations.package=org.apache.struts2.showcase
struts.custon.i18n.resources=globalMessages
#struts.action.extension=jspa
struts.url.http.port=8080
#struts.freemarker.manager.classname=customFreemarkerManager
struts.serve.static=true
struts.serve.static.browserCache=false
struts.multipart.maxSize=2097252

web.xml은 WebContent/WEB-INF/ 아래에 만들면 되며, struts.xml, struts.properties 파일은 src 밑에 둔다.(이클립스에서 자동으로 classes 밑으로 배포해 준다.)
위와 같이 설정 한 후 이클립스에서 서버를 추가해 준다.

Tomcat 6.0으로 선택

Tomcat 6.0으로 선택



추가 된 서버를 더블 클릭하면 아래와 같은 창이 뜬다.

Server name을 struts2로 바꾼다.(이름은 아무렇게나 해도 된다.)

Server name을 struts2로 바꾼다.(이름은 아무렇게나 해도 된다.)


아래 Modules 탭으로 이동 후 아래 그림과 같이 나오면 다음 내용을 진행한다.

Add External Web Module... 클릭

Add External Web Module... 클릭



Browser를 눌러 Document Root를 지정해 준다

Browser를 눌러 Document Root를 지정해 준다



Document Root는 프로젝트 생성할 때 자동으로 생성된 Directory 중 WebContent 디렉토리를 지정해 준다.
이와 같이 지정 후 저장한 다음 서버 에디팅 창을 닫는다.
그런 다음 생성된 struts2 서버를 start 한다.
이상 없이 start 가 된다면 다음과 같이 간단한 struts예제를 만들어 시험해 본다.

struts.xml

<struts>
    <package name="default" extends="struts-default" namespace="">
        <action name="printString" class="example.chapter2.PrintStringAction">
            <result>/chapter2/printString.jsp</result>
        </action>
    </package>
</struts>

package example.chapter2;

public class PrintStringAction {
    private String greetings;
   
    public String execute() throws Exception {
        greetings = "반갑다! 스트럿츠2.";
        return "success";
    }
   
    public String getGreetings() { return greetings; }
    public void setGreetings(String greetings) {
        this.greetings = greetings;
    }
}


<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!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>
    <s:property value="greetings" />
</body>
</html>

struts.xml에서 정의한 것처럼, example.chapter2 패키지 않에 PrintStringAction.java 파일을 만들고, jsp 파일은 /WebContent/chapter2/printString.jsp로 만든다.
jsp에서는 "<%@ taglib prefix="s" uri="/struts-tags" %>"와 같이 strus에서 사용할 tag lib를 선언한 다음, PrintStringAction에서 정의한 greetings 변수를 "<s:property value="greetings" />"와 같이 출력해 준다.
<s:property/>에서 사용한 value부분은 Action 파일에서 정의한 getter를 사용해서 값을 가져 온다.(OGNL 사용)
만일 getter가 없다면 오류가 나지는 않으며, 단지 화면에 내용이 출력되지 않는다.

728x90
반응형

'Java > Struts2' 카테고리의 다른 글

스트럿츠를 사용하여 객체 출력하기  (0) 2009.05.11
스트럿츠를 사용하여 List 출력하기  (1) 2009.05.06