2010년 8월 17일 화요일

Struts2 - The Beginning

helloworld action

1. web.xml에 filter추가

    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
    </filter>
   
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>   
    </filter-mapping>

2. action 패키지에 액션클래스 생성


package action;

public class HelloWorld
{
    private String message;

    public String getMessage()
    {
        return message;
    }

    public String execute()
    {
        message = "helloworld";
        return "success";
    }
}

3. src에 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="kwon" extends="struts-default">    
    <!-- struts-default를 상속받는다 struts2-core-2.0.14.jar에 들어있음-->
        <action name="HelloWorld" class="action.HelloWorld">
        <!--HelloWorld.action으료 요청하면 =>
              action.HelloWorld 클래스 =>
              execute()메소드 실행 -->
        <result name="success">/helloworld.jsp</result>       
        <!-- HelloWorld.execute의 리턴값이 success이면 /helloworld.jsp페이지 실행 -->
        </action>
    </package>
</struts>

4. jar 파일 다섯개 lib에 넣기

commons-logging-1.0.4.jar
freemarker-2.3.8.jar
ognl-2.6.11.jar
struts2-core-2.0.14.jar
xwork-2.0.7.jar

5. result 페이지 helloworld.jsp 만들기

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<html>
<head>
<title></title>
</head>
<body>
    helloworld.jsp<br>
    ${message }
    <!-- helloworld 출력 HelloWorld클래스의 getMessage 
           && 이때 message는 메소드이름을 명시해야함
           ex)getText일때 ${text}
    -->

</body>
</html>



.

댓글 없음:

댓글 쓰기